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     BOOL IsNULL() const { return object == NULL; }
00123 
00129     PSmartObject * GetObject() const { return object; }
00131 
00132   protected:
00133     // Member variables
00135     PSmartObject * object;
00136 };
00137 
00138 
00155 #define PSMART_POINTER_INFO(cls, par, type) \
00156   PCLASSINFO(cls, par) \
00157   public: \
00158     type * operator->() const \
00159       { return (type *)PAssertNULL(object); } \
00160     type & operator*() const \
00161       { return *(type *)PAssertNULL(object); } \
00162     operator type*() const \
00163       { return (type *)object; }
00164 
00165 #endif
00166 

Generated on Fri Sep 21 14:40:11 2007 for PWLib by  doxygen 1.5.3