Prevented memory leak detection from considering anything
[pwlib.git] / include / ptlib / smartptr.h
blob74a8eff7df9d721b4e108b3375d11eb75860a6d8
1 #ifndef _PSMARTPTR_H
2 #define _PSMARTPTR_H
4 #include <ptlib.h>
5 #include <ptlib/object.h>
7 ///////////////////////////////////////////////////////////////////////////////
8 // "Smart" pointers.
10 /** This is the base class for objects that use the {\it smart pointer} system.
11 In conjunction with the #PSmartPointer# class, this class creates
12 objects that can have the automatic deletion of the object instance when
13 there are no more smart pointer instances pointing to it.
15 A #PSmartObject# carries the reference count that the #PSmartPointer#
16 requires to determine if the pointer is needed any more and should be
17 deleted.
19 class PSmartObject : public PObject
21 PCLASSINFO(PSmartObject, PObject);
23 public:
24 /** Construct a new smart object, subject to a #PSmartPointer# instance
25 referencing it.
27 PSmartObject()
28 :referenceCount(1) { }
30 protected:
31 /** Count of number of instances of #PSmartPointer# that currently
32 reference the object instance.
34 PAtomicInteger referenceCount;
37 friend class PSmartPointer;
41 /** This is the class for pointers to objects that use the {\it smart pointer}
42 system. In conjunction with the #PSmartObject# class, this class
43 references objects that can have the automatic deletion of the object
44 instance when there are no more smart pointer instances pointing to it.
46 A PSmartPointer carries the pointer to a #PSmartObject# instance which
47 contains a reference count. Assigning or copying instances of smart pointers
48 will automatically increment and decrement the reference count. When the
49 last instance that references a #PSmartObject# instance is destroyed or
50 overwritten, the #PSmartObject# is deleted.
52 A NULL value is possible for a smart pointer. It can be detected via the
53 #IsNULL()# function.
55 class PSmartPointer : public PObject
57 PCLASSINFO(PSmartPointer, PObject);
59 public:
60 /**@name Construction */
61 //@{
62 /** Create a new smart pointer instance and have it point to the specified
63 #PSmartObject# instance.
65 PSmartPointer(
66 PSmartObject * obj = NULL /// Smart object to point to.
67 ) { object = obj; }
69 /** Create a new smart pointer and point it at the data pointed to by the
70 #ptr# parameter. The reference count for the object being
71 pointed at is incremented.
73 PSmartPointer(
74 const PSmartPointer & ptr /// Smart pointer to make a copy of.
77 /** Destroy the smart pointer and decrement the reference count on the
78 object being pointed to. If there are no more references then the
79 object is deleted.
81 virtual ~PSmartPointer();
83 /** Assign this pointer to the value specified in the #ptr#
84 parameter.
86 The previous object being pointed to has its reference count
87 decremented as this will no longer point to it. If there are no more
88 references then the object is deleted.
90 The new object being pointed to after the assignment has its reference
91 count incremented.
93 PSmartPointer & operator=(
94 const PSmartPointer & ptr /// Smart pointer to assign.
96 //@}
98 /**@name Overrides from class PObject */
99 //@{
100 /** Determine the relative rank of the pointers. This is identical to
101 determining the relative rank of the integer values represented by the
102 memory pointers.
104 @return
105 #EqualTo# if objects point to the same object instance,
106 otherwise #LessThan# and #GreaterThan# may be
107 returned depending on the relative values of the memory pointers.
109 virtual Comparison Compare(
110 const PObject & obj // Other smart pointer to compare against.
111 ) const;
112 //@}
114 /**@name Pointer access functions */
115 //@{
116 /** Determine if the smart pointer has been set to point to an actual
117 object instance.
119 @return
120 TRUE if the pointer is NULL.
122 BOOL IsNULL() const { return object == NULL; }
124 /** Get the current value if the internal smart object pointer.
126 @return
127 pointer to object instance.
129 PSmartObject * GetObject() const { return object; }
130 //@}
132 protected:
133 // Member variables
134 /// Object the smart pointer points to.
135 PSmartObject * object;
139 /** This template class creates a type safe version of PSmartPointer.
141 template <class T> class PSmartPtr : public PSmartPointer
143 PCLASSINFO(PSmartPtr, PSmartPointer);
144 public:
145 /// Constructor
146 PSmartPtr(T * ptr = NULL)
147 : PSmartPointer(ptr) { }
149 /// Access to the members of the smart object in the smart pointer.
150 T * operator->() const
151 { return (T *)PAssertNULL(object); }
153 /// Access to the dereferenced smart object in the smart pointer.
154 T & operator*() const
155 { return *(T *)PAssertNULL(object); }
157 /// Access to the value of the smart pointer.
158 operator T*() const
159 { return (T *)object; }
163 #endif