File failed to load: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/config/TeX-MML-AM_CHTML/MathJax.js
DGtal 2.0.0
DGtal::CountedPtrOrPtr< T > Class Template Reference

Aim: Smart or simple pointer on T. It can be a smart pointer based on reference counts or a simple pointer on T depending either on a boolean value given at construction or on the constructor used. In the first case, we will call this pointer object smart, otherwise we will call it simple. More...

#include <DGtal/base/CountedPtrOrPtr.h>

Inheritance diagram for DGtal::CountedPtrOrPtr< T >:
[legend]

Public Types

typedef CountedPtr< T >::Counter Counter
 The counter is the same as CountedPtr.

Public Member Functions

 CountedPtrOrPtr (T *p=0, bool isCountedPtr=true)
 ~CountedPtrOrPtr ()
 CountedPtrOrPtr (const CountedPtr< T > &r) noexcept
 CountedPtrOrPtr (const CountedPtrOrPtr &r) noexcept
CountedPtrOrPtroperator= (const CountedPtrOrPtr &r)
CountedPtrOrPtroperator= (const CountedPtr< T > &r)
bool isSmart () const
bool isSimple () const
bool operator== (const T *other) const
bool operator!= (const T *other) const
T & operator* () const noexcept
T * operator-> () const noexcept
T * get () const noexcept
bool unique () const noexcept
unsigned int count () const
T * drop ()
void selfDisplay (std::ostream &out) const
bool isValid () const

Private Member Functions

CountercounterPtr () const
T * ptr () const
void acquire (Counter *c) noexcept
void release ()

Private Attributes

void * myAny
bool myIsCountedPtr
 If true, 'this' pointer object is smart, otherwise it is simple.

Friends

class CountedConstPtrOrConstPtr< T >

Detailed Description

template<typename T>
class DGtal::CountedPtrOrPtr< T >

Aim: Smart or simple pointer on T. It can be a smart pointer based on reference counts or a simple pointer on T depending either on a boolean value given at construction or on the constructor used. In the first case, we will call this pointer object smart, otherwise we will call it simple.

Description of template class 'CountedPtrOrPtr'

This object is useful when instantiating from an Alias<T> object, letting the user specify if it uses smart pointers or simply pointers. This class should be used as a meta-type for data members, when the programmer wants to hold a reference to some object during some period, but also wants to let the user decides whether the class should keep a smart reference or non-smart reference to the object. How and where to use such smart pointers is explained in User passing an argument to an Alias parameter.

