Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

CTBsharedCObjPtr Class Template Reference

Shared object pointer. More...

#include <CTBsharedObjPtr.hxx>

Inheritance diagram for CTBsharedCObjPtr:

Inheritance graph
[legend]
Collaboration diagram for CTBsharedCObjPtr:

Collaboration graph
[legend]
List of all members.

Public Methods

 CTBsharedCObjPtr ()
 CTBsharedCObjPtr (const CTBsharedCObjPtr< T > &rhs)
 CTBsharedCObjPtr (T *p)
 ~CTBsharedCObjPtr ()
bool IsUnique () const
bool IsShared () const
CTBint ShareCount () const
bool Equal (const CTBsharedCObjPtr< T > &rhs) const
void ToStream (ostream &os) const
CTBsharedCObjPtr< T > & operator= (const CTBsharedCObjPtr< T > &rhs)
CTBsharedCObjPtr< T > & operator= (T *rhs)
const Toperator * () const
const Toperator-> () const
bool operator! () const
 operator void * () const

Protected Attributes

Tmp_obj
CTBreferenceCountermp_refcount

Private Methods

void Clear ()

Related Functions

(Note that these are not member functions.)

bool operator== (const CTBsharedCObjPtr< T > &lhs, const CTBsharedCObjPtr< T > &rhs)
ostream & operator<< (ostream &os, const CTBsharedCObjPtr< T > &obj)

Detailed Description

template<class T>
class CTBsharedCObjPtr< T >

Shared object pointer.

