mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-17 22:03:24 +00:00
With shared pointers, we no longer need a deep copy.
Technically, this is a behavior change, since copies and assignments will now share data, causing changes in one copy to affect the other. However, the logic is that only one wmi_container is active at a time; the copies and assignments are used to transfer data between scenarios and to/from saves. So shallow copies are permissable (and slightly reduce memory consumption).
This commit is contained in:
parent
7341d0190b
commit
7b0722f819
@ -46,12 +46,6 @@ wmi_container::wmi_container()
|
|||||||
: wml_menu_items_()
|
: wml_menu_items_()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
wmi_container::wmi_container(const wmi_container& container)
|
|
||||||
: wml_menu_items_()
|
|
||||||
{
|
|
||||||
copy(container.wml_menu_items_);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destructor.
|
* Destructor.
|
||||||
* Default implementation, but defined here because this function needs to be
|
* Default implementation, but defined here because this function needs to be
|
||||||
@ -62,25 +56,6 @@ wmi_container::~wmi_container()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs a deep copy, replacing our current contents.
|
|
||||||
* Used by assignment and the copy constructor.
|
|
||||||
*/
|
|
||||||
void wmi_container::copy(const map_t & source)
|
|
||||||
{
|
|
||||||
// Safety measure.
|
|
||||||
if ( &source == &wml_menu_items_ )
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Free up the old memory.
|
|
||||||
wml_menu_items_.clear();
|
|
||||||
|
|
||||||
const map_t::const_iterator source_end = source.end();
|
|
||||||
for ( map_t::const_iterator itor = source.begin(); itor != source_end; ++itor )
|
|
||||||
// Deep copy.
|
|
||||||
wml_menu_items_[itor->first].reset(new wml_menu_item(*(itor->second)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Erases the item with id @a key. */
|
/** Erases the item with id @a key. */
|
||||||
wmi_container::size_type wmi_container::erase(const std::string & id)
|
wmi_container::size_type wmi_container::erase(const std::string & id)
|
||||||
{
|
{
|
||||||
|
@ -65,13 +65,8 @@ public:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
wmi_container();
|
wmi_container();
|
||||||
wmi_container(const wmi_container& container);
|
|
||||||
~wmi_container();
|
~wmi_container();
|
||||||
|
|
||||||
/// Assignment operator to support deep copies.
|
|
||||||
wmi_container & operator=(const wmi_container & that)
|
|
||||||
{ copy(that.wml_menu_items_); return *this; }
|
|
||||||
|
|
||||||
/// Returns true if *this contains no data.
|
/// Returns true if *this contains no data.
|
||||||
bool empty() const { return wml_menu_items_.empty(); }
|
bool empty() const { return wml_menu_items_.empty(); }
|
||||||
/// Erases the item with the provided @a id.
|
/// Erases the item with the provided @a id.
|
||||||
@ -108,10 +103,6 @@ public:
|
|||||||
const_iterator begin() const { return const_iterator(wml_menu_items_.begin()); }
|
const_iterator begin() const { return const_iterator(wml_menu_items_.begin()); }
|
||||||
const_iterator end() const { return const_iterator(wml_menu_items_.end()); }
|
const_iterator end() const { return const_iterator(wml_menu_items_.end()); }
|
||||||
|
|
||||||
private:
|
|
||||||
/// Performs a deep copy, replacing our current contents.
|
|
||||||
void copy(const map_t & source);
|
|
||||||
|
|
||||||
private: // data
|
private: // data
|
||||||
map_t wml_menu_items_;
|
map_t wml_menu_items_;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user