struct B {};
struct A {
A( Alias<B> someB ) : myPtrB( B ) {}
};
int main() {
B b1;
A a1( b1 ); // a1.myPtrB points to b1, classic pointer
B* b2 = new B;
A a2( b2 ); // a2.myPtrB acquires b2 (and will take care of freeing it)
CountedPtr<B> b3 = CountedPtr<B>( new B ); // smart pointer
A a3( b3 ); // a3.myPtrB smart points to b3.
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition Alias.h:183
Aim: Smart or simple pointer on T. It can be a smart pointer based on reference counts or a simple po...
int main()
Definition testBits.cpp:56
Template Parameters
Tany data type.
See also
CountedPtr
Alias

Definition at line 95 of file CountedPtrOrPtr.h.

Member Typedef Documentation

◆ Counter

template<typename T>
typedef CountedPtr<T>::Counter DGtal::CountedPtrOrPtr< T >::Counter

The counter is the same as CountedPtr.

Definition at line 104 of file CountedPtrOrPtr.h.

Constructor & Destructor Documentation

◆ CountedPtrOrPtr() [1/3]

template<typename T>
DGtal::CountedPtrOrPtr< T >::CountedPtrOrPtr ( T * p = 0,
bool isCountedPtr = true )
inlineexplicit

Default Constructor and constructor from pointer. The created object is either a simple pointer on p (not acquired) if isCountedPtr is false, or a smart pointer based on reference counts (CountedPtr).

Parameters
pis a pointer to some object T. If isCountedPtr is true, then pointer p should point to some dynamically allocated object T, and the pointer is acquired. If isCountedPtr is false, then this object holds only the pointer p, without acquiring it.
isCountedPtrwhen 'true', stores p as a smart (counted) pointer, otherwise stores p directly.

Definition at line 121 of file CountedPtrOrPtr.h.

123 {
124 if ( isCountedPtr ) {
125 if (p) myAny = static_cast<void*>( new Counter( p ) );
126 }
127 else
128 myAny = static_cast<void*>( p );
129 }
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
CountedPtr< T >::Counter Counter
The counter is the same as CountedPtr.

References myAny, and myIsCountedPtr.

Referenced by CountedConstPtrOrConstPtr< T >, CountedPtrOrPtr(), operator=(), and operator=().

◆ ~CountedPtrOrPtr()

template<typename T>
DGtal::CountedPtrOrPtr< T >::~CountedPtrOrPtr ( )
inline

Destructor. If this pointer object was smart, the pointed object is released (and possibly freed if the reference count was 1), otherwise, if this pointer object was simple, the destructor does nothing.

Definition at line 137 of file CountedPtrOrPtr.h.

138 {
139 if ( myIsCountedPtr ) release();
140 }

References myIsCountedPtr, and release().

◆ CountedPtrOrPtr() [2/3]

template<typename T>
DGtal::CountedPtrOrPtr< T >::CountedPtrOrPtr ( const CountedPtr< T > & r)
inlinenoexcept

Constructor from smart pointer (CountedPtr) r. In this case, this pointer object is smart and acquire the given smart pointer.

Parameters
rthe smart pointer to acquire.

Definition at line 149 of file CountedPtrOrPtr.h.

150 : myIsCountedPtr( true )
151 {
152 acquire( r.myCounter );
153 }
void acquire(Counter *c) noexcept

References acquire().

◆ CountedPtrOrPtr() [3/3]

template<typename T>
DGtal::CountedPtrOrPtr< T >::CountedPtrOrPtr ( const CountedPtrOrPtr< T > & r)
inlinenoexcept

Copy constructor. If r is smart, then this pointer object is also smart and acquires r (no duplication). Otherwise, if r is simple, then this pointer object only points at the same place.

Parameters
rthe other pointer to clone, which may be smart or simple.

Definition at line 164 of file CountedPtrOrPtr.h.

166 {
167 if ( myIsCountedPtr )
168 acquire( r.counterPtr() );
169 else
170 myAny = r.myAny;
171 }
Counter * counterPtr() const

References acquire(), CountedPtrOrPtr(), myAny, and myIsCountedPtr.

Member Function Documentation

◆ acquire()

template<typename T>
void DGtal::CountedPtrOrPtr< T >::acquire ( Counter * c)
inlineprivatenoexcept

Tells this smart pointer that it should reference the counter c. If c is not null, the number of reference counts is incremented.

Parameters
cany counter (except this.myCounter).
Precondition
'this' pointer object is smart.

Definition at line 382 of file CountedPtrOrPtr.h.

383 { // increment the count
384 // Travis is too slow in Debug mode with this ASSERT.
386 myAny = static_cast<void*>( c );
387 if (c) ++c->count;
388 }
unsigned int count() const

References myAny, and myIsCountedPtr.

Referenced by CountedPtrOrPtr(), CountedPtrOrPtr(), operator=(), and operator=().

◆ count()

template<typename T>
unsigned int DGtal::CountedPtrOrPtr< T >::count ( ) const
inline
Note
For debug.
Returns
if 'this' object is smart, returns the number of smart pointers pointing to the same object as 'this', or 0 if 'this' object is simple.

Definition at line 314 of file CountedPtrOrPtr.h.

315 {
316 return myIsCountedPtr ? counterPtr()->count : 0;
317 }

References DGtal::CountedPtr< T >::Counter::count, counterPtr(), and myIsCountedPtr.

Referenced by testCountedConstPtrOrConstPtrMemory(), and testCountedPtrOrPtrMemory().

◆ counterPtr()

template<typename T>
Counter * DGtal::CountedPtrOrPtr< T >::counterPtr ( ) const
inlineprivate
Precondition
'this' pointer object is smart.
Returns
the (possibly shared) counter pointed by myAny.

Definition at line 356 of file CountedPtrOrPtr.h.

357 {
358 // Travis is too slow in Debug mode with this ASSERT.
360 return static_cast<Counter*>( myAny );
361 }

References myAny, and myIsCountedPtr.

Referenced by count(), drop(), get(), operator!=(), operator*(), operator->(), DGtal::CountedConstPtrOrConstPtr< T >::operator=(), operator=(), operator==(), release(), and unique().

◆ drop()

template<typename T>
T * DGtal::CountedPtrOrPtr< T >::drop ( )
inline

Gives back the pointer without deleting him. Deletes only the Counter if 'this' was smart.

Returns
the address that was {smartly} or {simply} pointed by 'this' pointer.
Note
Use with care.
Precondition
'isValid()' and, if smart, 'unique()'.

Definition at line 329 of file CountedPtrOrPtr.h.

330 { // Gives back the pointer without deleting him. Delete only the counter.
331 ASSERT( isValid() );
332 if ( myIsCountedPtr ) {
333 ASSERT( unique() );
334 T* tmp = counterPtr()->ptr;
335 delete counterPtr();
336 myAny = 0;
337 return tmp;
338 } else {
339 return ptr();
340 }
341 }
bool unique() const noexcept

References counterPtr(), isValid(), myAny, myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, ptr(), and unique().

◆ get()

template<typename T>
T * DGtal::CountedPtrOrPtr< T >::get ( ) const
inlinenoexcept

Secured member access operator.

Returns
a pointer on the object of type T that is smartly or simply pointed by 'this' or 0 if the object is not valid ('isValid()' is false).

Definition at line 290 of file CountedPtrOrPtr.h.

291 {
292 return myIsCountedPtr ? ( myAny ? counterPtr()->ptr : 0 ) : ptr();
293 }

References counterPtr(), myAny, myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and ptr().

Referenced by testCountedConstPtrOrConstPtrMemory(), and testCountedPtrOrPtrMemory().

◆ isSimple()

template<typename T>
bool DGtal::CountedPtrOrPtr< T >::isSimple ( ) const
inline
Returns
'true' iff 'this' pointer object is simple.

Definition at line 225 of file CountedPtrOrPtr.h.

226 {
227 return ! myIsCountedPtr;
228 }

References myIsCountedPtr.

Referenced by testCountedPtrOrPtrMemory().

◆ isSmart()

template<typename T>
bool DGtal::CountedPtrOrPtr< T >::isSmart ( ) const
inline
Returns
'true' iff 'this' pointer object is smart.

Definition at line 217 of file CountedPtrOrPtr.h.

218 {
219 return myIsCountedPtr;
220 }

References myIsCountedPtr.

Referenced by testCountedPtrOrPtrMemory().

◆ isValid()

template<typename T>
bool DGtal::CountedPtrOrPtr< T >::isValid ( ) const

Checks the validity/consistency of the object.

Returns
'true' if the object is valid, 'false' otherwise.

Referenced by drop(), operator*(), and operator->().

◆ operator!=()

template<typename T>
bool DGtal::CountedPtrOrPtr< T >::operator!= ( const T * other) const
inline

Inequality operator !=

Parameters
otherany other pointer.
Returns
'true' if 'this' points to a different address than other.

Definition at line 247 of file CountedPtrOrPtr.h.

248 {
249 return myIsCountedPtr ? ( myAny ? counterPtr()->ptr : 0 ) != other : ptr() != other;
250 }

References counterPtr(), myAny, myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and ptr().

◆ operator*()

template<typename T>
T & DGtal::CountedPtrOrPtr< T >::operator* ( ) const
inlinenoexcept

Dereferencing operator.

Returns
a reference on the object of type T that is smartly or simply pointed by 'this'.
Precondition
'isValid()' is true

Definition at line 260 of file CountedPtrOrPtr.h.

261 {
262 // Travis is too slow in Debug mode with this ASSERT.
263 ASSERT( isValid() );
264 return myIsCountedPtr ? ( * counterPtr()->ptr ) : ( * ptr() );
265 }

References counterPtr(), isValid(), myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and ptr().

◆ operator->()

template<typename T>
T * DGtal::CountedPtrOrPtr< T >::operator-> ( ) const
inlinenoexcept

Member access operator.

Returns
a pointer on the object of type T that is smartly or simply pointed by 'this' or 0 if the object is not valid ('isValid()' is false).
Precondition
'isValid()' is true

Definition at line 276 of file CountedPtrOrPtr.h.

277 {
278 // Travis is too slow in Debug mode with this ASSERT.
279 ASSERT( isValid() );
280 return myIsCountedPtr ? counterPtr()->ptr : ptr();
281 }

References counterPtr(), isValid(), myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and ptr().

◆ operator=() [1/2]

template<typename T>
CountedPtrOrPtr & DGtal::CountedPtrOrPtr< T >::operator= ( const CountedPtr< T > & r)
inline

Assignment with smart pointer (CountedPtr). If 'this' was smart, then the shared pointer is released. Then this pointer object becomes also smart and acquires r (no duplication).

Parameters
rthe other smart pointer to clone.
Returns
a reference to 'this'.

Definition at line 206 of file CountedPtrOrPtr.h.

207 {
208 if ( myIsCountedPtr ) release();
209 myIsCountedPtr = true;
210 acquire( r.myCounter );
211 return *this;
212 }

References acquire(), CountedPtrOrPtr(), DGtal::CountedPtr< T >::myCounter, myIsCountedPtr, and release().

◆ operator=() [2/2]

template<typename T>
CountedPtrOrPtr & DGtal::CountedPtrOrPtr< T >::operator= ( const CountedPtrOrPtr< T > & r)
inline

Assignment. If 'this' was smart, then the shared pointer is released. Then, if r is smart, then this pointer object is also smart and acquires r (no duplication). Otherwise, if r is simple, then this pointer object only points at the same place.

Parameters
rthe other pointer to clone, which may be smart or simple.
Returns
a reference to 'this'.

Definition at line 185 of file CountedPtrOrPtr.h.

186 {
187 if ( this != & r ) {
188 if ( myIsCountedPtr ) release();
190 if ( r.myIsCountedPtr ) acquire( r.counterPtr() );
191 else myAny = r.myAny;
192 }
193 return *this;
194 }

References acquire(), CountedPtrOrPtr(), counterPtr(), myAny, myIsCountedPtr, and release().

◆ operator==()

template<typename T>
bool DGtal::CountedPtrOrPtr< T >::operator== ( const T * other) const
inline

Equality operator ==

Parameters
otherany other pointer.
Returns
'true' if pointed address is equal to other.

Definition at line 236 of file CountedPtrOrPtr.h.

237 {
238 return myIsCountedPtr ? ( myAny ? counterPtr()->ptr : 0 ) == other : ptr() == other;
239 }

References counterPtr(), myAny, myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and ptr().

◆ ptr()

template<typename T>
T * DGtal::CountedPtrOrPtr< T >::ptr ( ) const
inlineprivate
Precondition
'this' pointer object is simple.
Returns
the address pointed by myAny.

Definition at line 368 of file CountedPtrOrPtr.h.

369 {
370 // Travis is too slow in Debug mode with this ASSERT.
372 return static_cast<T*>( myAny );
373 }

References myAny, and myIsCountedPtr.

Referenced by drop(), get(), operator!=(), operator*(), operator->(), and operator==().

◆ release()

template<typename T>
void DGtal::CountedPtrOrPtr< T >::release ( )
inlineprivate

Tells this smart pointer to that it should release its current counter. If this counter was shared then the number of reference counts is decremented, else both the object pointed by the counter and the counter are freed. In all cases, this smart pointer becomes invalid.

Precondition
'this' pointer object is smart.

Definition at line 398 of file CountedPtrOrPtr.h.

399 { // decrement the count, delete if it is 0
400 // Travis is too slow in Debug mode with this ASSERT.
402 if (myAny) {
404 if (--counter->count == 0) {
405 delete counter->ptr;
406 delete counter;
407 }
408 myAny = 0;
409 }
410 }

References DGtal::CountedPtr< T >::Counter::count, counterPtr(), myAny, myIsCountedPtr, and DGtal::CountedPtr< T >::Counter::ptr.

Referenced by operator=(), operator=(), and ~CountedPtrOrPtr().

◆ selfDisplay()

template<typename T>
void DGtal::CountedPtrOrPtr< T >::selfDisplay ( std::ostream & out) const

Writes/Displays the object on an output stream.

Parameters
outthe output stream where the object is written.

◆ unique()

template<typename T>
bool DGtal::CountedPtrOrPtr< T >::unique ( ) const
inlinenoexcept
Returns
'true' iff the smart pointer is the sole one pointing on this object or if the smart pointer is invalid ('isValid()' is false) or if 'this' object is simple.

Definition at line 300 of file CountedPtrOrPtr.h.

301 {
302 return myIsCountedPtr
303 ? ( myAny ? counterPtr()->count == 1 : true )
304 : true;
305 }

References DGtal::CountedPtr< T >::Counter::count, counterPtr(), myAny, and myIsCountedPtr.

Referenced by drop().

◆ CountedConstPtrOrConstPtr< T >

template<typename T>
friend class CountedConstPtrOrConstPtr< T >
friend

Definition at line 450 of file CountedPtrOrPtr.h.

References CountedPtrOrPtr().

Field Documentation

◆ myAny

template<typename T>
void* DGtal::CountedPtrOrPtr< T >::myAny
private

If smart, the counter object pointed by 'this', or if simple, the address of the object pointed by 'this'.

Definition at line 347 of file CountedPtrOrPtr.h.

Referenced by acquire(), CountedPtrOrPtr(), CountedPtrOrPtr(), counterPtr(), drop(), get(), operator!=(), DGtal::CountedConstPtrOrConstPtr< T >::operator=(), operator=(), operator==(), ptr(), release(), and unique().

◆ myIsCountedPtr

template<typename T>
bool DGtal::CountedPtrOrPtr< T >::myIsCountedPtr
private

The documentation for this class was generated from the following files: