smartptr.h

Go to the documentation of this file.
00001 #ifndef _PSMARTPTR_H
00002 #define _PSMARTPTR_H
00003 
00004 #include <ptlib.h>
00005 #include <ptlib/object.h>
00006 
00008 // "Smart" pointers.
00009 
00019 class PSmartObject : public PObject
00020 {
00021   PCLASSINFO(PSmartObject, PObject);
00022 
00023   public:
00027     PSmartObject()
00028       :referenceCount(1) { }
00029 
00030   protected:
00034     PAtomicInteger referenceCount;
00035 
00036 
00037   friend class PSmartPointer;
00038 };
00039 
00040 
00055 class PSmartPointer : public PObject
00056 {
00057   PCLASSINFO(PSmartPointer, PObject);
00058 
00059   public:
00065     PSmartPointer(
00066       PSmartObject * obj = NULL   
00067     ) { object = obj; }
00068 
00073     PSmartPointer(
00074       const PSmartPointer & ptr  
00075     );
00076 
00081     virtual ~PSmartPointer();
00082 
00093     PSmartPointer & operator=(
00094       const PSmartPointer & ptr  
00095     );
00097 
00109     virtual Comparison Compare(
00110       const PObject & obj   // Other smart pointer to compare against.
00111     ) const;
00113 
00122     PBoolean IsNULL() const { return object == NULL; }
00123 
00129     PSmartObject * GetObject() const { return object; }
00131 
00132   protected:
00133     // Member variables
00135     PSmartObject * object;
00136 };
00137 
00138 
00141 template <class T> class PSmartPtr : public PSmartPointer
00142 {
00143   PCLASSINFO(PSmartPtr, PSmartPointer);
00144   public:
00146     PSmartPtr(T * ptr = NULL)
00147       : PSmartPointer(ptr) { }
00148 
00150     T * operator->() const
00151       { return (T *)PAssertNULL(object); }
00152 
00154     T & operator*() const
00155       { return *(T *)PAssertNULL(object); }
00156 
00158     operator T*() const
00159       { return (T *)object; }
00160 };
00161 
00162 
00163 #endif
00164 

Generated on Mon Sep 15 01:21:35 2008 for PTLib by  doxygen 1.5.1