DGtal 1.3.0
Loading...
Searching...
No Matches
Data Structures | Public Member Functions | Private Member Functions | Private Attributes | Friends
DGtal::CountedPtr< T > Class Template Reference

Aim: Smart pointer based on reference counts. More...

#include <DGtal/base/CountedPtr.h>

Data Structures

struct  Counter
 

Public Member Functions

 CountedPtr (T *p=0)
 
 ~CountedPtr ()
 
 CountedPtr (const CountedPtr &r) noexcept
 
CountedPtroperator= (const CountedPtr &r)
 
T & operator* () const noexcept
 
T * operator-> () const noexcept
 
T * get () const noexcept
 
bool unique () const noexcept
 
bool operator== (const T *other) const
 
bool operator!= (const T *other) const
 
unsigned int count () const
 
T * drop ()
 
void selfDisplay (std::ostream &out) const
 
bool isValid () const
 

Private Member Functions

void acquire (Counter *c) noexcept
 
void release ()
 

Private Attributes

CountermyCounter
 The counter object pointed by this smart pointer. More...
 

Friends

class CountedPtrOrPtr< T >
 Friend class which needs to access to CountedPtr.myCounter. More...
 
class CountedConstPtrOrConstPtr< T >
 Friend class which needs to access to CountedPtr.myCounter. More...
 

Detailed Description

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

Aim: Smart pointer based on reference counts.

Description of template class 'CountedPtr'

It is a standard smart pointer by reference counts. Of course, only dynamically allocated objects may be pointed by a smart pointer. The CountedPtr<T> holds a pointer to a CountedPtr::Counter object. This Counter object holds the pointer to the dynamically allocated object and an integer representing the number of smart pointers currently pointing to this Counter.

struct A{};
CountedPtr<A> smart_p1( new A );
CountedPtr<A> smart_p2( new A );
smart_p2 = smart_p1; // second object is freed, first object is now shared.
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:80
Template Parameters
Tany data type.

Taken from http://ootips.org/yonat/4dev/smart-pointers.html

Examples
geometry/volumes/fullConvexityThinning3D.cpp.

Definition at line 79 of file CountedPtr.h.

Constructor & Destructor Documentation

◆ CountedPtr() [1/2]

template<typename T >
DGtal::CountedPtr< T >::CountedPtr ( T *  p = 0)
inlineexplicit

Default Constructor and constructor from pointer.

Creates a new CountedPtr, either null if p is 0 or pointing to the given address p.

Parameters
peither 0 or a pointer on a dynamically allocated object of type T.

Definition at line 119 of file CountedPtr.h.

119 : myCounter(0)
120 {
121 if (p) myCounter = new Counter(p);
122 }
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308

References DGtal::CountedPtr< T >::myCounter.

◆ ~CountedPtr()

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

Destructor. If it was the last CountedPtr pointing on the object, delete it.

Definition at line 128 of file CountedPtr.h.

129 {
130 release();
131 }

References DGtal::CountedPtr< T >::release().

◆ CountedPtr() [2/2]

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

Copy Constructor.

Performs a smart copy. The CountedPtr only references the same object as r. There is now one more reference on the same object.

Parameters
rthe object to copy.

Definition at line 142 of file CountedPtr.h.

143 {
144 acquire(r.myCounter);
145 }
void acquire(Counter *c) noexcept
Definition: CountedPtr.h:265

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

Member Function Documentation

◆ acquire()

template<typename T >
void DGtal::CountedPtr< 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).

Definition at line 265 of file CountedPtr.h.

266 { // increment the count
267 myCounter = c;
268 if (c) ++c->count;
269 }
unsigned count
The number of CountedPtr pointing to this counter.
Definition: CountedPtr.h:107

References DGtal::CountedPtr< T >::Counter::count, and DGtal::CountedPtr< T >::myCounter.

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

◆ count()

template<typename T >
unsigned int DGtal::CountedPtr< T >::count ( ) const
inline
Note
For debug.
Returns
the number of smart pointers pointing to the same object as 'this'.

Definition at line 236 of file CountedPtr.h.

