Skip to content
Snippets Groups Projects
Commit ca5241d9 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

union shared ptrs to single instance if possible

parent c67a1b72
No related branches found
No related tags found
No related merge requests found
......@@ -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 ) {
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment