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

add simple shared_ptr

parent c21d3221
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
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