Cleaned up shared_ptr assignment

Made use of reset, make_shared, or direct assignment instead of creating new shared_ptrs.
This commit is contained in:
Charles Dang 2017-09-12 11:10:19 +11:00
parent 3eafff0d7d
commit 20eb42477c
10 changed files with 21 additions and 22 deletions

View file

@ -384,7 +384,7 @@ bool move_result::test_route(const unit &un)
pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(un, my_team, true);///@todo 1.9: see_all -> false
//do an A*-search
route_ = std::shared_ptr<pathfind::plain_route>( new pathfind::plain_route(pathfind::a_star_search(un.get_location(), to_, 10000.0, calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports)));
route_.reset(new pathfind::plain_route(pathfind::a_star_search(un.get_location(), to_, 10000.0, calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports)));
if (route_->steps.empty()) {
set_error(E_NO_ROUTE);
return false;

View file

@ -146,11 +146,11 @@ public:
}
if (!valid_variant_ && valid_ ) {
value_variant_ = std::shared_ptr<wfl::variant>(new wfl::variant(variant_value_translator<T>::value_to_variant(this->get())));
value_variant_.reset(new wfl::variant(variant_value_translator<T>::value_to_variant(this->get())));
valid_variant_ = true;
} else if (!valid_variant_ && valid_lua_) {
value_ = value_lua_->get();
value_variant_ = std::shared_ptr<wfl::variant>(new wfl::variant(variant_value_translator<T>::value_to_variant(this->get())));
value_variant_.reset(new wfl::variant(variant_value_translator<T>::value_to_variant(this->get())));
valid_variant_ = true; // @note: temporary workaround
} else {
assert(valid_variant_);
@ -171,7 +171,7 @@ public:
if (!valid_ ) {
if (valid_variant_) {
value_ = std::shared_ptr<T>(new T(variant_value_translator<T>::variant_to_value(get_variant())));
value_.reset(new T(variant_value_translator<T>::variant_to_value(get_variant())));
valid_ = true;
} else if (valid_lua_){
value_ = value_lua_->get();
@ -309,13 +309,13 @@ public:
///@todo 1.9 optimize in case of an aspect which returns variant
for(const auto& f : boost::adaptors::reverse(facets_)) {
if (f->active()) {
this->value_ = std::shared_ptr<T>(f->get_ptr());
this->value_ = f->get_ptr();
this->valid_ = true;
return;
}
}
if (default_) {
this->value_ = std::shared_ptr<T>(default_->get_ptr());
this->value_ = default_->get_ptr();
this->valid_ = true;
}
}
@ -442,7 +442,7 @@ public:
// error
return;
}
handler_ = std::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(), *l_ctx));
handler_.reset(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(), *l_ctx));
}
void recalculate() const

View file

@ -177,7 +177,7 @@ void target_location_goal::on_create()
}
const config &criteria = cfg_.child("criteria");
if (criteria) {
filter_ptr_ = std::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),resources::filter_con));
filter_ptr_.reset(new terrain_filter(vconfig(criteria),resources::filter_con));
}
}
@ -228,7 +228,7 @@ void protect_goal::on_create()
}
const config &criteria = cfg_.child("criteria");
if (criteria) {
filter_ptr_ = std::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),resources::filter_con));
filter_ptr_.reset(new terrain_filter(vconfig(criteria),resources::filter_con));
}
@ -320,13 +320,12 @@ lua_goal::lua_goal(readonly_context &context, const config &cfg)
void lua_goal::on_create(std::shared_ptr<ai::lua_ai_context> l_ctx)
{
handler_ = std::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(), *l_ctx));
handler_.reset(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(), *l_ctx));
}
void lua_goal::add_targets(std::back_insert_iterator< std::vector< target > > target_list)
{
std::shared_ptr< lua_object< std::vector < target > > > l_obj
= std::shared_ptr< lua_object< std::vector < target > > >(new lua_object< std::vector < target > >());
std::shared_ptr<lua_object<std::vector<target>>> l_obj = std::make_shared<lua_object<std::vector<target>>>();
config c(cfg_.child_or_empty("args"));
handler_->handle(c, true, l_obj);
try {

View file

@ -435,7 +435,7 @@ aspect_attacks_lua::aspect_attacks_lua(readonly_context &context, const config &
// error
return;
}
handler_ = std::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(), *l_ctx));
handler_.reset(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(), *l_ctx));
}
void aspect_attacks_lua::recalculate() const

