cartobase  5.0.5
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)
 
template<class U >
 const_ref (std::auto_ptr< U > r)
 
 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 77 of file rcptr.h.

Member Typedef Documentation

◆ referenced_type

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

Definition at line 303 of file rcptr.h.

Constructor & Destructor Documentation

◆ const_ref() [1/8]

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

Definition at line 304 of file rcptr.h.

◆ const_ref() [2/8]

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

Definition at line 306 of file rcptr.h.

◆ const_ref() [3/8]

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

Definition at line 311 of file rcptr.h.

◆ const_ref() [4/8]

template<class T>
template<class U >
carto::const_ref< T >::const_ref ( std::auto_ptr< U >  r)
inline

Definition at line 327 of file rcptr.h.

◆ const_ref() [5/8]

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

Definition at line 333 of file rcptr.h.

◆ const_ref() [6/8]

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

Definition at line 343 of file rcptr.h.

◆ const_ref() [7/8]

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

Definition at line 354 of file rcptr.h.

◆ const_ref() [8/8]

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

Definition at line 367 of file rcptr.h.

◆ ~const_ref()

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

Definition at line 377 of file rcptr.h.

Member Function Documentation

◆ isNull()

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

Definition at line 383 of file rcptr.h.

◆ operator!=() [1/3]

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

Definition at line 387 of file rcptr.h.

◆ operator!=() [2/3]

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

Definition at line 388 of file rcptr.h.

◆ operator!=() [3/3]

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

Definition at line 389 of file rcptr.h.

◆ operator*()

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

Definition at line 395 of file rcptr.h.

◆ operator->()

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

Definition at line 391 of file rcptr.h.

◆ operator=() [1/2]

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

Definition at line 427 of file rcptr.h.

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

◆ operator=() [2/2]

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

Definition at line 445 of file rcptr.h.

◆ operator==() [1/3]

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

Definition at line 384 of file rcptr.h.

◆ operator==() [2/3]

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

Definition at line 385 of file rcptr.h.

◆ operator==() [3/3]

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

Definition at line 386 of file rcptr.h.

◆ pointer()

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

Definition at line 399 of file rcptr.h.

◆ refCount()

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

Definition at line 403 of file rcptr.h.

Friends And Related Function Documentation

◆ DefaultRefConstruction

template<class T>
friend class DefaultRefConstruction
friend

Definition at line 296 of file rcptr.h.

◆ RCObject

template<class T>
friend class RCObject
friend

Definition at line 297 of file rcptr.h.

◆ ref

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

Definition at line 299 of file rcptr.h.


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