Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members

RCPtr< T > Class Template Reference

This template implements a "smart pointer" for reference counted objects. More...

#include <RCPtr.h>

Inheritance diagram for RCPtr< T >:

Inheritance graph
[legend]
Collaboration diagram for RCPtr< T >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 RCPtr (T *realPtr=0)
 RCPtr (const RCPtr &rhs)
 ~RCPtr ()
bool operator== (const RCPtr &rhs)
RCPtroperator= (const RCPtr &rhs)
 Assign to the "smart pointer".

T * operator-> () const
T & operator * () const

Private Member Functions

void init ()

Private Attributes

T * pointee

Detailed Description

template<class T>
class RCPtr< T >

This template implements a "smart pointer" for reference counted objects.

This template is designed to be used with the RCObject base class for reference counted objects.

This template was published in More Effective C++ by Scott Meyers, Addison-Wesley, 1996. The RCPtr template is part of Scott Meyers reference counted String class, published in "Item 29".

The RCPtr template is used to instantiate a shared data object for a reference counted object. A simplified version of the SubString object is shown below. Note that the SubString class variable value is an instance of the local shared data object SharedData.

class SubString { private:

class SharedData : public RCObject { private: String *pStr_; size_t start_; size_t subStrLen_;

public: SharedData(); }; // class sharedData

RCPtr<SharedData> value; }

The RCPtr "smart pointer" is designed to be instantiated with a class that is derived from RCObject. RCObject support the reference count. The RCPtr "smart pointer" handles assignment by deleting the shared data of "this" and assigning it the shared data from the right hand side object. The RCPtr template also decrements the reference count when the destructor is called.

Author:
Ian Kaplan www.bearcave.com

Definition at line 62 of file RCPtr.h.


Constructor & Destructor Documentation

template<class T>
RCPtr< T >::RCPtr T *  realPtr = 0  )  [inline]
 

Definition at line 93 of file RCPtr.h.

References RCPtr< T >::init().

00093                           : pointee(realPtr)
00094 {
00095   init();
00096 }

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

Definition at line 100 of file RCPtr.h.

References RCPtr< T >::init().

00100                                  : pointee( rhs.pointee )
00101 {
00102   init();
00103 }

template<class T>
RCPtr< T >::~RCPtr  )  [inline]
 

Definition at line 108 of file RCPtr.h.

References RCPtr< T >::pointee.

00109 {
00110   if (pointee) {
00111     pointee->removeReference();
00112   }
00113 }


Member Function Documentation

template<class T>
void RCPtr< T >::init  )  [inline, private]
 

Definition at line 83 of file RCPtr.h.

References RCPtr< T >::pointee.

Referenced by RCPtr< T >::operator=(), and RCPtr< T >::RCPtr().

00084 {
00085   if (pointee != 0) {
00086     pointee->addReference();
00087   }
00088 } // init

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

Definition at line 160 of file RCPtr.h.

References RCPtr< T >::pointee.

00161 {
00162   return *pointee;
00163 }

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

Definition at line 152 of file RCPtr.h.

References RCPtr< T >::pointee.

00153 {
00154   return pointee;
00155 }

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

Assign to the "smart pointer".

The "smart pointer" should be instantiated with an object derived from RCObject. The RCObject base class supports the removeReference function which deallocates "this".

In the code below, the new value for the smart pointer is assigned to pointee. The reference count is incremented to 1 (in init()) and the old object (oldPointee) is deallocated.

Definition at line 127 of file RCPtr.h.

References RCPtr< T >::init(), and RCPtr< T >::pointee.

00128 {
00129   if (pointee != rhs.pointee) {
00130     T *oldPointee = pointee;
00131     pointee = rhs.pointee;
00132     init();
00133     if (oldPointee) {
00134       oldPointee->removeReference();
00135     }
00136   }
00137   return *this;
00138 }

template<class T>
bool RCPtr< T >::operator== const RCPtr< T > &  rhs  )  [inline]
 

Definition at line 143 of file RCPtr.h.

References RCPtr< T >::pointee.

00144 {
00145   bool rslt = (pointee == rhs.pointee);
00146   return rslt;
00147 }


Member Data Documentation

template<class T>
T* RCPtr< T >::pointee [private]
 

Definition at line 76 of file RCPtr.h.

Referenced by RCPtr< T >::init(), RCPtr< T >::operator *(), RCPtr< T >::operator->(), RCPtr< T >::operator=(), RCPtr< T >::operator==(), and RCPtr< T >::~RCPtr().


The documentation for this class was generated from the following file:
Generated on Mon Sep 22 20:23:02 2003 by doxygen 1.3.3