237 {
238 return myCounter->count;
239 }

References DGtal::CountedPtr< T >::Counter::count, and DGtal::CountedPtr< T >::myCounter.

Referenced by testCountedConstPtrOrConstPtrMemory(), testCountedPtrMemory(), and testCountedPtrOrPtrMemory().

◆ drop()

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

Gives back the pointer without deleting him. Deletes only the Counter.

Returns
the address that was pointed by this smart pointer
Note
Use with care.
Precondition
'isValid()' and 'unique()'.

Definition at line 249 of file CountedPtr.h.

250 {
251 ASSERT( isValid() );
252 ASSERT( unique() );
253 T* tmp = myCounter->ptr;
254 delete myCounter;
255 myCounter = 0;
256 return tmp;
257 }
bool unique() const noexcept
Definition: CountedPtr.h:204
bool isValid() const
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105

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

◆ get()

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

◆ isValid()

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

Checks the validity/consistency of the object.

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

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

◆ operator!=()

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

227 {
228 return get() != other;
229 }
T * get() const noexcept
Definition: CountedPtr.h:195

References DGtal::CountedPtr< T >::get().

◆ operator*()

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

Dereferencing operator.

Returns
a reference on the object that is pointed by the smart pointer.
Precondition
'isValid()' is true

Definition at line 174 of file CountedPtr.h.

175 {
176 return *myCounter->ptr;
177 }

References DGtal::CountedPtr< T >::myCounter, and DGtal::CountedPtr< T >::Counter::ptr.

◆ operator->()

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

Member access operator.

Returns
a pointer on the object that is pointed by the smart pointer.
Precondition
'isValid()' is true

Definition at line 184 of file CountedPtr.h.

185 {
186 return myCounter->ptr;
187 }

References DGtal::CountedPtr< T >::myCounter, and DGtal::CountedPtr< T >::Counter::ptr.

◆ operator=()

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

Assignment.

Performs a smart assignment. The current referenced object is dropped (and possibly freed if it was the last smart pointer pointing on it). Then, this CountedPtr only references the same object as r. There is now one more reference on the same object.

Parameters
rthe object to copy.
Returns
a reference on 'this'.

Definition at line 159 of file CountedPtr.h.

160 {
161 if (this != &r)
162 {
163 release();
164 acquire(r.myCounter);
165 }
166 return *this;
167 }

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

◆ operator==()

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

Equality operator ==

Parameters
otherany other pointer.
Returns
'true' if 'this' points to other.

Definition at line 215 of file CountedPtr.h.

216 {
217 return get() == other;
218 }

References DGtal::CountedPtr< T >::get().

◆ release()

template<typename T >
void DGtal::CountedPtr< 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.

Definition at line 278 of file CountedPtr.h.

279 { // decrement the count, delete if it is 0
280 if (myCounter) {
281 if (--myCounter->count == 0) {
282 delete myCounter->ptr;
283 delete myCounter;
284 }
285 myCounter = 0;
286 }
287 }

References DGtal::CountedPtr< T >::Counter::count, DGtal::CountedPtr< T >::myCounter, and DGtal::CountedPtr< T >::Counter::ptr.

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

◆ selfDisplay()

template<typename T >
void DGtal::CountedPtr< 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::CountedPtr< 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).

Definition at line 204 of file CountedPtr.h.

205 {
206 return (myCounter ? myCounter->count == 1 : true);
207 }

References DGtal::CountedPtr< T >::Counter::count, and DGtal::CountedPtr< T >::myCounter.

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

Friends And Related Function Documentation

◆ CountedConstPtrOrConstPtr< T >

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

Friend class which needs to access to CountedPtr.myCounter.

Definition at line 330 of file CountedPtr.h.

◆ CountedPtrOrPtr< T >

template<typename T >
friend class CountedPtrOrPtr< T >
friend

Friend class which needs to access to CountedPtr.myCounter.

Definition at line 330 of file CountedPtr.h.

Field Documentation

◆ myCounter

template<typename T >
Counter* DGtal::CountedPtr< T >::myCounter
private

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