diff --git a/alib2data/src/common/wrapper.hpp b/alib2data/src/common/wrapper.hpp index fc2b25d6da0456ac5964a574ca97844c1a1c952e..004155b8abcf46cdbd9a5a6e467c4675050456b8 100644 --- a/alib2data/src/common/wrapper.hpp +++ b/alib2data/src/common/wrapper.hpp @@ -18,6 +18,15 @@ class wrapper { protected: std::cow_shared_ptr < T > data; +private: + + void unify(wrapper& other) { + if(this->data.getUseCount() > other.data.getUseCount()) { + other.data = this->data; + } else { + this->data = other.data; + } + } public: explicit wrapper ( T * data ) : data ( data ) { } @@ -78,7 +87,9 @@ public: int compare ( const wrapper & other ) const { if ( this->data.get ( ) == other.data.get ( ) ) return 0; - return ( * this->data ).compare ( * other.data ); + int res = ( * this->data ).compare ( * other.data ); + if(res == 0) const_cast<wrapper*>(this)->unify(const_cast<wrapper&>(other)); + return res; } friend std::ostream & operator <<( std::ostream & os, const wrapper & instance ) { diff --git a/alib2std/src/extensions/memory.hpp b/alib2std/src/extensions/memory.hpp index efc71b46fa5206f3a86dbc2de6339436c3651a20..af0b732e458650df5d009ab6b670f7b7f8fcae73 100644 --- a/alib2std/src/extensions/memory.hpp +++ b/alib2std/src/extensions/memory.hpp @@ -86,6 +86,11 @@ public: return m_Data == NULL || m_Data->m_UseCount == 1; } + int getUseCount() const { + if(m_Data == NULL) return 0; + return m_Data->m_UseCount; + } + private: void attach ( T * data ) { m_Data = data;