Skip to content

Commit

Permalink
WFL/Formula: some minor code refactoring and build fix from e50a639
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Nov 14, 2017
1 parent e50a639 commit 608de6a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/ai/formula/ai.cpp
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/ai/formula/function_table.cpp
Expand Up @@ -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##_function>(#name, ai)))
#define DECLARE_FAI_FUNCTION(name) \
add_function(#name, std::make_shared<ai_formula_function<name##_function>>(#name, ai));

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Nov 15, 2017

Member

Please remove the final semicolon.


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>()))
Expand Down
8 changes: 3 additions & 5 deletions src/formula/formula.cpp
Expand Up @@ -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
)
);

Expand Down
16 changes: 8 additions & 8 deletions src/formula/function.cpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1417,23 +1417,23 @@ 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)
: parent(parent ? parent : get_builtins())
{
}

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);
}
Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/formula/function.hpp
Expand Up @@ -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) \
{ \
} \
\
Expand All @@ -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));

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Nov 15, 2017

Member

Please remove the final semicolon.


struct call_stack_manager
{
Expand Down Expand Up @@ -217,7 +217,7 @@ class builtin_formula_function : public formula_function

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);
}
};

Expand All @@ -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());
}
Expand Down

0 comments on commit 608de6a

Please sign in to comment.