Deployed unique_ptrs in a bunch more places

The changes from stack::push to stack::emplace in ai::handler are since the unique_ptrs make the copy
ctor malformed.
This commit is contained in:
Charles Dang 2017-09-04 03:18:21 +11:00
parent e52d532cae
commit 19bbfedb32
8 changed files with 28 additions and 55 deletions

View file

@ -65,7 +65,7 @@ void addons_client::connect()
utils::string_map i18n_symbols; utils::string_map i18n_symbols;
i18n_symbols["server_address"] = addr_; i18n_symbols["server_address"] = addr_;
conn_ = new network_asio::connection(host_, port_); conn_.reset(new network_asio::connection(host_, port_));
this->wait_for_transfer_done( this->wait_for_transfer_done(
vgettext("Connecting to $server_address|...", i18n_symbols)); vgettext("Connecting to $server_address|...", i18n_symbols));
@ -499,7 +499,7 @@ void addons_client::wait_for_transfer_done(const std::string& status_message, bo
else else
cd.reset(new read_addon_connection_data{ *conn_ }); cd.reset(new read_addon_connection_data{ *conn_ });
if(!stat_) { if(!stat_) {
stat_ = new network_transmission(*cd, _("Add-ons Manager"), status_message); stat_.reset(new network_transmission(*cd, _("Add-ons Manager"), status_message));
} else { } else {
stat_->set_subtitle(status_message); stat_->set_subtitle(status_message);
stat_->set_connection_data(*cd); stat_->set_connection_data(*cd);
@ -510,9 +510,3 @@ void addons_client::wait_for_transfer_done(const std::string& status_message, bo
throw user_exit(); throw user_exit();
} }
} }
addons_client::~addons_client()
{
delete stat_; // stat_ depends on conn_, so it must be destroyed first!
delete conn_;
}

View file

