convert attack_type to use shared_from_this instead of instrusive_ptrs

This commit is contained in:
Charles Dang 2017-04-12 06:28:23 +11:00 committed by Celtic Minstrel
parent 2a017b4862
commit 2ca105e146
4 changed files with 6 additions and 19 deletions

View file

@ -81,7 +81,7 @@ attack_type& luaW_checkweapon(lua_State* L, int idx)
}
template<typename T>
using attack_ptr_in = boost::intrusive_ptr<typename utils::const_clone<attack_type, typename std::remove_pointer<T>::type>::type>;
using attack_ptr_in = std::shared_ptr<typename utils::const_clone<attack_type, typename std::remove_pointer<T>::type>::type>;
// Note that these two templates are designed on the assumption that T is either unit or unit_type
template<typename T>
@ -139,7 +139,7 @@ static attack_itors::iterator get_attack_iter(unit& u, attack_ptr atk)
{
// This is slightly inefficient since it walks the attack list a second time...
return std::find_if(u.attacks().begin(), u.attacks().end(), [&atk](const attack_type& atk2) {
return &atk2 == atk;
return &atk2 == atk.get();
});
}

View file

@ -60,7 +60,6 @@ attack_type::attack_type(const config& cfg) :
parry_(cfg["parry"]),
specials_(cfg.child_or_empty("specials"))
{
assert(ref_count == 0);
if (description_.empty())
description_ = translation::egettext(id_.c_str());

View file

@ -30,7 +30,7 @@ class unit_ability_list;
//the 'attack type' is the type of attack, how many times it strikes,
//and how much damage it does.
class attack_type
class attack_type : public std::enable_shared_from_this<attack_type>
{
public:
@ -126,22 +126,10 @@ private:
int movement_used_;
int parry_;
config specials_;
mutable size_t ref_count = 0;
friend void intrusive_ptr_add_ref(const attack_type* atk) {
++atk->ref_count;
}
friend void intrusive_ptr_release(const attack_type* atk) {
assert(atk->ref_count < 1000000);
if(--atk->ref_count == 0) {
delete atk;
}
}
};
using attack_ptr = boost::intrusive_ptr<attack_type>;
using const_attack_ptr = boost::intrusive_ptr<const attack_type>;
using attack_ptr = std::shared_ptr<attack_type>;
using const_attack_ptr = std::shared_ptr<const attack_type>;
using attack_list = std::vector<attack_ptr>;
using attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::iterator>>;
using const_attack_itors = boost::iterator_range<boost::indirect_iterator<attack_list::const_iterator>>;

View file

@ -974,7 +974,7 @@ void unit::advance_to(const unit_type &u_type,
emit_zoc_ = new_type.has_zoc();
attacks_.clear();
std::transform(new_type.attacks().begin(), new_type.attacks().end(), std::back_inserter(attacks_), [](const attack_type& atk) {
return new attack_type(atk);
return std::make_shared<attack_type>(atk);
});
unit_value_ = new_type.cost();