Skip to content

Commit

Permalink
AI: deployed std::make_shared in a whole bunch of places
Browse files Browse the repository at this point in the history
Also simplified a few instance of shared_ptr assignment. No need to create a temp ptr
just to immediately assign them.

(cherry-picked from commit ba150c0)
  • Loading branch information
Vultraz committed Oct 7, 2018
1 parent 2417af2 commit c80e506
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 17 deletions.
9 changes: 3 additions & 6 deletions src/ai/composite/aspect.hpp
Expand Up @@ -379,8 +379,7 @@ class standard_aspect : public typesafe_aspect<T> {
: typesafe_aspect<T>(context, cfg, id)
{
this->name_ = "standard_aspect";
std::shared_ptr<T> value(new T(config_value_translator<T>::cfg_to_value(this->cfg_)));
this->value_= value;
this->value_ = std::make_shared<T>(config_value_translator<T>::cfg_to_value(this->cfg_));
LOG_STREAM(debug, aspect::log()) << "standard aspect has value: "<< std::endl << config_value_translator<T>::value_to_cfg(this->get()) << std::endl;
}

Expand Down Expand Up @@ -502,8 +501,7 @@ class register_aspect_factory : public aspect_factory {

aspect_ptr get_new_instance( readonly_context &context, const config &cfg, const std::string &id)
{
std::shared_ptr<ASPECT> _a(new ASPECT(context,cfg,id));
aspect_ptr a = _a;
aspect_ptr a = std::make_shared<ASPECT>(context, cfg, id);
a->on_create();
return a;
}
Expand Down Expand Up @@ -544,8 +542,7 @@ class register_lua_aspect_factory : public lua_aspect_factory {

aspect_ptr get_new_instance( readonly_context &context, const config &cfg, const std::string &id, std::shared_ptr<lua_ai_context>& l_ctx)
{
std::shared_ptr<ASPECT> _a(new ASPECT(context,cfg,id,l_ctx));
aspect_ptr a = _a;
aspect_ptr a = std::make_shared<ASPECT>(context, cfg, id, l_ctx);
a->on_create();
return a;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ai/composite/engine.hpp
Expand Up @@ -165,7 +165,7 @@ class register_engine_factory : public engine_factory {
config cfg;
cfg["name"] = name;
cfg["engine"] = "cpp"; // @Crab: what is the purpose of this line(neph)
return engine_ptr(new ENGINE(ai,cfg));
return std::make_shared<ENGINE>(ai, cfg);
}
};

Expand Down
12 changes: 6 additions & 6 deletions src/ai/composite/property_handler.hpp
Expand Up @@ -290,24 +290,24 @@ template<typename X>
static inline void register_vector_property(property_handler_map& property_handlers, const std::string& property,
std::vector<std::shared_ptr<X>>& values, std::function<void(std::vector<std::shared_ptr<X>>&, const config&)> construction_factory)
{
property_handler_ptr handler_ptr(new vector_property_handler<X>(property, values, construction_factory));
property_handlers.emplace(property, handler_ptr);
property_handlers.emplace(property,
std::make_shared<vector_property_handler<X>>(property, values, construction_factory));
}

template<typename X>
static inline void register_facets_property(property_handler_map& property_handlers, const std::string& property,
std::vector<std::shared_ptr<X>>& values, std::shared_ptr<X>& def, std::function<void(std::vector<std::shared_ptr<X>>&, const config&)> construction_factory)
{
property_handler_ptr handler_ptr(new facets_property_handler<X>(property, values, def, construction_factory));
property_handlers.emplace(property, handler_ptr);
property_handlers.emplace(property,
std::make_shared<facets_property_handler<X>>(property, values, def, construction_factory));
}

template<typename X>
static inline void register_aspect_property(property_handler_map& property_handlers, const std::string& property,
std::map<std::string, std::shared_ptr<X>>& aspects, std::function<void(std::map<std::string, std::shared_ptr<X>>&, const config&, std::string)> construction_factory)
{
property_handler_ptr handler_ptr(new aspect_property_handler<X>(property, aspects, construction_factory));
property_handlers.emplace(property, handler_ptr);
property_handlers.emplace(property,
std::make_shared<aspect_property_handler<X>>(property, aspects, construction_factory));
}

} //of namespace ai
2 changes: 1 addition & 1 deletion src/ai/composite/rca.hpp
Expand Up @@ -181,7 +181,7 @@ class register_candidate_action_factory : public candidate_action_factory {
}

virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
return candidate_action_ptr(new CANDIDATE_ACTION(ai,cfg));
return std::make_shared<CANDIDATE_ACTION>(ai, cfg);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/ai/formula/engine_fai.cpp
Expand Up @@ -105,9 +105,9 @@ void engine_fai::do_parse_stage_from_config( ai_context &context, const config &
// st_ptr = stage_ptr(new stage_rca_formulas(context,cfg,formula_ai_));

if (name=="side_formulas") {
st_ptr = stage_ptr(new stage_side_formulas(context,cfg,*formula_ai_));
st_ptr = std::make_shared<stage_side_formulas>(context, cfg, *formula_ai_);
} else if (name=="unit_formulas") {
st_ptr = stage_ptr(new stage_unit_formulas(context,cfg,*formula_ai_));
st_ptr = std::make_shared<stage_unit_formulas>(context, cfg, *formula_ai_);
} else {
ERR_AI_ENGINE_FAI << "unknown type of formula_ai stage: ["<< name <<"]"<<std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ai/formula/function_table.cpp
Expand Up @@ -1092,7 +1092,7 @@ class ai_formula_function : public formula_function {
public:
ai_formula_function(const std::string& name, ai::formula_ai& ai) : formula_function(name), ai_(ai) {}
function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const {
return function_expression_ptr(new T(args, ai_));
return std::make_shared<T>(args, ai_);
}
};

Expand Down

0 comments on commit c80e506

Please sign in to comment.