diff --git a/src/ai/formula/ai.cpp b/src/ai/formula/ai.cpp index c155ba600bc9..b7841f0c1535 100644 --- a/src/ai/formula/ai.cpp +++ b/src/ai/formula/ai.cpp @@ -265,7 +265,7 @@ pathfind::teleport_map formula_ai::get_allowed_teleports(unit_map::iterator& uni void formula_ai::add_formula_function(const std::string& name, const_formula_ptr formula, const_formula_ptr precondition, const std::vector& args) { formula_function_ptr fcn(new user_formula_function(name,formula,precondition,args)); - function_table_.add_function(name, fcn); + function_table_.add_function(name, std::move(fcn)); } namespace { diff --git a/src/ai/formula/function_table.cpp b/src/ai/formula/function_table.cpp index a10caddcce51..a913c78b3da5 100644 --- a/src/ai/formula/function_table.cpp +++ b/src/ai/formula/function_table.cpp @@ -1353,8 +1353,8 @@ class ai_formula_function : public formula_function { // This macro is for functions taking an additional formula_ai argument. // Functions using the other macro could potentially be made core. -#define DECLARE_FAI_FUNCTION(name) add_function(#name, formula_function_ptr( \ - new ai_formula_function(#name, ai))) +#define DECLARE_FAI_FUNCTION(name) \ + add_function(#name, std::make_shared>(#name, ai)); ai_function_symbol_table::ai_function_symbol_table(ai::formula_ai& ai) : function_symbol_table(std::make_shared(std::make_shared())) diff --git a/src/formula/formula.cpp b/src/formula/formula.cpp index 8aa1b3814006..d10f46d3e0cd 100644 --- a/src/formula/formula.cpp +++ b/src/formula/formula.cpp @@ -1265,11 +1265,9 @@ expression_ptr parse_expression(const tk::token* i1, const tk::token* i2, functi } symbols->add_function(formula_name, - formula_function_ptr( - new user_formula_function( - formula_name, const_formula_ptr(new formula(beg, i1, symbols)), - formula::create_optional_formula(precond, symbols), args - ) + std::make_shared( + formula_name, const_formula_ptr(new formula(beg, i1, symbols)), + formula::create_optional_formula(precond, symbols), args ) ); diff --git a/src/formula/function.cpp b/src/formula/function.cpp index 68c79cb2883d..05370e81e5be 100644 --- a/src/formula/function.cpp +++ b/src/formula/function.cpp @@ -87,7 +87,7 @@ DEFINE_WFL_FUNCTION(debug, 0, 1) if(fdb == nullptr) { fdbp.reset(new formula_debugger()); - fdb = &*fdbp; + fdb = fdbp.get(); need_wrapper = true; } @@ -363,7 +363,7 @@ DEFINE_WFL_FUNCTION(tomap, 1, 2) if(auto kv = (*it).try_convert()) { tmp[kv->query_value("key")] = kv->query_value("value"); } else { - std::map::iterator map_it = tmp.find(*it); + auto map_it = tmp.find(*it); if(map_it == tmp.end()) { tmp[*it] = variant(1); @@ -1417,7 +1417,7 @@ variant formula_function_expression::execute(const formula_callable& variables, function_expression_ptr user_formula_function::generate_function_expression( const std::vector& args) const { - return function_expression_ptr(new formula_function_expression(name_, args, formula_, precondition_, args_)); + return std::make_shared(name_, args, formula_, precondition_, args_); } function_symbol_table::function_symbol_table(std::shared_ptr parent) @@ -1425,15 +1425,15 @@ function_symbol_table::function_symbol_table(std::shared_ptr(fcn)); } expression_ptr function_symbol_table::create_function( const std::string& fn, const std::vector& args) const { - const functions_map::const_iterator i = custom_formulas_.find(fn); + const auto i = custom_formulas_.find(fn); if(i != custom_formulas_.end()) { return i->second->generate_function_expression(args); } @@ -1455,8 +1455,8 @@ std::set function_symbol_table::get_function_names() const res = parent->get_function_names(); } - for(functions_map::const_iterator iter = custom_formulas_.begin(); iter != custom_formulas_.end(); ++iter) { - res.insert((*iter).first); + for(const auto& formula : custom_formulas_) { + res.insert(formula.first); } return res; diff --git a/src/formula/function.hpp b/src/formula/function.hpp index c5ce6a20a788..151a4b8f3e45 100644 --- a/src/formula/function.hpp +++ b/src/formula/function.hpp @@ -29,7 +29,7 @@ namespace wfl { \ public: \ explicit name##_function(const args_list& args) \ - : function_expression(#name, args, ##min_args, ##max_args) \ + : function_expression(#name, args, min_args, max_args) \ { \ } \ \ @@ -45,7 +45,7 @@ namespace wfl * The function must be defined by a `name_function` class which is accessible in the current scope. */ #define DECLARE_WFL_FUNCTION(name) \ - functions_table.add_function(#name, formula_function_ptr(new builtin_formula_function(#name))) + functions_table.add_function(#name, std::make_shared>(#name)); struct call_stack_manager { @@ -217,7 +217,7 @@ class builtin_formula_function : public formula_function function_expression_ptr generate_function_expression(const std::vector& args) const { - return function_expression_ptr(new T(args)); + return std::make_shared(args); } }; @@ -229,13 +229,13 @@ class function_symbol_table public: explicit function_symbol_table(std::shared_ptr parent = nullptr); - void add_function(const std::string& name, formula_function_ptr fcn); + void add_function(const std::string& name, formula_function_ptr&& fcn); expression_ptr create_function(const std::string& fn, const std::vector& args) const; std::set get_function_names() const; - bool empty() + bool empty() const { return custom_formulas_.empty() && (parent == nullptr || parent->empty()); }