From ca5241d9d43ea6fef2c65fb2cabdfffad577915c Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 6 Oct 2015 17:04:55 +0200 Subject: [PATCH] union shared ptrs to single instance if possible --- alib2data/src/common/wrapper.hpp | 13 ++++++++++++- alib2std/src/extensions/memory.hpp | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/alib2data/src/common/wrapper.hpp b/alib2data/src/common/wrapper.hpp index fc2b25d6da..004155b8ab 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 efc71b46fa..af0b732e45 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; -- GitLab