WFL/Formula: some minor code refactoring and build fix from e50a63927bd1

This commit is contained in:
Charles Dang 2017-11-15 00:53:53 +11:00
parent e50a63927b
commit 608de6a42d
5 changed files with 19 additions and 21 deletions

View File

@ -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<std::string>& 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 {

View File

@ -1353,8 +1353,8 @@ public:
// 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##_function>(#name, ai)))
#define DECLARE_FAI_FUNCTION(name) \
add_function(#name, std::make_shared<ai_formula_function<name##_function>>(#name, ai));
ai_function_symbol_table::ai_function_symbol_table(ai::formula_ai& ai)
: function_symbol_table(std::make_shared<gamestate_function_symbol_table>(std::make_shared<action_function_symbol_table>()))

View File

@ -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<user_formula_function>(
formula_name, const_formula_ptr(new formula(beg, i1, symbols)),
formula::create_optional_formula(precond, symbols), args
)
);

View File

@ -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<key_value_pair>()) {
tmp[kv->query_value("key")] = kv->query_value("value");
} else {
std::map<variant, variant>::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<expression_ptr>& args) const
{
return function_expression_ptr(new formula_function_expression(name_, args, formula_, precondition_, args_));
return std::make_shared<formula_function_expression>(name_, args, formula_, precondition_, args_);
}
function_symbol_table::function_symbol_table(std::shared_ptr<function_symbol_table> parent)
@ -1425,15 +1425,15 @@ function_symbol_table::function_symbol_table(std::shared_ptr<function_symbol_tab
{
}
void function_symbol_table::add_function(const std::string& name, formula_function_ptr fcn)
void function_symbol_table::add_function(const std::string& name, formula_function_ptr&& fcn)
{
custom_formulas_[name] = fcn;
custom_formulas_.emplace(name, std::forward<formula_function_ptr>(fcn));
}
expression_ptr function_symbol_table::create_function(
const std::string& fn, const std::vector<expression_ptr>& 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<std::string> 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;

View File

@ -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##_function>(#name)))
functions_table.add_function(#name, std::make_shared<builtin_formula_function<name##_function>>(#name));
struct call_stack_manager
{
@ -217,7 +217,7 @@ public:
function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const
{
return function_expression_ptr(new T(args));
return std::make_shared<T>(args);
}
};
@ -229,13 +229,13 @@ class function_symbol_table
public:
explicit function_symbol_table(std::shared_ptr<function_symbol_table> 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<expression_ptr>& args) const;
std::set<std::string> get_function_names() const;
bool empty()
bool empty() const
{
return custom_formulas_.empty() && (parent == nullptr || parent->empty());
}