Newer
Older
/*
* memoru.hpp
*
* Created on: May 1, 2015
* Author: Jan Travnicek
*/
#ifndef __MEMORY_HPP_
#define __MEMORY_HPP_
class cow_shared_ptr;
class cow_shared_ptr_base {
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 > {
cow_shared_ptr ( cow_shared_ptr < T > && other ) noexcept {
m_Data = other.m_Data;
other.m_Data = NULL;
}
~cow_shared_ptr ( ) noexcept {
cow_shared_ptr < T > & operator =( const cow_shared_ptr < T > & other ) {
if ( this == & other ) return * this;
cow_shared_ptr < T > & operator =( cow_shared_ptr < T > && other ) noexcept {
swap ( * this, other );
return * this;
T & operator *( ) {
make_unique ( );
return * m_Data;
return m_Data;
}
bool unique ( ) const {
return m_Data == NULL || m_Data->m_UseCount == 1;
}
int getUseCount ( ) const {
if ( m_Data == NULL ) return 0;
return m_Data->m_UseCount;
}
explicit operator bool( ) const {
return getUseCount ( ) == 0;
}
private:
void attach ( T * data ) {
m_Data = data;
if ( m_Data && ( --( m_Data->m_UseCount ) <= 0 ) ) delete m_Data;
T * tmp = m_Data;
detach ( );
tmp = std::clone ( tmp );
T * m_Data;
friend void swap ( cow_shared_ptr < T > & first, cow_shared_ptr < T > & second ) {
T * tmp = first.m_Data;
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 ( ) {
delete m_Data;
}
attach ( data ? new cow_shared_ptr_data ( data ) : NULL );
cow_shared_ptr ( cow_shared_ptr < T > && other ) noexcept {
m_Data = other.m_Data;
other.m_Data = NULL;
}
~cow_shared_ptr ( ) noexcept {
cow_shared_ptr < T > & operator =( const cow_shared_ptr < T > & other ) {
if ( this == & other ) return * this;
cow_shared_ptr < T > & operator =( cow_shared_ptr < T > && other ) noexcept {
swap ( * this, other );
return * this;
T & operator *( ) {
make_unique ( );
return * ( m_Data->m_Data );
T const & operator *( ) const {
return * ( m_Data->m_Data );
return m_Data->m_Data;
}
bool unique ( ) const {
return m_Data == NULL || m_Data->m_UseCount == 1;
int getUseCount ( ) const {
if ( m_Data == NULL ) return 0;
return m_Data->m_UseCount;
}
explicit operator bool( ) const {
return getUseCount ( ) == 0;
}
void attach ( typename cow_shared_ptr < T >::cow_shared_ptr_data * data ) {
if ( m_Data && ( --( m_Data->m_UseCount ) <= 0 ) ) delete 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;
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
}
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;
}
return m_Data;
}
T & operator *( ) {
return * m_Data;
}
return * m_Data;
}
T * get ( ) {
return m_Data;
}
T * release ( ) {
T * res = m_Data;
m_Data = nullptr;
return res;
}
explicit operator bool( ) const {
return m_Data;
}
};
std::ostream & operator <<( std::ostream & out, const std::cow_shared_ptr < T > & ptr ) {
out << * ptr;
return out;
std::ostream & operator <<( std::ostream & out, const std::shared_ptr < T > & ptr ) {
out << * ptr;
return out;
}
template < class T >
std::ostream & operator <<( std::ostream & out, const std::unique_ptr < T > & ptr ) {
out << * ptr;
return out;
}
template < class T >
std::ostream & operator <<( std::ostream & out, const std::smart_ptr < T > & ptr ) {
out << * ptr;
return out;
}
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
template < class T >
struct compare < cow_shared_ptr < T > > {
int operator ()( const cow_shared_ptr < T > & first, const cow_shared_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 );
}
};
template < class T >
struct compare < shared_ptr < T > > {
int operator ()( const shared_ptr < T > & first, const shared_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 );
}
};
template < class T >
struct compare < unique_ptr < T > > {
int operator ()( const unique_ptr < T > & first, const unique_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 );
}
};
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
template < typename T, typename ... Args >
std::unique_ptr < T > make_unique ( Args && ... args ) {
return std::unique_ptr < T > ( new T ( std::forward < Args > ( args ) ... ) );
}
template < typename T, typename ... Args >
std::smart_ptr < T > make_smart ( Args && ... args ) {
return std::smart_ptr < T > ( new T ( std::forward < Args > ( args ) ... ) );