DGtal 1.3.0
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Private Attributes
DGtal::CountedConstPtrOrConstPtr< T > Class Template Reference

Aim: Smart or simple const 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/CountedConstPtrOrConstPtr.h>

Public Types

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

Public Member Functions

 CountedConstPtrOrConstPtr (const T *p=0, bool isCountedPtr=true)
 
 ~CountedConstPtrOrConstPtr ()
 
 CountedConstPtrOrConstPtr (const CountedPtr< T > &r) noexcept
 
 CountedConstPtrOrConstPtr (const CountedConstPtrOrConstPtr &r) noexcept
 
 CountedConstPtrOrConstPtr (const CountedPtrOrPtr< T > &r) noexcept
 
CountedConstPtrOrConstPtroperator= (const CountedConstPtrOrConstPtr &r)
 
CountedConstPtrOrConstPtroperator= (const CountedPtrOrPtr< T > &r)
 
CountedConstPtrOrConstPtroperator= (const CountedPtr< T > &r)
 
bool isSmart () const
 
bool isSimple () const
 
bool operator== (const T *other) const
 
bool operator!= (const T *other) const
 
const T & operator* () const noexcept
 
const T * operator-> () const noexcept
 
const T * get () const noexcept
 
bool unique () const noexcept
 
unsigned int count () const
 
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...
 

Detailed Description

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

Aim: Smart or simple const 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 'CountedConstPtrOrConstPtr'

This object is useful when instantiating from a ConstAlias<T> object, letting the user specifies 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 const-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 a ConstAlias parameter.

