DGtal  1.5.beta
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. More...
 

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. More...
 

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 {
CountedPtrOrPtr<B> myPtrB;
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.
int main(int argc, char **argv)
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.

122  : myAny(0), myIsCountedPtr( isCountedPtr )
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 DGtal::CountedPtrOrPtr< T >::myAny.

◆ ~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 DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, and DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::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.

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

References DGtal::CountedPtrOrPtr< T >::acquire(), DGtal::CountedPtrOrPtr< T >::myAny, and DGtal::CountedPtrOrPtr< T >::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.
385  ASSERT( myIsCountedPtr );
386  myAny = static_cast<void*>( c );
387  if (c) ++c->count;
388  }

References DGtal::CountedPtrOrPtr< T >::myAny, and DGtal::CountedPtrOrPtr< T >::myIsCountedPtr.

Referenced by DGtal::CountedPtrOrPtr< T >::CountedPtrOrPtr(), and DGtal::CountedPtrOrPtr< T >::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  }
Counter * counterPtr() const
unsigned count
The number of CountedPtr pointing to this counter.
Definition: CountedPtr.h:107

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

Referenced by testCountedConstPtrOrConstPtrMemory(), and testCountedPtrOrPtrMemory().

◆ counterPtr()

template<typename T >
Counter* DGtal::CountedPtrOrPtr< T >::counterPtr ( ) const
inlineprivate

◆ 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
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105

References DGtal::CountedPtrOrPtr< T >::counterPtr(), DGtal::CountedPtrOrPtr< T >::isValid(), DGtal::CountedPtrOrPtr< T >::myAny, DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, DGtal::CountedPtrOrPtr< T >::ptr(), and DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::counterPtr(), DGtal::CountedPtrOrPtr< T >::myAny, DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::drop(), DGtal::CountedPtrOrPtr< T >::operator*(), and DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::counterPtr(), DGtal::CountedPtrOrPtr< T >::myAny, DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::counterPtr(), DGtal::CountedPtrOrPtr< T >::isValid(), DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::counterPtr(), DGtal::CountedPtrOrPtr< T >::isValid(), DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::acquire(), DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, and DGtal::CountedPtrOrPtr< T >::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();
189  myIsCountedPtr = r.myIsCountedPtr;
190  if ( r.myIsCountedPtr ) acquire( r.counterPtr() );
191  else myAny = r.myAny;
192  }
193  return *this;
194  }

References DGtal::CountedPtrOrPtr< T >::acquire(), DGtal::CountedPtrOrPtr< T >::myAny, DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, and DGtal::CountedPtrOrPtr< T >::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 DGtal::CountedPtrOrPtr< T >::counterPtr(), DGtal::CountedPtrOrPtr< T >::myAny, DGtal::CountedPtrOrPtr< T >::myIsCountedPtr, DGtal::CountedPtr< T >::Counter::ptr, and DGtal::CountedPtrOrPtr< T >::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.
371  ASSERT( ! myIsCountedPtr );
372  return static_cast<T*>( myAny );
373  }

References DGtal::CountedPtrOrPtr< T >::myAny, and DGtal::CountedPtrOrPtr< T >::myIsCountedPtr.

Referenced by DGtal::CountedPtrOrPtr< T >::drop(), DGtal::CountedPtrOrPtr< T >::get(), DGtal::CountedPtrOrPtr< T >::operator!=(), DGtal::CountedPtrOrPtr< T >::operator*(), DGtal::CountedPtrOrPtr< T >::operator->(), and DGtal::CountedPtrOrPtr< T >::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.
401  ASSERT( myIsCountedPtr );
402  if (myAny) {
403  Counter * counter = counterPtr();
404  if (--counter->count == 0) {
405  delete counter->ptr;
406  delete counter;
407  }
408  myAny = 0;
409  }
410  }

References DGtal::CountedPtrOrPtr< T >::counterPtr(), DGtal::CountedPtrOrPtr< T >::myAny, and DGtal::CountedPtrOrPtr< T >::myIsCountedPtr.

Referenced by DGtal::CountedPtrOrPtr< T >::operator=(), and DGtal::CountedPtrOrPtr< T >::~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, DGtal::CountedPtrOrPtr< T >::counterPtr(), DGtal::CountedPtrOrPtr< T >::myAny, and DGtal::CountedPtrOrPtr< T >::myIsCountedPtr.

Referenced by DGtal::CountedPtrOrPtr< T >::drop().

Friends And Related Function Documentation

◆ CountedConstPtrOrConstPtr< T >

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

Definition at line 450 of file CountedPtrOrPtr.h.

Field Documentation

◆ myAny

◆ myIsCountedPtr


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