Newer
Older
#ifndef _MEMORY_HPP__
#define _MEMORY_HPP__
namespace std {
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 > {
public:
cow_shared_ptr ( ) {
attach ( NULL );
}
cow_shared_ptr ( T * data ) {
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;
}
};
public:
cow_shared_ptr ( ) {
attach ( NULL );
}
cow_shared_ptr ( T * 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;
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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;
}
};
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;
}
346
347
348
349
350
351
352
353
354
355
356
357
358
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
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 >
std::unique_ptr < typename std::decay < T >::type > copy_unique ( const std::unique_ptr < T > & ptr ) {
return ptr ? std::make_unique < typename std::decay < T >::type > ( * ptr ) : nullptr;
}
} /* namespace std */
#endif // _MEMORY_HPP__