struct B {};
struct A {
A( ConstAlias<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: ConstAlias.h:187
Aim: Smart or simple const pointer on T. It can be a smart pointer based on reference counts or a sim...
int main()
Definition: testBits.cpp:56
Template Parameters
Tany data type.
See also
CountedPtr
ConstAlias
Examples
geometry/surfaces/dvcm-2d-curvature.cpp.

Definition at line 94 of file CountedConstPtrOrConstPtr.h.

Member Typedef Documentation

◆ Counter

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

The counter is the same as CountedPtr.

Definition at line 102 of file CountedConstPtrOrConstPtr.h.

Constructor & Destructor Documentation

◆ CountedConstPtrOrConstPtr() [1/4]

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

Default Constructor and constructor from pointer. The created object is either a simple const pointer on p (not acquired) if isCountedPtr is false, or a smart const 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 119 of file CountedConstPtrOrConstPtr.h.

120 : myAny(0), myIsCountedPtr( isCountedPtr )
121 {
122 if ( isCountedPtr ) {
123 if (p) myAny = static_cast<void*>( new Counter( const_cast<T*>( p ) ) );
124 }
125 else
126 myAny = const_cast<void*>( static_cast<const void*>( p ) );
127 }
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::CountedConstPtrOrConstPtr< T >::myAny.

◆ ~CountedConstPtrOrConstPtr()

template<typename T >
DGtal::CountedConstPtrOrConstPtr< T >::~CountedConstPtrOrConstPtr ( )
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 135 of file CountedConstPtrOrConstPtr.h.

References DGtal::CountedConstPtrOrConstPtr< T >::myIsCountedPtr, and DGtal::CountedConstPtrOrConstPtr< T >::release().

◆ CountedConstPtrOrConstPtr() [2/4]

template<typename T >
DGtal::CountedConstPtrOrConstPtr< T >::CountedConstPtrOrConstPtr ( 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 147 of file CountedConstPtrOrConstPtr.h.

148 : myIsCountedPtr( true )
149 {
150 acquire( r.myCounter );
151 }

References DGtal::CountedConstPtrOrConstPtr< T >::acquire().

◆ CountedConstPtrOrConstPtr() [3/4]

template<typename T >
DGtal::CountedConstPtrOrConstPtr< T >::CountedConstPtrOrConstPtr ( const CountedConstPtrOrConstPtr< 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 162 of file CountedConstPtrOrConstPtr.h.

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

References DGtal::CountedConstPtrOrConstPtr< T >::acquire(), DGtal::CountedConstPtrOrConstPtr< T >::myAny, and DGtal::CountedConstPtrOrConstPtr< T >::myIsCountedPtr.

◆ CountedConstPtrOrConstPtr() [4/4]

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

Constructor from CountedPtrOrPtr. 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 180 of file CountedConstPtrOrConstPtr.h.

181 : myIsCountedPtr( r.myIsCountedPtr )
182 {
183 if ( myIsCountedPtr )
184 acquire( r.counterPtr() );
185 else
186 myAny = r.myAny;
187 }

References DGtal::CountedConstPtrOrConstPtr< T >::acquire(), DGtal::CountedConstPtrOrConstPtr< T >::myAny, and DGtal::CountedConstPtrOrConstPtr< T >::myIsCountedPtr.

Member Function Documentation

◆ acquire()

template<typename T >
void DGtal::CountedConstPtrOrConstPtr< 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 420 of file CountedConstPtrOrConstPtr.h.

421 { // increment the count
422 // Travis is too slow in Debug mode with this ASSERT.
423 ASSERT( myIsCountedPtr );
424 myAny = static_cast<void*>( c );
425 if (c) ++c->count;
426 }

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

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

◆ count()

template<typename T >
unsigned int DGtal::CountedConstPtrOrConstPtr< 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 351 of file CountedConstPtrOrConstPtr.h.

352 {
353 return myIsCountedPtr ? counterPtr()->count : 0;
354 }
unsigned count
The number of CountedPtr pointing to this counter.
Definition: CountedPtr.h:107

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

Referenced by testCountedConstPtrOrConstPtrMemory().

◆ counterPtr()

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

◆ drop()

template<typename T >
const T * DGtal::CountedConstPtrOrConstPtr< 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 366 of file CountedConstPtrOrConstPtr.h.

367 { // Gives back the pointer without deleting him. Delete only the counter.
368 ASSERT( isValid() );
369 if ( myIsCountedPtr ) {
370 ASSERT( unique() );
371 T* tmp = counterPtr()->ptr;
372 delete counterPtr();
373 myAny = 0;
374 return tmp;
375 } else {
376 return ptr();
377 }
378 }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105

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

◆ get()

template<typename T >
const T * DGtal::CountedConstPtrOrConstPtr< 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 327 of file CountedConstPtrOrConstPtr.h.

328 {
329 return myIsCountedPtr ? ( myAny ? counterPtr()->ptr : 0 ) : ptr();
330 }

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

Referenced by testCountedConstPtrOrConstPtrMemory().

◆ isSimple()

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

Definition at line 262 of file CountedConstPtrOrConstPtr.h.

263 {
264 return ! myIsCountedPtr;
265 }

References DGtal::CountedConstPtrOrConstPtr< T >::myIsCountedPtr.

Referenced by testCountedConstPtrOrConstPtrMemory().

◆ isSmart()

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

Definition at line 254 of file CountedConstPtrOrConstPtr.h.

255 {
256 return myIsCountedPtr;
257 }

References DGtal::CountedConstPtrOrConstPtr< T >::myIsCountedPtr.

Referenced by testCountedConstPtrOrConstPtrMemory().

◆ isValid()

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

Checks the validity/consistency of the object.

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

Referenced by DGtal::CountedConstPtrOrConstPtr< T >::drop(), DGtal::CountedConstPtrOrConstPtr< T >::operator*(), and DGtal::CountedConstPtrOrConstPtr< T >::operator->().

◆ operator!=()

template<typename T >
bool DGtal::CountedConstPtrOrConstPtr< 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 284 of file CountedConstPtrOrConstPtr.h.

285 {
286 return myIsCountedPtr ? ( myAny ? counterPtr()->ptr : 0 ) != other : ptr() != other;
287 }

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

◆ operator*()

template<typename T >
const T & DGtal::CountedConstPtrOrConstPtr< 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 297 of file CountedConstPtrOrConstPtr.h.

298 {
299 // Travis is too slow in Debug mode with this ASSERT.
300 ASSERT( isValid() );
301 return myIsCountedPtr ? ( * counterPtr()->ptr ) : ( * ptr() );
302 }

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

◆ operator->()

template<typename T >
const T * DGtal::CountedConstPtrOrConstPtr< 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 313 of file CountedConstPtrOrConstPtr.h.

314 {
315 // Travis is too slow in Debug mode with this ASSERT.
316 ASSERT( isValid() );
317 return myIsCountedPtr ? counterPtr()->ptr : ptr();
318 }

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

◆ operator=() [1/3]

template<typename T >
CountedConstPtrOrConstPtr & DGtal::CountedConstPtrOrConstPtr< T >::operator= ( const CountedConstPtrOrConstPtr< 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 201 of file CountedConstPtrOrConstPtr.h.

202 {
203 if ( this != & r ) {
204 if ( myIsCountedPtr ) release();
205 myIsCountedPtr = r.myIsCountedPtr;
206 if ( r.myIsCountedPtr ) acquire( r.counterPtr() );
207 else myAny = r.myAny;
208 }
209 return *this;
210 }

References DGtal::CountedConstPtrOrConstPtr< T >::acquire(), DGtal::CountedConstPtrOrConstPtr< T >::myAny, DGtal::CountedConstPtrOrConstPtr< T >::myIsCountedPtr, and DGtal::CountedConstPtrOrConstPtr< T >::release().

◆ operator=() [2/3]

template<typename T >
CountedConstPtrOrConstPtr & DGtal::CountedConstPtrOrConstPtr< 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 243 of file CountedConstPtrOrConstPtr.h.

244 {
245 if ( myIsCountedPtr ) release();
246 myIsCountedPtr = true;
247 acquire( r.myCounter );
248 return *this;
249 }

References DGtal::CountedConstPtrOrConstPtr< T >::acquire(), DGtal::CountedConstPtrOrConstPtr< T >::myIsCountedPtr, and DGtal::CountedConstPtrOrConstPtr< T >::release().

◆ operator=() [3/3]

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

Assignment with CountedPtrOrPtr. 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 224 of file CountedConstPtrOrConstPtr.h.

225 {
226 if ( myIsCountedPtr ) release();
227 myIsCountedPtr = r.myIsCountedPtr;
228 if ( r.myIsCountedPtr ) acquire( r.counterPtr() );
229 else myAny = r.myAny;
230 return *this;
231 }

References DGtal::CountedConstPtrOrConstPtr< T >::acquire(), DGtal::CountedConstPtrOrConstPtr< T >::myAny, DGtal::CountedConstPtrOrConstPtr< T >::myIsCountedPtr, and DGtal::CountedConstPtrOrConstPtr< T >::release().

◆ operator==()

template<typename T >
bool DGtal::CountedConstPtrOrConstPtr< 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 273 of file CountedConstPtrOrConstPtr.h.

274 {
275 return myIsCountedPtr ? ( myAny ? counterPtr()->ptr : 0 ) == other : ptr() == other;
276 }

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

◆ ptr()

template<typename T >
T * DGtal::CountedConstPtrOrConstPtr< T >::ptr ( ) const
inlineprivate

◆ release()

template<typename T >
void DGtal::CountedConstPtrOrConstPtr< 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 436 of file CountedConstPtrOrConstPtr.h.

437 { // decrement the count, delete if it is 0
438 // Travis is too slow in Debug mode with this ASSERT.
439 ASSERT( myIsCountedPtr );
440 if (myAny) {
441 Counter * counter = counterPtr();
442 if (--counter->count == 0) {
443 delete counter->ptr;
444 delete counter;
445 }
446 myAny = 0;
447 }
448 }

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

Referenced by DGtal::CountedConstPtrOrConstPtr< T >::operator=(), and DGtal::CountedConstPtrOrConstPtr< T >::~CountedConstPtrOrConstPtr().

◆ selfDisplay()

template<typename T >
void DGtal::CountedConstPtrOrConstPtr< 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::CountedConstPtrOrConstPtr< 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 337 of file CountedConstPtrOrConstPtr.h.

338 {
339 return myIsCountedPtr
340 ? ( myAny ? counterPtr()->count == 1 : true )
341 : true;
342 }

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

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

Field Documentation

◆ myAny

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

◆ myIsCountedPtr

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

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