Skip to content

Commit

Permalink
Refactored formula's use of boost::intrusive_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Jul 24, 2016
1 parent 5850d1d commit efcca8f
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 86 deletions.
1 change: 0 additions & 1 deletion src/actions/undo.cpp
Expand Up @@ -54,7 +54,6 @@
#include "undo_update_shroud_action.hpp"

#include <algorithm> // for reverse
#include <boost/intrusive_ptr.hpp> // for intrusive_ptr
#include <cassert> // for assert
#include <ostream> // for operator<<, basic_ostream, etc
#include <set> // for set
Expand Down
1 change: 0 additions & 1 deletion src/ai/contexts.cpp
Expand Up @@ -58,7 +58,6 @@
#include "formula/variant.hpp" // for variant

#include <algorithm> // for find, count, max, fill_n
#include <boost/smart_ptr/intrusive_ptr.hpp> // for intrusive_ptr
#include <cmath> // for sqrt
#include <cstdlib> // for abs
#include <ctime> // for time
Expand Down
6 changes: 0 additions & 6 deletions src/ai/formula/ai.cpp
Expand Up @@ -54,7 +54,6 @@
#include "ai/game_info.hpp" // for move_result_ptr, move_map, etc
#include "candidates.hpp" // for base_candidate_action, etc

#include <boost/intrusive_ptr.hpp> // for intrusive_ptr
#include <cassert> // for assert
#include <ctime> // for time
#include <map> // for multimap<>::const_iterator, etc
Expand Down Expand Up @@ -112,7 +111,6 @@ formula_ai::formula_ai(readonly_context &context, const config &cfg)
vars_(),
function_table_(*this)
{
add_ref();
init_readonly_context_proxy(context);
LOG_AI << "creating new formula ai"<< std::endl;
}
Expand Down Expand Up @@ -167,7 +165,6 @@ std::string formula_ai::evaluate(const std::string& formula_str)
game_logic::formula f(formula_str, &function_table_);

game_logic::map_formula_callable callable(this);
callable.add_ref();

