Clean up pointer use:

* Use unique_ptr for Actions instead of manual memory management
 * Use unique_ptr in KeywordDatabase
 * Use unique_ptr instead of shared_ptr for file formats
 * Don't pass shared_ptr to Reader/Writer, use references instead
Also
 * Switch to C++17 so we can use map::try_emplace
This commit is contained in:
Twan van Laarhoven
2020-04-25 21:30:05 +02:00
parent 708b4389a0
commit 64ea1d7322
57 changed files with 363 additions and 385 deletions
+1 -1
View File
@@ -378,7 +378,7 @@ SCRIPT_FUNCTION(copy_file) {
String out_path = get_export_full_path(out_name);
// copy
ExportInfo& ei = *export_info();
InputStreamP in = ei.export_template->openIn(input);
auto in = ei.export_template->openIn(input);
wxFileOutputStream out(out_path);
if (!out.Ok()) throw Error(_("Unable to open file '") + out_path + _("' for output"));
out.Write(*in);
+3 -3
View File
@@ -213,9 +213,9 @@ void TokenIterator::readToken() {
// read the entire file, and start at the beginning of it
pos = 0;
filename = include_file;
InputStreamP is = package_manager.openFileFromPackage(package, include_file);
eat_utf8_bom(*is);
input = read_utf8_line(*is, true);
auto stream = package_manager.openFileFromPackage(package, include_file);
eat_utf8_bom(*stream);
input = read_utf8_line(*stream, true);
} else if (isAlpha(c) || isUnicodeAlpha(c) || c == _('_') || (isDigit(c) && !buffer.empty() && buffer.back() == _("."))) {
// name, or a number after a . token, as in array.0
size_t start = pos - 1;
+17 -37
View File
@@ -21,57 +21,37 @@
#include <data/action/keyword.hpp>
#include <util/error.hpp>
typedef map<const StyleSheet*,Context*> Contexts;
DECLARE_TYPEOF(Contexts);
DECLARE_TYPEOF_COLLECTION(CardP);
DECLARE_TYPEOF_COLLECTION(FieldP);
DECLARE_TYPEOF_COLLECTION(Dependency);
DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA StyleP>);
DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA ValueP>);
//#define LOG_UPDATES
// ----------------------------------------------------------------------------- : SetScriptContext : initialization
SetScriptContext::SetScriptContext(Set& set)
: set(set)
{}
SetScriptContext::~SetScriptContext() {
// destroy contexts
FOR_EACH(sc, contexts) {
delete sc.second;
}
}
Context& SetScriptContext::getContext(const StyleSheetP& stylesheet) {
Contexts::iterator it = contexts.find(stylesheet.get());
if (it != contexts.end()) {
return *it->second; // we already have a context
} else {
// create a new context
Context* ctx = new Context();
contexts.insert(make_pair(stylesheet.get(), ctx));
auto it = contexts.try_emplace(stylesheet.get());
Context& ctx = it.first->second;
if (it.second) {
// we created a new context
// variables
// NOTE: do not use a smart pointer for the pointer to the set, because the set owns this
// which would lead to a reference cycle.
init_script_functions(*ctx);
ctx->setVariable(SCRIPT_VAR_set, make_intrusive<ScriptObject<Set*>>(&set));
ctx->setVariable(SCRIPT_VAR_game, to_script(set.game));
ctx->setVariable(SCRIPT_VAR_stylesheet, to_script(stylesheet));
ctx->setVariable(SCRIPT_VAR_card_style, to_script(&stylesheet->card_style));
ctx->setVariable(SCRIPT_VAR_card, set.cards.empty() ? script_nil : to_script(set.cards.front())); // dummy value
ctx->setVariable(SCRIPT_VAR_styling, to_script(&set.stylingDataFor(*stylesheet)));
init_script_functions(ctx);
ctx.setVariable(SCRIPT_VAR_set, make_intrusive<ScriptObject<Set*>>(&set));
ctx.setVariable(SCRIPT_VAR_game, to_script(set.game));
ctx.setVariable(SCRIPT_VAR_stylesheet, to_script(stylesheet));
ctx.setVariable(SCRIPT_VAR_card_style, to_script(&stylesheet->card_style));
ctx.setVariable(SCRIPT_VAR_card, set.cards.empty() ? script_nil : to_script(set.cards.front())); // dummy value
ctx.setVariable(SCRIPT_VAR_styling, to_script(&set.stylingDataFor(*stylesheet)));
try {
// perform init scripts, don't use a scope, variables stay bound in the context
set.game ->init_script.invoke(*ctx, false);
stylesheet->init_script.invoke(*ctx, false);
set.game ->init_script.invoke(ctx, false);
stylesheet->init_script.invoke(ctx, false);
} catch (const Error& e) {
handle_error(e);
}
onInit(stylesheet, ctx);
return *ctx;
}
return ctx;
}
Context& SetScriptContext::getContext(const CardP& card) {
StyleSheetP stylesheet = set.stylesheetForP(card);
@@ -100,13 +80,13 @@ SetScriptManager::~SetScriptManager() {
set.actions.removeListener(this);
}
void SetScriptManager::onInit(const StyleSheetP& stylesheet, Context* ctx) {
void SetScriptManager::onInit(const StyleSheetP& stylesheet, Context& ctx) {
assert(wxThread::IsMain());
// initialize dependencies
try {
// find script dependencies
initDependencies(*ctx, *set.game);
initDependencies(*ctx, *stylesheet);
initDependencies(ctx, *set.game);
initDependencies(ctx, *stylesheet);
} catch (const Error& e) {
handle_error(e);
}
+6 -7
View File
@@ -28,21 +28,20 @@ DECLARE_POINTER_TYPE(Style);
/// Manager of the script context for a set
class SetScriptContext {
public:
public:
SetScriptContext(Set& set);
virtual ~SetScriptContext();
/// Get a context to use for the set, for a given stylesheet
Context& getContext(const StyleSheetP&);
/// Get a context to use for the set, for a given card
Context& getContext(const CardP&);
protected:
Set& set; ///< Set for which we are managing scripts
map<const StyleSheet*,Context*> contexts; ///< Context for evaluating scripts that use a given stylesheet
protected:
Set& set; ///< Set for which we are managing scripts
map<const StyleSheet*,Context> contexts; ///< Context for evaluating scripts that use a given stylesheet
/// Called when a new context for a stylesheet is initialized
virtual void onInit(const StyleSheetP& stylesheet, Context* ctx) {}
virtual void onInit(const StyleSheetP& stylesheet, Context& ctx) {}
};
@@ -73,7 +72,7 @@ class SetScriptManager : public SetScriptContext, public ActionListener {
void updateAll();
private:
virtual void onInit(const StyleSheetP& stylesheet, Context* ctx);
void onInit(const StyleSheetP& stylesheet, Context& ctx) override;
void initDependencies(Context&, Game&);
void initDependencies(Context&, StyleSheet&);