DGtal  0.9.4beta
Data Structures | Public Member Functions | Private Member Functions | Private Attributes | Friends
DGtal::CountedPtr< T > Class Template Reference

#include <DGtal/base/CountedPtr.h>

Inheritance diagram for DGtal::CountedPtr< T >:
[legend]
Collaboration diagram for DGtal::CountedPtr< T >:
[legend]

Data Structures

struct  Counter
 

Public Member Functions

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

Private Attributes

CountermyCounter
 

Friends

class CountedPtrOrPtr< T >
 
class CountedConstPtrOrConstPtr< T >
 

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.
Template Parameters
Tany data type.

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

Definition at line 79 of file CountedPtr.h.

Constructor & Destructor Documentation

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
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  }
template<typename T>
DGtal::CountedPtr< T >::CountedPtr ( const CountedPtr< T > &  r)
throw (
)
inline

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)
Definition: CountedPtr.h:265

Member Function Documentation

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

Definition at line 265 of file CountedPtr.h.

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

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
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308
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  }
unsigned count
The number of CountedPtr pointing to this counter.
Definition: CountedPtr.h:107
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308
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 isValid() const
bool unique() const
Definition: CountedPtr.h:204
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308
template<typename T>
T* DGtal::CountedPtr< T >::get ( ) const
throw (
)
inline

Secured member access operator.

Returns
a pointer on the object that is pointed by the smart pointer or 0 if the object is not valid ('isValid()' is false).

Definition at line 195 of file CountedPtr.h.

Referenced by DGtal::CowPtr< Domain >::copy(), DGtal::GraphVisitorRange< TGraphVisitor >::GenericConstIterator< TAccessor >::operator*(), DGtal::GraphVisitorRange< TGraphVisitor >::GenericConstIterator< TAccessor >::operator->(), and DGtal::GraphVisitorRange< TGraphVisitor >::GenericConstIterator< TAccessor >::operator==().

196  {
197  return myCounter ? myCounter->ptr : 0;
198  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308
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< Domain >::drop().

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  }
template<typename T>
T& DGtal::CountedPtr< T >::operator* ( ) const
throw (
)
inline

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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308
template<typename T>
T* DGtal::CountedPtr< T >::operator-> ( ) const
throw (
)
inline

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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308
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  }
void acquire(Counter *c)
Definition: CountedPtr.h:265
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  }
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.

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

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  }
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
unsigned count
The number of CountedPtr pointing to this counter.
Definition: CountedPtr.h:107
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308
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.
template<typename T>
bool DGtal::CountedPtr< 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).

Definition at line 204 of file CountedPtr.h.

Referenced by DGtal::CowPtr< Domain >::copy(), and DGtal::CountedPtr< Domain >::drop().

205  {
206  return (myCounter ? myCounter->count == 1 : true);
207  }
unsigned count
The number of CountedPtr pointing to this counter.
Definition: CountedPtr.h:107
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308

Friends And Related Function Documentation

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

Friend class which needs to access to CountedPtr.myCounter.

Definition at line 86 of file CountedPtr.h.

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

Friend class which needs to access to CountedPtr.myCounter.

Definition at line 84 of file CountedPtr.h.

Field Documentation

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

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