This is a `smart pointer' implementing a many-to-one correspondence between pointers and an object on the heap. A reference count keeps track of how many pointers refer to the same object.

The copy constructor and the assignment operator increment and decrement the reference count. The controled object is deleted when the last controling pointer is destroyed or assigned with another value.

For obvious reasons there is no operator T *() method. But sometimes it is necessary to have an explicit pointer to the controlled object. Use &(*p) at your own risk, it can compromise the lifetime control of CTBsharedObjPtr.

Applicability:
This pointer class should work with almost every type T. It doesn't require a copy constructor and can thus be used for abstract base classes.
Requirements:
Credits:
Inspired by the discussion of Programmer-Defined Pointer Classes in Barton & Nackman "Scientific and Engeneering C++".

Definition at line 19 of file CTBsharedObjPtr.hxx.


Constructor & Destructor Documentation

template<class T>
CTBsharedCObjPtr< T >::CTBsharedCObjPtr [inline]
 

Default constructor.

Definition at line 48 of file CTBsharedObjPtr.icc.

template<class T>
CTBsharedCObjPtr< T >::CTBsharedCObjPtr const CTBsharedCObjPtr< T > & rhs [inline]
 

Copy constructor.

If rhs is non-zero, the reference count of the object controlled by rhs is increased by one.

Definition at line 61 of file CTBsharedObjPtr.icc.

template<class T>
CTBsharedCObjPtr< T >::CTBsharedCObjPtr T * p [inline]
 

Construct from pointer.

The object refered to by p becomes a controlled object, its reference count is set to one. In most cases the argument is the new operator, like

    CTBsharedCObjPtr<foo>  p(new foo(a1,a2));

Definition at line 79 of file CTBsharedObjPtr.icc.

template<class T>
CTBsharedCObjPtr< T >::~CTBsharedCObjPtr [inline]
 

Destructor.

The reference count of the controlled object will be decreased by one. If this was the last pointer refering to the object (the reference count reaches zero), the controlled object is deleted.

Definition at line 93 of file CTBsharedObjPtr.icc.


Member Function Documentation

template<class T>
bool CTBsharedCObjPtr< T >::IsUnique const [inline]
 

Returns true if controlled object is not shared.

Note, that true is returned if the ShareCount() is less or equal 1. As a consequence this method returns also true if the pointer is zero. See also IsShared() and ShareCount() .

Definition at line 107 of file CTBsharedObjPtr.icc.

template<class T>
bool CTBsharedCObjPtr< T >::IsShared const [inline]
 

Returns true if controlled object is shared.

Is true if more than one pointer refers to the controlled object. See also IsUnique() and ShareCount() .

Definition at line 120 of file CTBsharedObjPtr.icc.

template<class T>
CTBint CTBsharedCObjPtr< T >::ShareCount const [inline]
 

Returns the count of pointers refering to the controlled object.

A zero is returned if the pointer is zero. See also IsUnique() and IsShared() .

Definition at line 133 of file CTBsharedObjPtr.icc.

template<class T>
bool CTBsharedCObjPtr< T >::Equal const CTBsharedCObjPtr< T > & rhs const [inline]
 

Returns true if rhs points to the same controlled object.

Definition at line 142 of file CTBsharedObjPtr.icc.

Referenced by operator==().

template<class T>
void CTBsharedCObjPtr< T >::ToStream ostream & os const [inline]
 

ostream insertion.

Definition at line 151 of file CTBsharedObjPtr.icc.

template<class T>
CTBsharedCObjPtr< T > & CTBsharedCObjPtr< T >::operator= const CTBsharedCObjPtr< T > & rhs [inline]
 

Assign reference.

This pointer will refer to the same object as pointer rhs does.

Definition at line 185 of file CTBsharedObjPtr.icc.

Referenced by CTBsharedObjPtr::operator=().

template<class T>
CTBsharedCObjPtr< T > & CTBsharedCObjPtr< T >::operator= T * rhs [inline]
 

Make rhs the controlled object.

Reimplemented in CTBsharedObjPtr.

Definition at line 199 of file CTBsharedObjPtr.icc.

template<class T>
const T & CTBsharedCObjPtr< T >::operator * const [inline]
 

Dereference pointer (access controlled object).

Reimplemented in CTBsharedObjPtr.

Definition at line 211 of file CTBsharedObjPtr.icc.

template<class T>
const T * CTBsharedCObjPtr< T >::operator-> const [inline]
 

Access member of controlled object.

Reimplemented in CTBsharedObjPtr.

Definition at line 220 of file CTBsharedObjPtr.icc.

template<class T>
bool CTBsharedCObjPtr< T >::operator! const [inline]
 

Returns true if pointer null.

Definition at line 229 of file CTBsharedObjPtr.icc.

template<class T>
CTBsharedCObjPtr< T >::operator void * const [inline]
 

Returns non-zero pointer if pointer not null.

Definition at line 238 of file CTBsharedObjPtr.icc.

template<class T>
void CTBsharedCObjPtr< T >::Clear [inline, private]
 

Clear pointer.

The reference count of the controlled object will be decreased by one. If the reference count reaches zero, the controlled object is deleted.

Definition at line 165 of file CTBsharedObjPtr.icc.

Referenced by operator=(), and ~CTBsharedCObjPtr().


Friends And Related Function Documentation

template<class T>
bool operator== const CTBsharedCObjPtr< T > & lhs,
const CTBsharedCObjPtr< T > & rhs
[related]
 

Returns true if lhs and rhs point to the same objct.

Definition at line 250 of file CTBsharedObjPtr.icc.

template<class T>
ostream & operator<< ostream & os,
const CTBsharedCObjPtr< T > & obj
[related]
 

ostream insertion operator.

Definition at line 263 of file CTBsharedObjPtr.icc.


Member Data Documentation

template<class T>
T* CTBsharedCObjPtr::mp_obj [protected]
 

pointer to controlled object.

Definition at line 47 of file CTBsharedObjPtr.hxx.

template<class T>
CTBreferenceCounter* CTBsharedCObjPtr::mp_refcount [protected]
 

pointer to reference count.

Definition at line 48 of file CTBsharedObjPtr.hxx.


The documentation for this class was generated from the following files:
Generated at Fri Oct 24 18:14:06 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001