diff --git a/alib2std/src/extensions/memory.hpp b/alib2std/src/extensions/memory.hpp index 9c29ee04a1466c4a8b71da0d435d39e5e0b866bc..1ee1dff21bdd62da278eacfc4a460a5edaadc44e 100644 --- a/alib2std/src/extensions/memory.hpp +++ b/alib2std/src/extensions/memory.hpp @@ -252,6 +252,73 @@ private: }; +template<class T> +class smart_ptr { + T * m_Data; +public: + + smart_ptr ( ) : m_Data ( NULL ) { + } + + smart_ptr ( T * data ) : m_Data ( data ){ + } + + smart_ptr ( const smart_ptr < T > & other ) : m_Data ( std::clone ( other.m_Data ) ) { + } + + smart_ptr ( smart_ptr < T > && other ) noexcept { + m_Data = other.m_Data; + other.m_Data = NULL; + } + + ~smart_ptr ( ) noexcept { + delete m_Data; + } + + smart_ptr < T > & operator =( const smart_ptr < T > & other ) { + if ( this == & other ) return * this; + + delete m_Data; + m_Data = std::clone ( other.m_Data ); + + return * this; + } + + smart_ptr < T > & operator =( smart_ptr < T > && other ) noexcept { + swap ( this->m_Data, other.m_Data ); + return * this; + } + + T * operator ->( ) { + return m_Data; + } + + T const * operator ->( ) const { + return m_Data; + } + + T & operator *( ) { + return * m_Data; + } + + T const & operator *( ) const { + return * m_Data; + } + + T * get ( ) { + return m_Data; + } + + T const * get ( ) const { + return m_Data; + } + + explicit operator bool( ) const { + return m_Data; + } + +}; + template < class T > std::ostream & operator <<( std::ostream & out, const std::cow_shared_ptr < T > & ptr ) { out << * ptr; @@ -270,6 +337,12 @@ std::ostream & operator <<( std::ostream & out, const std::unique_ptr < T > & pt return out; } +template < class T > +std::ostream & operator <<( std::ostream & out, const std::smart_ptr < T > & ptr ) { + out << * ptr; + return out; +} + template < class T > struct compare < cow_shared_ptr < T > > { int operator ()( const cow_shared_ptr < T > & first, const cow_shared_ptr < T > & second ) const { @@ -313,6 +386,21 @@ struct compare < unique_ptr < T > > { return comp ( * first, * second ); } +}; + +template < class T > +struct compare < smart_ptr < T > > { + int operator ()( const smart_ptr < T > & first, const smart_ptr < T > & second ) const { + if ( first.get ( ) == second.get ( ) ) return 0; + + if ( !first ) return -1; + + if ( !second ) return 1; + + compare < typename std::decay < T >::type > comp; + return comp ( * first, * second ); + } + }; // TODO remove after switching to c++14