Gyoto
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Gyoto::SmartPointer< T > Class Template Reference

Pointers performing reference counting. More...

#include <GyotoSmartPointer.h>

+ Inheritance diagram for Gyoto::SmartPointer< T >:
+ Collaboration diagram for Gyoto::SmartPointer< T >:

Public Member Functions

 SmartPointer (T *orig=NULL)
 Constructor from a standard pointer-to-class. More...
 
 SmartPointer (const SmartPointer< T > &orig)
 Copy constructor from same type. More...
 
template<class U >
 SmartPointer (const SmartPointer< U > &orig)
 Copy constructor from compatible type (used for casting) More...
 
T & operator* ()
 Dereference operator "*". More...
 
const T & operator* () const
 Dereference operator "*". More...
 
T * operator-> ()
 Dereference operator "->". More...
 
T * operator-> () const
 Dereference operator "->" (const) More...
 
bool operator== (const SmartPointer< T > &right)
 Comparison operator between two SmartPointer of same kind.
 
bool operator!= (const SmartPointer< T > &right)
 Comparison operator between two SmartPointer of same kind.
 
SmartPointer< T > & operator= (SmartPointer< T > &right)
 Copy a SmartPointer to another (already defined) SmartPointer of same kind.
 
SmartPointer< T > & operator= (T *right)
 Copy a normal pointer to an (already defined) SmartPointer of same kind.
 
 operator T* () const
 Cast SmartPointer to normal pointer.
 
const T * operator() () const
 Get standard, non-smart pointer to object. Use with care. More...
 

Private Member Functions

void decRef ()
 Decrement the reference counter. Warning: don't mess with it.
 

Private Attributes

T * obj
 Real pointer, don't mess with it.
 

Detailed Description

template<class T>
class Gyoto::SmartPointer< T >

Pointers performing reference counting.

Pointee must inherit from class SmartPointee.

To create an object and a SmartPointer pointing to it:

SmartPointer<Gyoto::Metric::Generic> ObjPtr (new Gyoto::Metric(...));
Template Parameters
TSub-class of Gyoto::SmartPointee.

Constructor & Destructor Documentation

◆ SmartPointer() [1/3]

template<class T>
Gyoto::SmartPointer< T >::SmartPointer ( T *  orig = NULL)
inline

Constructor from a standard pointer-to-class.

Parameters
orig: a pointer to an instance of class T, created using new T().

Example:

SmartPointer<Gyoto::Metric::Generic> ObjPtr (new Gyoto::Metric(...)); // create SmartPointer ObjPtr
170  : obj(orig)
171  {
172  if (obj)
173  obj->incRefCount();
174  }
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142

◆ SmartPointer() [2/3]

template<class T>
Gyoto::SmartPointer< T >::SmartPointer ( const SmartPointer< T > &  orig)
inline

Copy constructor from same type.

Parameters
orig: a SmartPointer to an instance of class T

Example:

SmartPointer<Gyoto::Metric::Generic> ObjPtr (new Gyoto::Metric(...)); // create SmartPointer ObjPtr
SmartPointer<Gyoto::Metric::Generic> ObjPtr2 = ObjPtr; // create SmartPointer ObjPtr2

ObjPtr and ObjPtr2 point to the same instance of class T. Copying increments the reference counter.

191  {
192  obj = orig.obj;
193  if (obj)
194  obj->incRefCount ();
195  }
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142

◆ SmartPointer() [3/3]

template<class T>
template<class U >
Gyoto::SmartPointer< T >::SmartPointer ( const SmartPointer< U > &  orig)
inline

Copy constructor from compatible type (used for casting)

Parameters
orig: a SmartPointer to an instance of another class U

Example: MetricPtr is a SmartPoiter<Metric>, but really points to an instance of the child class Gyoto::Kerr:

SmartPointer<Gyoto::Kerr> KerrPtr (MetricPtr);

MetricPtr and KerrPtr point to the same instance of class Kerr. The methods specific to class Kerr are available only to KerrPtr.

215  {
216  obj = dynamic_cast<T*>(const_cast<U*>(orig()));
217  if (obj)
218  obj->incRefCount ();
219  }
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142

Member Function Documentation

◆ operator()()

template<class T>
const T* Gyoto::SmartPointer< T >::operator() ( ) const
inline

Get standard, non-smart pointer to object. Use with care.

This public method is needed to cast from a SmartPointer flavor to another. It should almost certainly never be used in user code, except perhaps for debugging purposes.

Returns
usual pointer to object, T*.
352 { return obj; }
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142

◆ operator*() [1/2]

template<class T>
T& Gyoto::SmartPointer< T >::operator* ( )
inline

Dereference operator "*".

Returns
address of the pointed-to-object
227  {
228  if (!obj)
229  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
230  return *obj;
231  }
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142
void throwError(std::string)
Throw a Gyoto::Error.

◆ operator*() [2/2]

template<class T>
const T& Gyoto::SmartPointer< T >::operator* ( ) const
inline

Dereference operator "*".

Returns
address of the pointed-to-object
239  {
240  if (!obj)
241  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
242  return *obj;
243  }
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142
void throwError(std::string)
Throw a Gyoto::Error.

◆ operator->() [1/2]

template<class T>
T* Gyoto::SmartPointer< T >::operator-> ( )
inline

Dereference operator "->".

Access to the pointed-to-object's members.

251  {
252  if (!obj)
253  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
254  return obj;
255  }
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142
void throwError(std::string)
Throw a Gyoto::Error.

◆ operator->() [2/2]

template<class T>
T* Gyoto::SmartPointer< T >::operator-> ( ) const
inline

Dereference operator "->" (const)

Access to the pointed-to-object's members.

263  {
264  if (!obj)
265  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
266  return obj;
267  }
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142
void throwError(std::string)
Throw a Gyoto::Error.

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