Skip to content

Commit

Permalink
Unit: don't store advancements in a boost::ptr_vector
Browse files Browse the repository at this point in the history
I really have no idea why it was done this way, especially since it always seems to be allocating new config objects...?
Probably a relic of the pre-C++11 age.

Addresses one header brought up in #5359.
  • Loading branch information
Vultraz committed Dec 15, 2020
1 parent a4d1090 commit 3e2d0ae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
31 changes: 8 additions & 23 deletions src/units/unit.cpp
Expand Up @@ -54,7 +54,6 @@
#include "variable.hpp" // for vconfig, etc

#include <boost/dynamic_bitset.hpp>
#include <boost/function_output_iterator.hpp>

#ifdef _MSC_VER
#pragma warning (push)
Expand Down Expand Up @@ -245,19 +244,6 @@ static unit_race::GENDER generate_gender(const unit_type& u_type, const config&
return generate_gender(u_type, cfg["random_gender"].to_bool());
}

struct ptr_vector_pushback
{
ptr_vector_pushback(boost::ptr_vector<config>& vec) : vec_(&vec) {}

void operator()(const config& cfg)
{
vec_->push_back(new config(cfg));
}

//Don't use reference to be copyable.
boost::ptr_vector<config>* vec_;
};

// Copy constructor
unit::unit(const unit& o)
: std::enable_shared_from_this<unit>()
Expand Down Expand Up @@ -593,7 +579,9 @@ void unit::init(const config& cfg, bool use_traits, const vconfig* vcfg)
if(cfg.has_child("advancement")) {
set_attr_changed(UA_ADVANCEMENTS);
this->advancements_.clear();
boost::copy(cfg.child_range("advancement"), boost::make_function_output_iterator(ptr_vector_pushback(advancements_)));
for(const config& adv : cfg.child_range("advancement")) {
advancements_.push_back(adv);
}
}

// Don't use the unit_type's abilities if this config has its own defined
Expand Down Expand Up @@ -919,7 +907,7 @@ void unit::advance_to(const unit_type& u_type, bool use_traits)
advancements_.clear();

for(const config& advancement : new_type.advancements()) {
advancements_.push_back(new config(advancement));
advancements_.push_back(advancement);
}

// If unit has specific profile, remember it and keep it after advancing
Expand Down Expand Up @@ -1801,11 +1789,7 @@ std::vector<config> unit::get_modification_advances() const
void unit::set_advancements(std::vector<config> advancements)
{
set_attr_changed(UA_ADVANCEMENTS);
this->advancements_.clear();
for(config& advancement : advancements) {
this->advancements_.push_back(new config());
this->advancements_.back().swap(advancement);
}
advancements_ = advancements;
}

const std::string& unit::type_id() const
Expand Down Expand Up @@ -2174,8 +2158,9 @@ void unit::apply_builtin_effect(std::string apply_to, const config& effect)
advancements_.clear();
}

config temp = effect;
boost::copy(effect.child_range("advancement"), boost::make_function_output_iterator(ptr_vector_pushback(advancements_)));
for(const config& adv : effect.child_range("advancement")) {
advancements_.push_back(adv);
}
}
} else if(apply_to == "remove_advancement") {
const std::string& types = effect["types"];
Expand Down
6 changes: 2 additions & 4 deletions src/units/unit.hpp
Expand Up @@ -25,7 +25,6 @@

#include <bitset>
#include <boost/dynamic_bitset_fwd.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/variant.hpp>

class display;
Expand Down Expand Up @@ -310,9 +309,8 @@ class unit : public std::enable_shared_from_this<unit>
*/
std::vector<std::pair<std::string, std::string>> amla_icons() const;

using advancements_list= boost::ptr_vector<config>;
/** The raw, unparsed data for modification advancements. */
const advancements_list& modification_advancements() const
const std::vector<config>& modification_advancements() const
{
return advancements_;
}
Expand Down Expand Up @@ -1882,7 +1880,7 @@ class unit : public std::enable_shared_from_this<unit>
config modifications_;
config abilities_;

advancements_list advancements_;
std::vector<config> advancements_;

t_string description_;
std::vector<t_string> special_notes_;
Expand Down

0 comments on commit 3e2d0ae

Please sign in to comment.