//formula_debugger fdb;
const variant v = f.evaluate(callable,nullptr);
Expand Down Expand Up @@ -531,7 +528,6 @@ variant formula_ai::execute_variant(const variant& var, ai_context &ai_, bool co
*/

game_logic::map_formula_callable callable(this);
callable.add_ref();

if(error != variant())
callable.add("error", error);
Expand Down Expand Up @@ -934,7 +930,6 @@ bool formula_ai::can_reach_unit(map_location unit_A, map_location unit_B) const

void formula_ai::on_create(){
//make sure we don't run out of refcount
vars_.add_ref();

for(const config &func : cfg_.child_range("function"))
{
Expand Down Expand Up @@ -978,7 +973,6 @@ void formula_ai::evaluate_candidate_action(ca_ptr fai_ca)
bool formula_ai::execute_candidate_action(ca_ptr fai_ca)
{
game_logic::map_formula_callable callable(this);
callable.add_ref();
fai_ca->update_callable_map( callable );
const_formula_ptr move_formula(fai_ca->get_action());
return !make_action(move_formula, callable).is_empty();
Expand Down
3 changes: 0 additions & 3 deletions src/ai/formula/candidates.cpp
Expand Up @@ -77,7 +77,6 @@ candidate_action_with_filters::candidate_action_with_filters(
variant candidate_action_with_filters::do_filtering(ai::formula_ai* ai, variant& input, game_logic::const_formula_ptr formula)
{
game_logic::map_formula_callable callable(static_cast<const formula_callable*>(ai));
callable.add_ref();
callable.add("input", input);

return formula::evaluate(formula, callable);
Expand Down Expand Up @@ -123,7 +122,6 @@ void move_candidate_action::evaluate(ai::formula_ai* ai, unit_map& units)
for(variant_iterator i = filtered_units.begin() ; i != filtered_units.end() ; ++i)
{
game_logic::map_formula_callable callable(static_cast<const formula_callable*>(ai));
callable.add_ref();
callable.add("me", *i);

int res = execute_formula(eval_, callable, ai);
Expand Down Expand Up @@ -227,7 +225,6 @@ void attack_candidate_action::evaluate(ai::formula_ai* ai, unit_map& units)
if( ai->can_reach_unit( my_unit_callalbe->get_location(), enemy_units_flt[enemy_unit]->get_location() )) {

game_logic::map_formula_callable callable(static_cast<const formula_callable*>(ai));
callable.add_ref();
callable.add("me", filtered_my_units[my_unit]);
callable.add("target", filtered_enemy_units[enemy_unit]);

Expand Down
1 change: 0 additions & 1 deletion src/ai/formula/stage_side_formulas.cpp
Expand Up @@ -46,7 +46,6 @@ stage_side_formulas::~stage_side_formulas()
bool stage_side_formulas::do_play_stage()
{
game_logic::map_formula_callable callable(&fai_);
callable.add_ref();
try {
if (move_formula_) {
while( !fai_.make_action(move_formula_,callable).is_empty() ) { }
Expand Down
3 changes: 0 additions & 3 deletions src/ai/formula/stage_unit_formulas.cpp
Expand Up @@ -66,7 +66,6 @@ bool stage_unit_formulas::do_play_stage()
game_logic::const_formula_ptr priority_formula(fai_.create_optional_formula(i->formula_manager().get_priority_formula()));
if (priority_formula) {
game_logic::map_formula_callable callable(&fai_);
callable.add_ref();
callable.add("me", variant(new unit_callable(*i)));
priority = (game_logic::formula::evaluate(priority_formula, callable)).as_int();
} else {
Expand Down Expand Up @@ -100,7 +99,6 @@ bool stage_unit_formulas::do_play_stage()
game_logic::const_formula_ptr formula(fai_.create_optional_formula(i->formula_manager().get_formula()));
if (formula) {
game_logic::map_formula_callable callable(&fai_);
callable.add_ref();
callable.add("me", variant(new unit_callable(*i)));
fai_.make_action(formula, callable);
} else {
Expand All @@ -123,7 +121,6 @@ bool stage_unit_formulas::do_play_stage()
game_logic::const_formula_ptr loop_formula(fai_.create_optional_formula(i->formula_manager().get_loop_formula()));
if (loop_formula) {
game_logic::map_formula_callable callable(&fai_);
callable.add_ref();
callable.add("me", variant(new unit_callable(*i)));
while ( !fai_.make_action(loop_formula, callable).is_empty() && i.valid() )
{
Expand Down
14 changes: 3 additions & 11 deletions src/formula/callable.hpp
Expand Up @@ -33,7 +33,7 @@ struct formula_input {
};

//interface for objects that can have formulae run on them
class formula_callable : public reference_counted_object {
class formula_callable {
public:
explicit formula_callable(bool has_self=true) : type_(FORMULA_C), has_self_(has_self)
{}
Expand Down Expand Up @@ -104,14 +104,6 @@ class formula_callable : public reference_counted_object {
bool has_self_;
};

class formula_callable_no_ref_count : public formula_callable {
public:
formula_callable_no_ref_count() {
turn_reference_counting_off();
}
virtual ~formula_callable_no_ref_count() {}
};

class formula_callable_with_backup : public formula_callable {
const formula_callable& main_;
const formula_callable& backup_;
Expand Down Expand Up @@ -175,8 +167,8 @@ class map_formula_callable : public formula_callable {
const formula_callable* fallback_;
};

typedef boost::intrusive_ptr<map_formula_callable> map_formula_callable_ptr;
typedef boost::intrusive_ptr<const map_formula_callable> const_map_formula_callable_ptr;
typedef std::shared_ptr<map_formula_callable> map_formula_callable_ptr;
typedef std::shared_ptr<const map_formula_callable> const_map_formula_callable_ptr;

}

Expand Down
6 changes: 2 additions & 4 deletions src/formula/callable_fwd.hpp
Expand Up @@ -15,14 +15,12 @@
#ifndef FORMULA_CALLABLE_FWD_HPP_INCLUDED
#define FORMULA_CALLABLE_FWD_HPP_INCLUDED

#include <boost/intrusive_ptr.hpp>

namespace game_logic {

class formula_callable;
class formula_debugger;
typedef boost::intrusive_ptr<formula_callable> formula_callable_ptr;
typedef boost::intrusive_ptr<const formula_callable> const_formula_callable_ptr;
typedef std::shared_ptr<formula_callable> formula_callable_ptr;
typedef std::shared_ptr<const formula_callable> const_formula_callable_ptr;

}

Expand Down
6 changes: 0 additions & 6 deletions src/formula/function.cpp
Expand Up @@ -98,7 +98,6 @@ class dir_function : public function_expression {
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
variant var = args()[0]->evaluate(variables, fdb);
const formula_callable* callable = var.as_callable();
callable->add_ref();
std::vector<formula_input> inputs = callable->inputs();
std::vector<variant> res;
for(size_t i=0; i<inputs.size(); ++i) {
Expand Down Expand Up @@ -767,7 +766,6 @@ class choose_function : public function_expression {
}
} else {
map_formula_callable self_callable;
self_callable.add_ref();
const std::string self = args()[1]->evaluate(variables,fdb).as_string();
for(variant_iterator it = items.begin(); it != items.end(); ++it) {
self_callable.add(self, *it);
Expand Down Expand Up @@ -937,7 +935,6 @@ class filter_function : public function_expression {
}
} else {
map_formula_callable self_callable;
self_callable.add_ref();
const std::string self = args()[1]->evaluate(variables,fdb).as_string();
for(variant_iterator it = items.begin(); it != items.end(); ++it) {
self_callable.add(self, *it);
Expand Down Expand Up @@ -975,7 +972,6 @@ class find_function : public function_expression {
}
} else {
map_formula_callable self_callable;
self_callable.add_ref();
const std::string self = args()[1]->evaluate(variables,fdb).as_string();
for(variant_iterator it = items.begin(); it != items.end(); ++it){
self_callable.add(self, *it);
Expand Down Expand Up @@ -1011,7 +1007,6 @@ class map_function : public function_expression {
}
} else {
map_formula_callable self_callable;
self_callable.add_ref();
const std::string self = args()[1]->evaluate(variables,fdb).as_string();
for(variant_iterator it = items.begin(); it != items.end(); ++it) {
self_callable.add(self, *it);
Expand Down Expand Up @@ -1117,7 +1112,6 @@ class reduce_function : public function_expression {
++it;
}
map_formula_callable self_callable;
self_callable.add_ref();
for(; it != items.end(); ++it) {
self_callable.add("a", res);
self_callable.add("b", *it);
Expand Down

0 comments on commit efcca8f

Please sign in to comment.