35#ifndef CARTOBASE_SMART_RCPTR_H
36#define CARTOBASE_SMART_RCPTR_H
47#define CARTO_THREADED_RCPTR
49#ifdef CARTO_THREADED_RCPTR
51#include <boost/version.hpp>
52#if BOOST_VERSION >= 103300 && \
53 ( !defined( linux ) || ( defined( __GNUC__ ) && __GNUC__ >= 4 ) )
54#include <boost/detail/atomic_count.hpp>
55#ifndef BOOST_AC_USE_PTHREADS
56#define CARTO_RCPTR_USE_BOOST
61#ifndef CARTO_RCPTR_USE_BOOST
66#if !defined( CARTO_RCPTR_USE_BOOST ) && !defined( CARTO_RCPTR_USE_ZOOLIB )
67#undef CARTO_THREADED_RCPTR
84#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
91 template <
class T>
friend class ref;
97 static void construct(
const_ref<T> *,
const T *,
bool externalowner );
113#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
117 template <
class T>
friend class ref;
125 static void construct(
const_ref<T> *,
const T *,
bool externalowner );
130#ifdef CARTO_THREADED_RCPTR
144 : _refCounter( 0 ) {}
151 long refCounter = _refCounter;
154 std::cerr <<
"RCObject destructor called while " << refCounter
155 <<
" references are still alive on object " <<
this
159 for(
long i=0; i<refCounter; ++i )
162 for(
long i=0; i<-refCounter; ++i )
173#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
185 template <
class R>
friend class ref;
206 template <
class R>
friend class ref;
218 return ( _pcount ? *_pcount : 0 );
296#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
301#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
305 template <
class R>
friend class ref;
310 inline const_ref() { this->_pcount = 0; this->_ref = 0; }
314 RefConstruction<T>::construct(
this, pObject,
false );
317 inline explicit const_ref(
const T *pObject,
bool externalowner )
319 RefConstruction<T>::construct(
this, pObject, externalowner );
328 RefConstruction<T>::construct(
this, r.release(),
false );
333 std::cout <<
"New ref from other ref (" << other._ref <<
")" << std::endl;
335 if( other._pcount && *other._pcount >= 0 )
337 this->_pcount = other._pcount;
338 this->_ref = other._ref;
343 std::cout <<
"New ref from other ref (" << other._ref <<
")" << std::endl;
345 if( other._pcount && *other._pcount >= 0 )
347 this->_pcount = other._pcount;
348 this->_ref = other._ref;
355 std::cout <<
"New ref from other ref with implicit cast (" << other._ref
358 if( other._pcount && *other._pcount >= 0 )
360 this->_pcount = other._pcount;
361 this->_ref = other._ref;
367 std::cout <<
"New ref from other ref with implicit cast (" << other._ref <<
")" << std::endl;
369 if( other._pcount && *other._pcount >= 0 )
371 this->_pcount = other._pcount;
372 this->_ref = other._ref;
381 inline bool isNull()
const {
return this->_pcount == NULL; }
390 return static_cast<const T *
>( this->_ref );
394 return *
static_cast<const T *
>( this->_ref );
398 return static_cast<const T *
>( this->_ref );
403 return this->_pcount ? *this->_pcount : 0;
427 std::cout <<
"ref (" << this->_ref <<
") = ref (" << other._ref <<
")"
430 if ( this->_ref != other._ref )
432 if( other._pcount && *other._pcount >= 0 )
434 RefConstruction<T>::destroy(
this );
435 this->_pcount = other._pcount;
436 this->_ref = other._ref;
445 std::cout <<
"ref (" << this->_ref <<
") = ref (" << other._ref <<
")"
448 if (
this != &other && this->_ref != other._ref )
450 if( other._pcount && *other._pcount >= 0 )
452 RefConstruction<T>::destroy(
this );
453 this->_pcount = other._pcount;
454 this->_ref = other._ref;
474 std::cout <<
"New empty ref" << std::endl;
479 inline explicit ref( T *pObject,
bool externalowner )
480 :
const_ref<T>( pObject, externalowner ) {}
502 return static_cast<T *
>( this->_ref );
506 return *
static_cast<T *
>( this->_ref );
510 return static_cast<T *
>( this->_ref );
518#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
520void DefaultRefConstruction::construct( const_ref<T> *r,
const T *pObject,
524 std::cout <<
"New ref (" << pObject <<
")" << std::endl;
526 if ( pObject && !externalowner )
528 else r->_pcount = NULL;
529 r->_ref =
const_cast<T *
>( pObject );
534void DefaultRefConstruction::destroy(
const_ref<T> *r ) {
536 std::cout <<
"Deleting ref (" << r->_ref <<
")" << std::endl;
538 if ( r->_pcount && --(*r->_pcount) == 0 )
541 std::cout <<
"Deleting ref object (" << r->_ref <<
")" << std::endl;
544 delete static_cast<T *
>( r->_ref );
552 std::cout <<
"Releasing ref (" << r->_ref <<
")" << std::endl;
554 const T *
const result = r->_ref;
555 if ( r->_pcount && --(*r->_pcount) == 0 )
573void RCObject::construct(
const_ref<T> *r,
const T * pObject,
577 std::cout <<
"New RCObject (" << pObject <<
")" << std::endl;
581 long x = pObject->_refCounter;
582 if( externalowner && x == 0 )
584 --pObject->_refCounter;
588 ++pObject->_refCounter;
589 r->_pcount = &pObject->_refCounter;
593 r->_ref =
const_cast<T *
>( pObject );
601 std::cout <<
"RCObject::destroy (" << r->_ref <<
")" << std::endl;
603 std::cout <<
" count: " << *r->_pcount << std::endl;
605 std::cout <<
" no counter\n";
607 if( r->_pcount && --(*r->_pcount) == 0 )
610 std::cout <<
"Deleting RCObject object (" << r->_ref <<
")" << std::endl;
612 delete static_cast<T *
>( r->_ref );
620 std::cout <<
"Releasing RCObject (" << r->_ref <<
")" << std::endl;
622 const T *
const result = r->_ref;
623 if ( r->_pcount && *r->_pcount >= 0 )
635#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
639 void operator delete(
void* );
640 void operator delete[](
void* );
646 inline explicit rc_ptr( T *p,
bool externalowner )
647 :
ref<T>( p, externalowner ) {}
650 rc_ptr( std::unique_ptr<U>&& r ) :
ref<T>( std::move(r) ) {}
659#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
675 template <
class R >
friend class ref;
690 template <
class R >
friend class ref;
705 template <
class R >
friend class ref;
720 template <
class R >
friend class ref;
735 template <
class R >
friend class ref;
750 template <
class R >
friend class ref;
765 template <
class R >
friend class ref;
780 template <
class R >
friend class ref;
795 template <
class R >
friend class ref;
810 template <
class R >
friend class ref;
static const T * release(const_ref< T > *)
RCObject(const RCObject &)
RCObject & operator=(const RCObject &)
friend class rc_ptr_trick
friend class DefaultRefConstruction
friend class rc_ptr_trick
Constant Ref to an object of class T.
const_ref(const ref< T > &other)
const_ref(std::unique_ptr< U > &&r)
bool operator!=(const ref< T > &other) const
const T * pointer() const
const_ref(const const_ref< R > &other)
const_ref(const T *pObject)
friend class DefaultRefConstruction
const_ref(const ref< R > &o)
const_ref(const const_ref< T > &other)
bool operator==(const ref< T > &other) const
const_ref< T > & operator=(const ref< T > &other)
const T * operator->() const
const T & operator*() const
const_ref(const T *pObject, bool externalowner)
bool operator<(const rc_ptr< T > &other) const
rc_ptr(T *p, bool externalowner)
rc_ptr(std::unique_ptr< U > &&r)
ref< T > & operator=(const ref< T > &other)
ref(const ref< T > &other)
ref(T *pObject, bool externalowner)
ref(const ref< R > &other)
ref(std::unique_ptr< U > &&r)