cartobase 6.0.20
rcptr.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34
35#ifndef CARTOBASE_SMART_RCPTR_H
36#define CARTOBASE_SMART_RCPTR_H
37
39#include <memory>
40#include <iostream>
41#include <utility>
42
43// uncomment this line to get debugging information
44// #define DEBUG_REF
45
46// comment this line to disable thread-safe code using ZooLib atomic operations
47#define CARTO_THREADED_RCPTR
48
49#ifdef CARTO_THREADED_RCPTR
50#ifndef CARTO_NO_BOOST
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
57#endif
58#endif // BOOST_VERSION
59#endif // CARTO_NO_BOOST
60
61#ifndef CARTO_RCPTR_USE_BOOST
62// try using ZooLib-based implementation
64#endif
65
66#if !defined( CARTO_RCPTR_USE_BOOST ) && !defined( CARTO_RCPTR_USE_ZOOLIB )
67#undef CARTO_THREADED_RCPTR
68#endif
69
70#endif // CARTO_THREADED_RCPTR
71
72namespace carto {
73
76template <class T>
77class const_ref;
78template <class T>
79class ref;
80
81
84#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
90{
91 template <class T> friend class ref;
92 template <class T> friend class const_ref;
93
94 template <class T>
95 static void destroy( const_ref<T> * );
96 template <class T>
97 static void construct( const_ref<T> *, const T *, bool externalowner );
98protected:
99 /* if T has a release() method, there will be an ambiguity in
100 RefConstruction<T>::release, since RefConstruction inherits T and
101 DefaultRefConstruction. Then we will need to specialize
102 RefConstruction for these classes and overload release() there.
103 */
104 template <class T>
105 static const T *release( const_ref<T> * );
106};
107
108#endif // DOXYGEN_HIDE_INTERNAL_CLASSES
109
113#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
114: public virtual DefaultRefConstruction
115#endif // DOXYGEN_HIDE_INTERNAL_CLASSES
116{
117 template <class T> friend class ref;
118 template <class T> friend class const_ref;
119 template <class T> friend class shared_ptr;
120 friend class rc_ptr_trick;
121
122 template <class T>
123 static void destroy( const_ref<T> * );
124 template <class T>
125 static void construct( const_ref<T> *, const T *, bool externalowner );
126 template <class T>
127 static const T *release( const_ref<T> * );
128
129public:
130#ifdef CARTO_THREADED_RCPTR
131 typedef boost::detail::atomic_count RefCounterType;
132#else
133 typedef int RefCounterType;
134#endif
135
136private:
137 mutable RefCounterType _refCounter;
138
139public:
140
141 inline RCObject() : _refCounter( 0 ) {}
142
143 inline RCObject( const RCObject & )
144 : _refCounter( 0 ) {}
145
146 inline RCObject & operator = ( const RCObject & ) { return *this; }
147
148 inline virtual ~RCObject()
149 {
150 // set counter to 0 in case of forced deletion
151 long refCounter = _refCounter;
152#ifdef CARTO_DEBUG
153 if( refCounter > 0 )
154 std::cerr << "RCObject destructor called while " << refCounter
155 << " references are still alive on object " << this
156 << std::endl;
157#endif
158 if( refCounter > 0 )
159 for( long i=0; i<refCounter; ++i )
160 --_refCounter;
161 else
162 for( long i=0; i<-refCounter; ++i )
163 ++_refCounter;
164 }
165};
166
167
168class rc_ptr_trick;
169
170
173#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
182template <class T>
183class RefConstruction : T, virtual DefaultRefConstruction
184{
185 template <class R> friend class ref;
186 template <class R> friend class const_ref;
187
192 RefConstruction();
193};
194
202template <class T>
204{
205 template <class R> friend class const_ref;
206 template <class R> friend class ref;
208 friend class RCObject;
209 friend class rc_ptr_trick;
210
212 T *_ref;
213
214public:
215
216 inline int count() const
217 {
218 return ( _pcount ? *_pcount : 0 );
219 }
220};
221#endif // DOXYGEN_HIDE_INTERNAL_CLASSES
222
223
226
294template <class T>
296#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
297: public RefData<T>
298#endif // DOXYGEN_HIDE_INTERNAL_CLASSES
299{
300
301#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
303 friend class RCObject;
304#endif // DOXYGEN_HIDE_INTERNAL_CLASSES
305 template <class R> friend class ref;
306
307public:
308
310 inline const_ref() { this->_pcount = 0; this->_ref = 0; }
311
312 inline explicit const_ref( const T *pObject )
313 {
314 RefConstruction<T>::construct( this, pObject, false );
315 }
316
317 inline explicit const_ref( const T *pObject, bool externalowner )
318 {
319 RefConstruction<T>::construct( this, pObject, externalowner );
320 }
321
322 // Only unique_ptr with the default deleter can be converted, because
323 // const_ref does not support custom deleters (it calls the delete operator
324 // upon destruction, just like std::default_delete).
325 template< class U >
326 const_ref( std::unique_ptr<U>&& r )
327 {
328 RefConstruction<T>::construct( this, r.release(), false );
329 }
330
331 inline const_ref( const ref<T> &other ) {
332#ifdef DEBUG_REF
333 std::cout << "New ref from other ref (" << other._ref << ")" << std::endl;
334#endif
335 if( other._pcount && *other._pcount >= 0 )
336 ++(*other._pcount);
337 this->_pcount = other._pcount;
338 this->_ref = other._ref;
339 }
340
341 inline const_ref( const const_ref<T> &other ) {
342#ifdef DEBUG_REF
343 std::cout << "New ref from other ref (" << other._ref << ")" << std::endl;
344#endif
345 if( other._pcount && *other._pcount >= 0 )
346 ++(*other._pcount);
347 this->_pcount = other._pcount;
348 this->_ref = other._ref;
349 }
350
351 template <class R>
352 inline const_ref( const ref<R> &o ){
353 const RefData<R> &other = o;
354#ifdef DEBUG_REF
355 std::cout << "New ref from other ref with implicit cast (" << other._ref
356 << ")" << std::endl;
357#endif
358 if( other._pcount && *other._pcount >= 0 )
359 ++(*other._pcount);
360 this->_pcount = other._pcount;
361 this->_ref = other._ref;
362 }
363
364 template <class R>
365 inline const_ref( const const_ref<R> &other ){
366#ifdef DEBUG_REF
367 std::cout << "New ref from other ref with implicit cast (" << other._ref << ")" << std::endl;
368#endif
369 if( other._pcount && *other._pcount >= 0 )
370 ++(*other._pcount);
371 this->_pcount = other._pcount;
372 this->_ref = other._ref;
373 }
374
375 inline ~const_ref() { RefConstruction<T>::destroy( this ); }
376
377 // const_ref<T> &operator =( const T *pObject );
379 const_ref<T> &operator =( const const_ref<T> &other );
380
381 inline bool isNull() const { return this->_pcount == NULL; }
382 inline bool operator ==( const ref<T> &other ) const { return this->_ref == other._ref; }
383 inline bool operator ==( const T *pointer ) const { return this->_ref == pointer; }
384 inline bool operator ==( const const_ref<T> &other ) const { return this->_ref == other._ref; }
385 inline bool operator !=( const ref<T> &other ) const { return this->_ref != other._ref; }
386 inline bool operator !=( const const_ref<T> &other ) const { return this->_ref != other._ref; }
387 inline bool operator !=( const T *pointer ) const { return this->_ref != pointer; }
388
389 inline const T *operator ->() const {
390 return static_cast<const T *>( this->_ref );
391 }
392
393 inline const T &operator *() const {
394 return *static_cast<const T *>( this->_ref );
395 }
396
397 inline const T *pointer() const {
398 return static_cast<const T *>( this->_ref );
399 }
400
401 inline int refCount() const
402 {
403 return this->_pcount ? *this->_pcount : 0;
404 }
405};
406
407
409 /*
410template <class T>
411const_ref<T> &const_ref<T>::operator =( const T *pObject ) {
412#ifdef DEBUG_REF
413 std::cout << "ref (" << this->_ref << ") = " << pObject << std::endl;
414#endif
415 if ( this->_ref != pObject ) {
416 RefConstruction<T>::destroy( this );
417 RefConstruction<T>::construct( this, pObject, false );
418 }
419 return *this;
420}
421 */
422
424template <class T>
426#ifdef DEBUG_REF
427 std::cout << "ref (" << this->_ref << ") = ref (" << other._ref << ")"
428 << std::endl;
429#endif
430 if ( this->_ref != other._ref )
431 {
432 if( other._pcount && *other._pcount >= 0 )
433 ++(*other._pcount);
434 RefConstruction<T>::destroy( this );
435 this->_pcount = other._pcount;
436 this->_ref = other._ref;
437 }
438 return *this;
439}
440
442template <class T>
444#ifdef DEBUG_REF
445 std::cout << "ref (" << this->_ref << ") = ref (" << other._ref << ")"
446 << std::endl;
447#endif
448 if ( this != &other && this->_ref != other._ref )
449 {
450 if( other._pcount && *other._pcount >= 0 )
451 ++(*other._pcount);
452 RefConstruction<T>::destroy( this );
453 this->_pcount = other._pcount;
454 this->_ref = other._ref;
455 }
456 return *this;
457}
458
459
463template <class T>
464class ref : public const_ref<T>
465{
466 template <class R> friend class const_ref;
467
468public:
469
471
472 inline ref() : const_ref<T>() {
473#ifdef DEBUG_REF
474 std::cout << "New empty ref" << std::endl;
475#endif
476 }
477
478 inline explicit ref( T *pObject ) : const_ref<T>( pObject ) {}
479 inline explicit ref( T *pObject, bool externalowner )
480 : const_ref<T>( pObject, externalowner ) {}
481
482 template< class U >
483 ref( std::unique_ptr<U>&& r ) : const_ref<T>( std::move(r) ) {}
484
485 inline ref( const ref<T> &other ) : const_ref<T>( other ) {}
486
487 template <class R>
488 inline ref( const ref<R> &other ) : const_ref<T>( other ) {}
489
490 inline ~ref() {}
491
492 inline T *release() {
493 return const_cast<T *>( RefConstruction<T>::release( this ) );
494 }
495
496 inline ref<T> &operator =( const ref<T> &other ) {
497 this->const_ref<T>::operator =( other );
498 return *this;
499 }
500
501 inline T *operator ->() const {
502 return static_cast<T *>( this->_ref );
503 }
504
505 inline T &operator *() const {
506 return *static_cast<T *>( this->_ref );
507 }
508
509 inline T *pointer() const {
510 return static_cast<T *>( this->_ref );
511 }
512
513};
514
515
516
518#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
519template <class T>
520void DefaultRefConstruction::construct( const_ref<T> *r, const T *pObject,
521 bool externalowner )
522{
523#ifdef DEBUG_REF
524 std::cout << "New ref (" << pObject << ")" << std::endl;
525#endif
526 if ( pObject && !externalowner )
527 r->_pcount = new RCObject::RefCounterType( 1 );
528 else r->_pcount = NULL;
529 r->_ref = const_cast<T *>( pObject );
530}
531
533template <class T>
534void DefaultRefConstruction::destroy( const_ref<T> *r ) {
535#ifdef DEBUG_REF
536 std::cout << "Deleting ref (" << r->_ref << ")" << std::endl;
537#endif
538 if ( r->_pcount && --(*r->_pcount) == 0 )
539 {
540#ifdef DEBUG_REF
541 std::cout << "Deleting ref object (" << r->_ref << ")" << std::endl;
542#endif
543 delete r->_pcount;
544 delete static_cast<T *>( r->_ref );
545 }
546}
547
549template <class T>
551#ifdef DEBUG_REF
552 std::cout << "Releasing ref (" << r->_ref << ")" << std::endl;
553#endif
554 const T * const result = r->_ref;
555 if ( r->_pcount && --(*r->_pcount) == 0 )
556 {
557 delete r->_pcount;
558 r->_pcount = 0;
559 }
560 r->_ref = NULL;
561 return result;
562}
563#endif // DOXYGEN_HIDE_INTERNAL_CLASSES
564
567
568
569
570
572template <class T>
573void RCObject::construct( const_ref<T> *r, const T * pObject,
574 bool externalowner )
575{
576#ifdef DEBUG_REF
577 std::cout << "New RCObject (" << pObject << ")" << std::endl;
578#endif
579 if( pObject )
580 {
581 long x = pObject->_refCounter;
582 if( externalowner && x == 0 )
583 {
584 --pObject->_refCounter;
585 // r->_pcount = NULL;
586 }
587 else if( x >= 0 )
588 ++pObject->_refCounter;
589 r->_pcount = &pObject->_refCounter;
590 }
591 else
592 r->_pcount = NULL;
593 r->_ref = const_cast<T *>( pObject );
594}
595
597template <class T>
598void RCObject::destroy( const_ref<T> *r )
599{
600#ifdef DEBUG_REF
601 std::cout << "RCObject::destroy (" << r->_ref << ")" << std::endl;
602 if( r->_pcount )
603 std::cout << " count: " << *r->_pcount << std::endl;
604 else
605 std::cout << " no counter\n";
606#endif
607 if( r->_pcount && --(*r->_pcount) == 0 )
608 {
609#ifdef DEBUG_REF
610 std::cout << "Deleting RCObject object (" << r->_ref << ")" << std::endl;
611#endif
612 delete static_cast<T *>( r->_ref );
613 }
614}
615
617template <class T>
618const T *RCObject::release( const_ref<T> *r ) {
619#ifdef DEBUG_REF
620 std::cout << "Releasing RCObject (" << r->_ref << ")" << std::endl;
621#endif
622 const T * const result = r->_ref;
623 if ( r->_pcount && *r->_pcount >= 0 )
624 --(*r->_pcount);
625 r->_pcount = 0;
626 r->_ref = 0;
627 return result;
628}
629
630
631template <typename T>
632class rc_ptr : public ref<T>
633{
634public:
635#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
637 {
638 protected:
639 void operator delete( void* );
640 void operator delete[]( void* );
641 };
642#endif
643
644 inline rc_ptr() {}
645 inline explicit rc_ptr( T *p ) : ref<T>( p ) {}
646 inline explicit rc_ptr( T *p, bool externalowner )
647 : ref<T>( p, externalowner ) {}
648
649 template< class U >
650 rc_ptr( std::unique_ptr<U>&& r ) : ref<T>( std::move(r) ) {}
651
652 inline void reset( T *p = NULL )
653 { *static_cast< ref<T> * >( this ) = ref<T>( p ); }
654 inline T *get() const { return this->pointer(); }
655 inline bool operator <( const rc_ptr<T> &other ) const {
656 return this->pointer() < other.pointer();
657 }
658
659#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
660 inline operator insipid*() const
661 {
662 return reinterpret_cast< insipid* >( this->pointer() );
663 }
664#endif
665};
666
667
668
669template <>
670class RefConstruction< char > : DefaultRefConstruction
671{
672
673 private:
674
675 template < class R > friend class ref;
676 template < class R > friend class const_ref;
677
678 RefConstruction();
679
680
681};
682
683
684template <>
685class RefConstruction< unsigned char > : DefaultRefConstruction
686{
687
688 private:
689
690 template < class R > friend class ref;
691 template < class R > friend class const_ref;
692
693 RefConstruction();
694
695
696};
697
698
699template <>
700class RefConstruction< short > : DefaultRefConstruction
701{
702
703 private:
704
705 template < class R > friend class ref;
706 template < class R > friend class const_ref;
707
708 RefConstruction();
709
710
711};
712
713
714template <>
715class RefConstruction< unsigned short > : DefaultRefConstruction
716{
717
718 private:
719
720 template < class R > friend class ref;
721 template < class R > friend class const_ref;
722
723 RefConstruction();
724
725
726};
727
728
729template <>
730class RefConstruction< int > : DefaultRefConstruction
731{
732
733 private:
734
735 template < class R > friend class ref;
736 template < class R > friend class const_ref;
737
738 RefConstruction();
739
740
741};
742
743
744template <>
745class RefConstruction< unsigned int > : DefaultRefConstruction
746{
747
748 private:
749
750 template < class R > friend class ref;
751 template < class R > friend class const_ref;
752
753 RefConstruction();
754
755
756};
757
758
759template <>
760class RefConstruction< long > : DefaultRefConstruction
761{
762
763 private:
764
765 template < class R > friend class ref;
766 template < class R > friend class const_ref;
767
768 RefConstruction();
769
770
771};
772
773
774template <>
775class RefConstruction< unsigned long > : DefaultRefConstruction
776{
777
778 private:
779
780 template < class R > friend class ref;
781 template < class R > friend class const_ref;
782
783 RefConstruction();
784
785
786};
787
788
789template <>
790class RefConstruction< float > : DefaultRefConstruction
791{
792
793 private:
794
795 template < class R > friend class ref;
796 template < class R > friend class const_ref;
797
798 RefConstruction();
799
800
801};
802
803
804template <>
805class RefConstruction< double > : DefaultRefConstruction
806{
807
808 private:
809
810 template < class R > friend class ref;
811 template < class R > friend class const_ref;
812
813 RefConstruction();
814
815
816};
817
818
819} // namespace carto
820
821
822#endif
static const T * release(const_ref< T > *)
Definition rcptr.h:550
RCObject(const RCObject &)
Definition rcptr.h:143
RCObject & operator=(const RCObject &)
Definition rcptr.h:146
virtual ~RCObject()
Definition rcptr.h:148
friend class ref
Definition rcptr.h:117
friend class shared_ptr
Definition rcptr.h:119
int RefCounterType
Definition rcptr.h:133
friend class rc_ptr_trick
Definition rcptr.h:120
friend class const_ref
Definition rcptr.h:118
friend class ref
Definition rcptr.h:185
friend class const_ref
Definition rcptr.h:186
int count() const
Definition rcptr.h:216
friend class DefaultRefConstruction
Definition rcptr.h:207
friend class ref
Definition rcptr.h:206
friend class RCObject
Definition rcptr.h:208
friend class rc_ptr_trick
Definition rcptr.h:209
friend class const_ref
Definition rcptr.h:205
Constant Ref to an object of class T.
Definition rcptr.h:299
const_ref(const ref< T > &other)
Definition rcptr.h:331
const_ref(std::unique_ptr< U > &&r)
Definition rcptr.h:326
bool operator!=(const ref< T > &other) const
Definition rcptr.h:385
const T * pointer() const
Definition rcptr.h:397
const_ref(const const_ref< R > &other)
Definition rcptr.h:365
const_ref(const T *pObject)
Definition rcptr.h:312
friend class DefaultRefConstruction
Definition rcptr.h:302
const_ref(const ref< R > &o)
Definition rcptr.h:352
const_ref(const const_ref< T > &other)
Definition rcptr.h:341
bool operator==(const ref< T > &other) const
Definition rcptr.h:382
const_ref< T > & operator=(const ref< T > &other)
Definition rcptr.h:425
const T * operator->() const
Definition rcptr.h:389
friend class ref
Definition rcptr.h:305
friend class RCObject
Definition rcptr.h:303
bool isNull() const
Definition rcptr.h:381
const T & operator*() const
Definition rcptr.h:393
const_ref(const T *pObject, bool externalowner)
Definition rcptr.h:317
int refCount() const
Definition rcptr.h:401
bool operator<(const rc_ptr< T > &other) const
Definition rcptr.h:655
rc_ptr(T *p, bool externalowner)
Definition rcptr.h:646
void reset(T *p=NULL)
Definition rcptr.h:652
rc_ptr(T *p)
Definition rcptr.h:645
T * get() const
Definition rcptr.h:654
rc_ptr(std::unique_ptr< U > &&r)
Definition rcptr.h:650
ref(T *pObject)
Definition rcptr.h:478
ref< T > & operator=(const ref< T > &other)
Definition rcptr.h:496
ref(const ref< T > &other)
Definition rcptr.h:485
T * operator->() const
Definition rcptr.h:501
T referenced_type
Definition rcptr.h:470
T * pointer() const
Definition rcptr.h:509
ref(T *pObject, bool externalowner)
Definition rcptr.h:479
ref(const ref< R > &other)
Definition rcptr.h:488
T & operator*() const
Definition rcptr.h:505
ref(std::unique_ptr< U > &&r)
Definition rcptr.h:483
friend class const_ref
Definition rcptr.h:466
T * release()
Definition rcptr.h:492