View file

@ -97,7 +97,7 @@ void holder::init( side_number side )
default_ai_context_.reset(new default_ai_context_impl(*readwrite_context_,cfg_));
}
if (!this->ai_){
ai_ = std::shared_ptr<ai_composite>(new ai_composite(*default_ai_context_,cfg_));
ai_.reset(new ai_composite(*default_ai_context_,cfg_));
}
if (this->ai_) {

View file

@ -86,7 +86,7 @@ private:
std::shared_ptr<formula_debugger> fdbp;
bool need_wrapper = false;
if (fdb==nullptr) {
fdbp = std::shared_ptr<formula_debugger>(new formula_debugger());
fdbp.reset(new formula_debugger());
fdb = &*fdbp;
need_wrapper = true;

View file

@ -1554,7 +1554,7 @@ REPORT_GENERATOR(report_countdown, rc)
void reports::register_generator(const std::string &name, reports::generator *g)
{
dynamic_generators_[name] = std::shared_ptr<reports::generator>(g);
dynamic_generators_[name].reset(g);
}
config reports::generate_report(const std::string &name, reports::context & rc, bool only_static)

View file

@ -257,11 +257,11 @@ std::shared_ptr<randomness::rng> synced_context::get_rng_for_action()
const std::string& mode = resources::classification->random_mode;
if(mode == "deterministic")
{
return std::shared_ptr<randomness::rng>(new randomness::rng_deterministic(resources::gamedata->rng()));
return std::make_shared<randomness::rng_deterministic>(resources::gamedata->rng());
}
else
{
return std::shared_ptr<randomness::rng>(new randomness::synced_rng(generate_random_seed));
return std::make_shared<randomness::synced_rng>(generate_random_seed);
}
}

View file

@ -38,7 +38,7 @@ void name_generator_factory::add_name_generator_from_config(const config& config
if(config.has_attribute(cfg_name)) {
try {
name_generators_[id] = std::shared_ptr<name_generator>(new context_free_grammar_generator(config[cfg_name]));
name_generators_[id].reset(new context_free_grammar_generator(config[cfg_name]));
return;
}
catch (const name_generator_invalid_exception& ex) {
@ -50,7 +50,7 @@ void name_generator_factory::add_name_generator_from_config(const config& config
config::attribute_value markov_name_list = config[markov_name];
if(!markov_name_list.blank()) {
name_generators_[id] = std::shared_ptr<name_generator>(new markov_generator(utils::split(markov_name_list), config["markov_chain_size"].to_int(2), 12));
name_generators_[id].reset(new markov_generator(utils::split(markov_name_list), config["markov_chain_size"].to_int(2), 12));
}
}
}
@ -59,7 +59,7 @@ std::shared_ptr<name_generator> name_generator_factory::get_name_generator() {
std::map<std::string, std::shared_ptr<name_generator>>::const_iterator it = name_generators_.find("");
if(it == name_generators_.end()) {
//create a dummy instance, which always returns the empty string
return std::shared_ptr<name_generator>(new name_generator( ));
return std::make_shared<name_generator>();
}
return it->second;

View file

@ -1232,7 +1232,7 @@ future_map_if_active::~future_map_if_active()
real_map::real_map():
initial_planned_unit_map_(resources::whiteboard && resources::whiteboard->has_planned_unit_map()),
unit_map_lock_(resources::whiteboard ? resources::whiteboard->unit_map_lock_ : std::shared_ptr<bool>(new bool(false)))
unit_map_lock_(resources::whiteboard ? resources::whiteboard->unit_map_lock_ : std::make_shared<bool>(false))
{
if (!resources::whiteboard)
return;