DGtal  0.9.3beta
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends
DGtal::CountedPtrOrPtr< T > Class Template Reference

#include <DGtal/base/CountedPtr.h>

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

Public Types

typedef CountedPtr< T >::Counter Counter
 

Public Member Functions

 CountedPtrOrPtr (T *p=0, bool isCountedPtr=true)
 
 ~CountedPtrOrPtr ()
 
 CountedPtrOrPtr (const CountedPtr< T > &r) throw ()
 
 CountedPtrOrPtr (const CountedPtrOrPtr &r) throw ()
 
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 throw ()
 
T * operator-> () const throw ()
 
T * get () const throw ()
 
bool unique () const throw ()
 
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) throw ()
 
void release ()
 

Private Attributes

void * myAny
 
bool myIsCountedPtr
 

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.
Template Parameters
Tany data type.
See also
CountedPtr
Alias

Definition at line 51 of file CountedPtr.h.

Member Typedef Documentation

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

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  }
CountedPtr< T >::Counter Counter
The counter is the same as CountedPtr.
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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  }
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
template<typename T>
DGtal::CountedPtrOrPtr< T >::CountedPtrOrPtr ( const CountedPtr< T > &  r)
throw (
)
inline

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)
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
template<typename T>
DGtal::CountedPtrOrPtr< T >::CountedPtrOrPtr ( const CountedPtrOrPtr< T > &  r)
throw (
)
inline

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  }
void acquire(Counter *c)
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.

Member Function Documentation

template<typename T>
void DGtal::CountedPtrOrPtr< T >::acquire ( Counter c)
throw (
)
inlineprivate

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.

Referenced by DGtal::CountedPtrOrPtr< DummyTbl >::CountedPtrOrPtr(), and DGtal::CountedPtrOrPtr< DummyTbl >::operator=().

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  }
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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.

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

357  {
358  // Travis is too slow in Debug mode with this ASSERT.
359  ASSERT( myIsCountedPtr );
360  return static_cast<Counter*>( myAny );
361  }
CountedPtr< T >::Counter Counter
The counter is the same as CountedPtr.
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * counterPtr() const
bool isValid() const
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
template<typename T>
T* DGtal::CountedPtrOrPtr< T >::get ( ) const
throw (
)
inline

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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * counterPtr() const
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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  }
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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  }
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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< DummyTbl >::drop(), DGtal::CountedPtrOrPtr< DummyTbl >::operator*(), and DGtal::CountedPtrOrPtr< DummyTbl >::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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * counterPtr() const
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
template<typename T>
T& DGtal::CountedPtrOrPtr< T >::operator* ( ) const
throw (
)
inline

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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * counterPtr() const
bool isValid() const
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
template<typename T>
T* DGtal::CountedPtrOrPtr< T >::operator-> ( ) const
throw (
)
inline

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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * counterPtr() const
bool isValid() const
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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  }
void acquire(Counter *c)
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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  }
void acquire(Counter *c)
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * counterPtr() const
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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.

Referenced by DGtal::CountedPtrOrPtr< DummyTbl >::drop(), DGtal::CountedPtrOrPtr< DummyTbl >::get(), DGtal::CountedPtrOrPtr< DummyTbl >::operator!=(), DGtal::CountedPtrOrPtr< DummyTbl >::operator*(), DGtal::CountedPtrOrPtr< DummyTbl >::operator->(), and DGtal::CountedPtrOrPtr< DummyTbl >::operator==().

369  {
370  // Travis is too slow in Debug mode with this ASSERT.
371  ASSERT( ! myIsCountedPtr );
372  return static_cast<T*>( myAny );
373  }
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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.

Referenced by DGtal::CountedPtrOrPtr< DummyTbl >::operator=(), and DGtal::CountedPtrOrPtr< DummyTbl >::~CountedPtrOrPtr().

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  }
Counter * counterPtr() const
CountedPtr< T >::Counter Counter
The counter is the same as CountedPtr.
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.
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.
template<typename T>
bool DGtal::CountedPtrOrPtr< T >::unique ( ) const
throw (
)
inline
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.

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

301  {
302  return myIsCountedPtr
303  ? ( myAny ? counterPtr()->count == 1 : true )
304  : true;
305  }
Counter * counterPtr() const
unsigned count
The number of CountedPtr pointing to this counter.
Definition: CountedPtr.h:107
bool myIsCountedPtr
If true, 'this' pointer object is smart, otherwise it is simple.

Friends And Related Function Documentation

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

Definition at line 98 of file CountedPtrOrPtr.h.

Field Documentation

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

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