@ -52,8 +52,6 @@ public:
*/ */
addons_client(CVideo& v, const std::string& address); addons_client(CVideo& v, const std::string& address);
~addons_client();
/** /**
* Try to establish a connection to the add-ons server. * Try to establish a connection to the add-ons server.
*/ */
@ -122,8 +120,8 @@ private:
std::string addr_; std::string addr_;
std::string host_; std::string host_;
std::string port_; std::string port_;
network_asio::connection* conn_; std::unique_ptr<network_asio::connection> conn_;
gui2::dialogs::network_transmission* stat_; std::unique_ptr<gui2::dialogs::network_transmission> stat_;
std::string last_error_; std::string last_error_;
/** /**

View file

@ -116,7 +116,7 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,
} }
bool from_cache = false; bool from_cache = false;
battle_context *bc; std::unique_ptr<battle_context> bc;
// This cache is only about 99% correct, but speeds up evaluation by about 1000 times. // This cache is only about 99% correct, but speeds up evaluation by about 1000 times.
// We recalculate when we actually attack. // We recalculate when we actually attack.
@ -128,15 +128,13 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,
static_cast<int>(up->attacks().size())) { static_cast<int>(up->attacks().size())) {
from_cache = true; from_cache = true;
bc = new battle_context(usc->second.first, usc->second.second); bc.reset(new battle_context(usc->second.first, usc->second.second));
} else { } else {
bc = new battle_context(units, m->second, target, -1, -1, m_aggression, prev_def); bc.reset(new battle_context(units, m->second, target, -1, -1, m_aggression, prev_def));
} }
const combatant &att = bc->get_attacker_combatant(prev_def); const combatant &att = bc->get_attacker_combatant(prev_def);
const combatant &def = bc->get_defender_combatant(prev_def); const combatant &def = bc->get_defender_combatant(prev_def);
delete prev_bc;
prev_bc = bc;
prev_def = &bc->get_defender_combatant(prev_def); prev_def = &bc->get_defender_combatant(prev_def);
if ( !from_cache ) { if ( !from_cache ) {
@ -245,7 +243,6 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,
avg_damage_inflicted += defend_it->hitpoints() - prev_def->average_hp(map.gives_healing(defend_it->get_location())); avg_damage_inflicted += defend_it->hitpoints() - prev_def->average_hp(map.gives_healing(defend_it->get_location()));
} }
delete prev_bc;
terrain_quality /= resources_used; terrain_quality /= resources_used;
// Restore the units to their original positions. // Restore the units to their original positions.

View file

@ -82,19 +82,19 @@ holder::holder( side_number side, const config &cfg )
void holder::init( side_number side ) void holder::init( side_number side )
{ {
if (side_context_ == nullptr) { if (side_context_ == nullptr) {
side_context_ = new side_context_impl(side,cfg_); side_context_.reset(new side_context_impl(side,cfg_));
} else { } else {
side_context_->set_side(side); side_context_->set_side(side);
} }
if (readonly_context_ == nullptr){ if (readonly_context_ == nullptr){
readonly_context_ = new readonly_context_impl(*side_context_,cfg_); readonly_context_.reset(new readonly_context_impl(*side_context_,cfg_));
readonly_context_->on_readonly_context_create(); readonly_context_->on_readonly_context_create();
} }
if (readwrite_context_ == nullptr){ if (readwrite_context_ == nullptr){
readwrite_context_ = new readwrite_context_impl(*readonly_context_,cfg_); readwrite_context_.reset(new readwrite_context_impl(*readonly_context_,cfg_));
} }
if (default_ai_context_ == nullptr){ if (default_ai_context_ == nullptr){
default_ai_context_ = new default_ai_context_impl(*readwrite_context_,cfg_); default_ai_context_.reset(new default_ai_context_impl(*readwrite_context_,cfg_));
} }
if (!this->ai_){ if (!this->ai_){
ai_ = std::shared_ptr<ai_composite>(new ai_composite(*default_ai_context_,cfg_)); ai_ = std::shared_ptr<ai_composite>(new ai_composite(*default_ai_context_,cfg_));
@ -129,10 +129,6 @@ holder::~holder()
LOG_AI_MANAGER << describe_ai() << "Managed AI will be deleted" << std::endl; LOG_AI_MANAGER << describe_ai() << "Managed AI will be deleted" << std::endl;
} }
} catch (...) {} } catch (...) {}
delete this->default_ai_context_;
delete this->readwrite_context_;
delete this->readonly_context_;
delete this->side_context_;
} }
@ -308,7 +304,7 @@ component* holder::get_component(component *root, const std::string &path) {
manager::AI_map_of_stacks manager::ai_map_; manager::AI_map_of_stacks manager::ai_map_;
game_info *manager::ai_info_; std::unique_ptr<game_info> manager::ai_info_;
events::generic_event manager::user_interact_("ai_user_interact"); events::generic_event manager::user_interact_("ai_user_interact");
events::generic_event manager::sync_network_("ai_sync_network"); events::generic_event manager::sync_network_("ai_sync_network");
events::generic_event manager::tod_changed_("ai_tod_changed"); events::generic_event manager::tod_changed_("ai_tod_changed");
@ -325,14 +321,13 @@ void manager::set_ai_info(const game_info& i)
if (ai_info_!=nullptr){ if (ai_info_!=nullptr){
clear_ai_info(); clear_ai_info();
} }
ai_info_ = new game_info(i); ai_info_.reset(new game_info(i));
registry::init(); registry::init();
} }
void manager::clear_ai_info(){ void manager::clear_ai_info(){
delete ai_info_; ai_info_.reset(nullptr);
ai_info_ = nullptr;
} }
@ -640,9 +635,8 @@ bool manager::add_ai_for_side_from_config( side_number side, const config& cfg,
remove_ai_for_side(side); remove_ai_for_side(side);
} }
holder new_holder(side,parsed_cfg);
std::stack<holder>& ai_stack_for_specific_side = get_or_create_ai_stack_for_side(side); std::stack<holder>& ai_stack_for_specific_side = get_or_create_ai_stack_for_side(side);
ai_stack_for_specific_side.push(new_holder); ai_stack_for_specific_side.emplace(side, parsed_cfg);
return true; return true;
} }
@ -655,9 +649,8 @@ bool manager::add_ai_for_side( side_number side, const std::string& ai_algorithm
} }
config cfg; config cfg;
cfg["ai_algorithm"] = ai_algorithm_type; cfg["ai_algorithm"] = ai_algorithm_type;
holder new_holder(side,cfg);
std::stack<holder>& ai_stack_for_specific_side = get_or_create_ai_stack_for_side(side); std::stack<holder>& ai_stack_for_specific_side = get_or_create_ai_stack_for_side(side);
ai_stack_for_specific_side.push(new_holder); ai_stack_for_specific_side.emplace(side, cfg);
return true; return true;
} }
@ -806,8 +799,7 @@ holder& manager::get_active_ai_holder_for_side( side_number side )
return ai_stack_for_specific_side.top(); return ai_stack_for_specific_side.top();
} else { } else {
config cfg = configuration::get_default_ai_parameters(); config cfg = configuration::get_default_ai_parameters();
holder new_holder(side, cfg); ai_stack_for_specific_side.emplace(side, cfg);
ai_stack_for_specific_side.push(new_holder);
return ai_stack_for_specific_side.top(); return ai_stack_for_specific_side.top();
} }
} }

View file

@ -82,10 +82,10 @@ private:
composite_ai_ptr ai_; composite_ai_ptr ai_;
side_context *side_context_; std::unique_ptr<side_context> side_context_;
readonly_context *readonly_context_; std::unique_ptr<readonly_context> readonly_context_;
readwrite_context *readwrite_context_; std::unique_ptr<readwrite_context> readwrite_context_;
default_ai_context *default_ai_context_; std::unique_ptr<default_ai_context> default_ai_context_;
side_number side_; side_number side_;
config cfg_; config cfg_;
}; };
@ -464,7 +464,7 @@ private:
static AI_map_of_stacks ai_map_; static AI_map_of_stacks ai_map_;
static std::deque< command_history_item > history_; static std::deque< command_history_item > history_;
static long history_item_counter_; static long history_item_counter_;
static game_info *ai_info_; static std::unique_ptr<game_info> ai_info_;
static events::generic_event map_changed_; static events::generic_event map_changed_;
static events::generic_event recruit_list_changed_; static events::generic_event recruit_list_changed_;

View file

@ -81,7 +81,7 @@ namespace {
lua_State* L; lua_State* L;
scoped_dialog* prev; scoped_dialog* prev;
static scoped_dialog* current; static scoped_dialog* current;
gui2::window* window; std::unique_ptr<gui2::window> window;
typedef std::map<gui2::widget*, int> callback_map; typedef std::map<gui2::widget*, int> callback_map;
callback_map callbacks; callback_map callbacks;
@ -107,7 +107,6 @@ namespace {
scoped_dialog::~scoped_dialog() scoped_dialog::~scoped_dialog()
{ {
delete window;
current = prev; current = prev;
lua_pushstring(L, dlgclbkKey); lua_pushstring(L, dlgclbkKey);
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
@ -131,7 +130,7 @@ static gui2::widget* find_widget(lua_State* L, int i, bool readonly)
return nullptr; return nullptr;
} }
gui2::widget* w = scoped_dialog::current->window; gui2::widget* w = scoped_dialog::current->window.get();
for(; !lua_isnoneornil(L, i); ++i) for(; !lua_isnoneornil(L, i); ++i)
{ {
#ifdef GUI2_EXPERIMENTAL_LISTBOX #ifdef GUI2_EXPERIMENTAL_LISTBOX

View file

@ -117,7 +117,7 @@ bool terrain_filter::match_internal(const map_location& loc, const unit* ref_uni
if(cfg_.has_attribute("terrain")) { if(cfg_.has_attribute("terrain")) {
if(cache_.parsed_terrain == nullptr) { if(cache_.parsed_terrain == nullptr) {
cache_.parsed_terrain = new t_translation::ter_match(cfg_["terrain"]); cache_.parsed_terrain.reset(new t_translation::ter_match(cfg_["terrain"]));
} }
if(!cache_.parsed_terrain->is_empty) { if(!cache_.parsed_terrain->is_empty) {
const t_translation::terrain_code letter = fc_->get_disp_context().map().get_terrain_info(loc).number(); const t_translation::terrain_code letter = fc_->get_disp_context().map().get_terrain_info(loc).number();
@ -563,7 +563,7 @@ void terrain_filter::get_locs_impl(std::set<map_location>& locs, const unit* ref
//handle location filter //handle location filter
if(cfg_.has_child("filter_adjacent_location")) { if(cfg_.has_child("filter_adjacent_location")) {
if(cache_.adjacent_matches == nullptr) { if(cache_.adjacent_matches == nullptr) {
cache_.adjacent_matches = new std::vector<std::set<map_location> >(); cache_.adjacent_matches.reset(new std::vector<std::set<map_location>>());
} }
const vconfig::child_list& adj_cfgs = cfg_.get_children("filter_adjacent_location"); const vconfig::child_list& adj_cfgs = cfg_.get_children("filter_adjacent_location");
for (unsigned i = 0; i < adj_cfgs.size(); ++i) { for (unsigned i = 0; i < adj_cfgs.size(); ++i) {
@ -664,8 +664,3 @@ config terrain_filter::to_config() const
{ {
return cfg_.get_config(); return cfg_.get_config();
} }
terrain_filter::terrain_filter_cache::~terrain_filter_cache() {
delete parsed_terrain;
delete adjacent_matches;
}

View file

@ -84,13 +84,11 @@ private:
struct terrain_filter_cache { struct terrain_filter_cache {
terrain_filter_cache(); terrain_filter_cache();
~terrain_filter_cache();
//parsed_terrain: optimizes handling of terrain="..." //parsed_terrain: optimizes handling of terrain="..."
t_translation::ter_match *parsed_terrain; std::unique_ptr<t_translation::ter_match> parsed_terrain;
//adjacent_matches: optimize handling of [filter_adjacent_location] for get_locations() //adjacent_matches: optimize handling of [filter_adjacent_location] for get_locations()
std::vector< std::set<map_location> > *adjacent_matches; std::unique_ptr<std::vector<std::set<map_location>>> adjacent_matches;
//adjacent_match_cache: optimize handling of [filter_adjacent_location] for match() //adjacent_match_cache: optimize handling of [filter_adjacent_location] for match()
std::vector< std::pair<terrain_filter, std::map<map_location,bool> > > adjacent_match_cache; std::vector< std::pair<terrain_filter, std::map<map_location,bool> > > adjacent_match_cache;