cartobase  4.5.0
carto::const_ref< T > Class Template Reference

Constant Ref to an object of class T. More...

#include <cartobase/smart/rcptr.h>

Inheritance diagram for carto::const_ref< T >:
Collaboration diagram for carto::const_ref< T >:

Public Types

typedef T referenced_type
 

Public Member Functions

 const_ref ()
 
 const_ref (const T *pObject)
 
 const_ref (const T *pObject, bool externalowner)
 
 const_ref (const ref< T > &other)
 
 const_ref (const const_ref< T > &other)
 
template<class R >
 const_ref (const ref< R > &o)
 
template<class R >
 const_ref (const const_ref< R > &other)
 
 ~const_ref ()
 
const_ref< T > & operator= (const ref< T > &other)
 
const_ref< T > & operator= (const const_ref< T > &other)
 
bool isNull () const
 
bool operator== (const ref< T > &other) const
 
bool operator== (const T *pointer) const
 
bool operator== (const const_ref< T > &other) const
 
bool operator!= (const ref< T > &other) const
 
bool operator!= (const const_ref< T > &other) const
 
bool operator!= (const T *pointer) const
 
const T * operator-> () const
 
const T & operator* () const
 
const T * pointer () const
 
int refCount () const
 
- Public Member Functions inherited from carto::RefData< T >
int count () const
 

Friends

class DefaultRefConstruction
 
class RCObject
 
template<class R >
class ref
 

Detailed Description

template<class T>
class carto::const_ref< T >

Constant Ref to an object of class T.

The purpose of this class is to provide a way to reference dynamically allocated objects without worrying about their destruction. To avoid confusion with build-in C++ references we call a Ref an object of class const_ref or ref.

A Ref act as a pointer on a dynamicaly allocated object (with new). The main difference between a Ref an a pointer is the link that exists between the Ref lifetime and the pointed object lifetime. An object pointed to by a Ref is destroyed when the last Ref pointed to it is destroyed. Therefore a Ref contains two things: a pointer to an object and a reference counter. Each time a Ref is created, the counter is incremented. When a Ref is destroyed, the counter is decremented and if the counter is null, the object is destroyed (with delete).

A Ref can be used to reference any object created by new. Therefore it can be used with existing classes.

Warning
It is the responssability to the programmer to avoid cyclic references or memory leak can occur. For example, in the following program, the objects of class A and B are never destroyed.
#include <iostream>
#include <rcptr.h>
using namespace carto;
class A;
class B;
class A {
public:
A();
~A();
ref<B> rb;
};
class B
{
public:
B();
~B();
ref<A> ra;
};
A::A() { cerr << "Construction of A" << endl; }
A::~A() { cerr << "Destruction of A" << endl; }
B::B() { cerr << "Construction of B" << endl; }
B::~B() { cerr << "Destruction of B" << endl; }
int main()
{
ref<A> ra = new A;
ref<B> rb = new B;
ra->rb = rb;
rb->ra = ra;
}
In the general case the pointer returned by new can be used to build only one Ref. If it is used to build several Ref, the object will be destroyed several times and the program will crash. See RCObject to avoid this problem.

Definition at line 76 of file rcptr.h.

Member Typedef Documentation

template<class T>
typedef T carto::const_ref< T >::referenced_type

Definition at line 295 of file rcptr.h.

Constructor & Destructor Documentation

template<class T>
carto::const_ref< T >::const_ref ( )
inline

Definition at line 296 of file rcptr.h.

template<class T>
carto::const_ref< T >::const_ref ( const T *  pObject)
inlineexplicit

Definition at line 298 of file rcptr.h.

template<class T>
carto::const_ref< T >::const_ref ( const T *  pObject,
bool  externalowner 
)
inlineexplicit

Definition at line 303 of file rcptr.h.

template<class T>
carto::const_ref< T >::const_ref ( const ref< T > &  other)
inline

Definition at line 308 of file rcptr.h.

template<class T>
carto::const_ref< T >::const_ref ( const const_ref< T > &  other)
inline

Definition at line 318 of file rcptr.h.

template<class T>
template<class R >
carto::const_ref< T >::const_ref ( const ref< R > &  o)
inline

Definition at line 329 of file rcptr.h.

template<class T>
template<class R >
carto::const_ref< T >::const_ref ( const const_ref< R > &  other)
inline

Definition at line 342 of file rcptr.h.

template<class T>
carto::const_ref< T >::~const_ref ( )
inline

Definition at line 352 of file rcptr.h.

Member Function Documentation

template<class T>
bool carto::const_ref< T >::isNull ( ) const
inline

Definition at line 358 of file rcptr.h.

template<class T>
bool carto::const_ref< T >::operator!= ( const ref< T > &  other) const
inline

Definition at line 362 of file rcptr.h.

template<class T>
bool carto::const_ref< T >::operator!= ( const const_ref< T > &  other) const
inline

Definition at line 363 of file rcptr.h.

template<class T>
bool carto::const_ref< T >::operator!= ( const T *  pointer) const
inline

Definition at line 364 of file rcptr.h.

template<class T>
const T& carto::const_ref< T >::operator* ( ) const
inline

Definition at line 370 of file rcptr.h.

template<class T>
const T* carto::const_ref< T >::operator-> ( ) const
inline

Definition at line 366 of file rcptr.h.

template<class T>
const_ref< T > & carto::const_ref< T >::operator= ( const ref< T > &  other)

Definition at line 402 of file rcptr.h.

Referenced by carto::ref< carto::BaseParameter >::operator=().

template<class T>
const_ref< T > & carto::const_ref< T >::operator= ( const const_ref< T > &  other)

Definition at line 420 of file rcptr.h.

template<class T>
bool carto::const_ref< T >::operator== ( const ref< T > &  other) const
inline

Definition at line 359 of file rcptr.h.

template<class T>
bool carto::const_ref< T >::operator== ( const T *  pointer) const
inline

Definition at line 360 of file rcptr.h.

template<class T>
bool carto::const_ref< T >::operator== ( const const_ref< T > &  other) const
inline

Definition at line 361 of file rcptr.h.

template<class T>
const T* carto::const_ref< T >::pointer ( ) const
inline
template<class T>
int carto::const_ref< T >::refCount ( ) const
inline

Definition at line 378 of file rcptr.h.

Friends And Related Function Documentation

template<class T>
friend class DefaultRefConstruction
friend

Definition at line 288 of file rcptr.h.

template<class T>
friend class RCObject
friend

Definition at line 289 of file rcptr.h.

template<class T>
template<class R >
friend class ref
friend

Definition at line 291 of file rcptr.h.


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