diff --git a/alib2data/src/common/wrapper.hpp b/alib2data/src/common/wrapper.hpp index 004155b8abcf46cdbd9a5a6e467c4675050456b8..bc3be6c6db9c4a2f84d11c48710569d0241495e7 100644 --- a/alib2data/src/common/wrapper.hpp +++ b/alib2data/src/common/wrapper.hpp @@ -19,14 +19,13 @@ protected: std::cow_shared_ptr < T > data; private: - - void unify(wrapper& other) { - if(this->data.getUseCount() > other.data.getUseCount()) { + void unify ( wrapper & other ) { + if ( this->data.getUseCount ( ) > other.data.getUseCount ( ) ) other.data = this->data; - } else { + else this->data = other.data; - } } + public: explicit wrapper ( T * data ) : data ( data ) { } @@ -61,34 +60,36 @@ public: } bool operator >=( const wrapper & other ) const { - return this->compare(other) >= 0; + return this->compare ( other ) >= 0; } bool operator <=( const wrapper & other ) const { - return this->compare(other) <= 0; + return this->compare ( other ) <= 0; } bool operator >( const wrapper & other ) const { - return this->compare(other) > 0; + return this->compare ( other ) > 0; } bool operator <( const wrapper & other ) const { - return this->compare(other) < 0; + return this->compare ( other ) < 0; } bool operator !=( const wrapper & other ) const { - return this->compare(other) != 0; + return this->compare ( other ) != 0; } bool operator ==( const wrapper & other ) const { - return this->compare(other) == 0; + return this->compare ( other ) == 0; } int compare ( const wrapper & other ) const { if ( this->data.get ( ) == other.data.get ( ) ) return 0; int res = ( * this->data ).compare ( * other.data ); - if(res == 0) const_cast<wrapper*>(this)->unify(const_cast<wrapper&>(other)); + + if ( res == 0 ) const_cast < wrapper * > ( this )->unify ( const_cast < wrapper & > ( other ) ); + return res; } diff --git a/alib2std/src/extensions/memory.hpp b/alib2std/src/extensions/memory.hpp index af0b732e458650df5d009ab6b670f7b7f8fcae73..b9888b63f3e576060223c69cfa5bb871e2d33882 100644 --- a/alib2std/src/extensions/memory.hpp +++ b/alib2std/src/extensions/memory.hpp @@ -3,36 +3,38 @@ namespace std { -template <typename T, typename Enable = void> +template < typename T, typename Enable = void > class cow_shared_ptr; class cow_shared_ptr_base { public: - cow_shared_ptr_base() : m_UseCount(0) {} + cow_shared_ptr_base ( ) : m_UseCount ( 0 ) { } private: int m_UseCount; - template<typename T, typename Enable> friend class cow_shared_ptr; + template < typename T, typename Enable > + friend class cow_shared_ptr; }; -template<class T> -class cow_shared_ptr<T, typename std::enable_if< std::is_base_of<cow_shared_ptr_base, T>::value >::type> { +template < class T > +class cow_shared_ptr < T, typename std::enable_if < std::is_base_of < cow_shared_ptr_base, T >::value >::type > { public: cow_shared_ptr ( ) { attach ( NULL ); } cow_shared_ptr ( T * data ) { - if(data) data->m_UseCount = 0; + if ( data ) data->m_UseCount = 0; + attach ( data ); } - cow_shared_ptr ( const cow_shared_ptr<T> & other ) { + cow_shared_ptr ( const cow_shared_ptr < T > & other ) { attach ( other.m_Data ); } - cow_shared_ptr ( cow_shared_ptr<T> && other ) noexcept { + cow_shared_ptr ( cow_shared_ptr < T > && other ) noexcept { m_Data = other.m_Data; other.m_Data = NULL; } @@ -41,44 +43,44 @@ public: detach ( ); } - cow_shared_ptr<T> & operator = ( const cow_shared_ptr<T> & other ) { - if(this == &other) return *this; + cow_shared_ptr < T > & operator =( const cow_shared_ptr < T > & other ) { + if ( this == & other ) return * this; detach ( ); attach ( other.m_Data ); - return *this; + return * this; } - cow_shared_ptr<T> & operator = ( cow_shared_ptr<T> && other ) noexcept { - swap(*this, other); - return *this; + cow_shared_ptr < T > & operator =( cow_shared_ptr < T > && other ) noexcept { + swap ( * this, other ); + return * this; } - T * operator -> ( ) { - make_unique( ); + T * operator ->( ) { + make_unique ( ); return m_Data; } - T const * operator -> ( ) const { + T const * operator ->( ) const { return m_Data; } - T & operator * ( ) { - make_unique( ); - return *m_Data; + T & operator *( ) { + make_unique ( ); + return * m_Data; } - T const & operator * ( ) const { - return *m_Data; + T const & operator *( ) const { + return * m_Data; } - T * get () { - make_unique( ); + T * get ( ) { + make_unique ( ); return m_Data; } - T const * get () const { + T const * get ( ) const { return m_Data; } @@ -86,50 +88,56 @@ public: return m_Data == NULL || m_Data->m_UseCount == 1; } - int getUseCount() const { - if(m_Data == NULL) return 0; + int getUseCount ( ) const { + if ( m_Data == NULL ) return 0; + return m_Data->m_UseCount; } private: void attach ( T * data ) { m_Data = data; - if(m_Data) m_Data->m_UseCount++; + + if ( m_Data ) m_Data->m_UseCount++; } void detach ( ) { - if(m_Data && --(m_Data->m_UseCount) <= 0) delete m_Data; + if ( m_Data && ( --( m_Data->m_UseCount ) <= 0 ) ) delete m_Data; + m_Data = NULL; } - void make_unique( ) { - if(unique( )) return; + void make_unique ( ) { + if ( unique ( ) ) return; - T* tmp = m_Data; - detach( ); - tmp = std::clone(tmp); + T * tmp = m_Data; + detach ( ); + tmp = std::clone ( tmp ); tmp->m_UseCount = 0; - attach(tmp); + attach ( tmp ); } - T* m_Data; + T * m_Data; + + friend void swap ( cow_shared_ptr < T > & first, cow_shared_ptr < T > & second ) { + T * tmp = first.m_Data; - friend void swap ( cow_shared_ptr<T> & first, cow_shared_ptr<T> & second ) { - T* tmp = first.m_Data; - first.m_Data = second.m_Data; + first.m_Data = second.m_Data; second.m_Data = tmp; } + }; -template<class T> -class cow_shared_ptr<T, typename std::enable_if< ! std::is_base_of<cow_shared_ptr_base, T>::value >::type> { +template < class T > +class cow_shared_ptr < T, typename std::enable_if < !std::is_base_of < cow_shared_ptr_base, T >::value >::type > { struct cow_shared_ptr_data { T * m_Data; int m_UseCount; - cow_shared_ptr_data ( T * data ) : m_Data ( data ), m_UseCount( 0 ) { } + cow_shared_ptr_data ( T * data ) : m_Data ( data ), m_UseCount ( 0 ) { } ~cow_shared_ptr_data ( ) { delete m_Data; } + }; public: @@ -138,14 +146,14 @@ public: } cow_shared_ptr ( T * data ) { - attach ( new cow_shared_ptr_data( data ) ); + attach ( new cow_shared_ptr_data ( data ) ); } - cow_shared_ptr ( const cow_shared_ptr<T> & other ) { + cow_shared_ptr ( const cow_shared_ptr < T > & other ) { attach ( other.m_Data ); } - cow_shared_ptr ( cow_shared_ptr<T> && other ) noexcept { + cow_shared_ptr ( cow_shared_ptr < T > && other ) noexcept { m_Data = other.m_Data; other.m_Data = NULL; } @@ -154,44 +162,44 @@ public: detach ( ); } - cow_shared_ptr<T> & operator = ( const cow_shared_ptr<T> & other ) { - if(this == &other) return *this; + cow_shared_ptr < T > & operator =( const cow_shared_ptr < T > & other ) { + if ( this == & other ) return * this; detach ( ); attach ( other.m_Data ); - return *this; + return * this; } - cow_shared_ptr<T> & operator = ( cow_shared_ptr<T> && other ) noexcept { - swap(*this, other); - return *this; + cow_shared_ptr < T > & operator =( cow_shared_ptr < T > && other ) noexcept { + swap ( * this, other ); + return * this; } - T * operator -> ( ) { - make_unique( ); + T * operator ->( ) { + make_unique ( ); return m_Data->m_Data; } - T const * operator -> ( ) const { + T const * operator ->( ) const { return m_Data->m_Data; } - T & operator * ( ) { - make_unique( ); - return *(m_Data->m_Data); + T & operator *( ) { + make_unique ( ); + return * ( m_Data->m_Data ); } - T const & operator * ( ) const { - return *(m_Data->m_Data); + T const & operator *( ) const { + return * ( m_Data->m_Data ); } - T * get () { - make_unique( ); + T * get ( ) { + make_unique ( ); return m_Data->m_Data; } - T const * get () const { + T const * get ( ) const { return m_Data->m_Data; } @@ -200,40 +208,43 @@ public: } private: - void attach ( typename cow_shared_ptr<T>::cow_shared_ptr_data * data ) { + void attach ( typename cow_shared_ptr < T >::cow_shared_ptr_data * data ) { m_Data = data; - if(m_Data) m_Data->m_UseCount++; + + if ( m_Data ) m_Data->m_UseCount++; } void detach ( ) { - if(m_Data && --(m_Data->m_UseCount) <= 0) delete m_Data; + if ( m_Data && ( --( m_Data->m_UseCount ) <= 0 ) ) delete m_Data; + m_Data = NULL; } - void make_unique( ) { - if(unique( )) return; + void make_unique ( ) { + if ( unique ( ) ) return; - typename cow_shared_ptr<T>::cow_shared_ptr_data* tmp = m_Data; - detach( ); - attach(new cow_shared_ptr_data(std::clone(tmp->m_Data))); + typename cow_shared_ptr < T >::cow_shared_ptr_data * tmp = m_Data; + detach ( ); + attach ( new cow_shared_ptr_data ( std::clone ( tmp->m_Data ) ) ); } cow_shared_ptr_data * m_Data; - friend void swap ( cow_shared_ptr<T> & first, cow_shared_ptr<T> & second ) { - typename cow_shared_ptr<T>::cow_shared_ptr_data* tmp = first.m_Data; - first.m_Data = second.m_Data; + friend void swap ( cow_shared_ptr < T > & first, cow_shared_ptr < T > & second ) { + typename cow_shared_ptr < T >::cow_shared_ptr_data * tmp = first.m_Data; + first.m_Data = second.m_Data; second.m_Data = tmp; } + }; -template<class T> -const cow_shared_ptr<T>& make_const(cow_shared_ptr<T>& ptr) { - return static_cast<const cow_shared_ptr<T>&>(ptr); +template < class T > +const cow_shared_ptr < T > & make_const ( cow_shared_ptr < T > & ptr ) { + return static_cast < const cow_shared_ptr < T > & > ( ptr ); } -template<class T> -const cow_shared_ptr<T>& make_const(const cow_shared_ptr<T>& ptr) { +template < class T > +const cow_shared_ptr < T > & make_const ( const cow_shared_ptr < T > & ptr ) { return ptr; }