5 #include <ptlib/object.h>
7 ///////////////////////////////////////////////////////////////////////////////
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
19 class PSmartObject
: public PObject
21 PCLASSINFO(PSmartObject
, PObject
);
24 /** Construct a new smart object, subject to a #PSmartPointer# instance
28 :referenceCount(1) { }
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
55 class PSmartPointer
: public PObject
57 PCLASSINFO(PSmartPointer
, PObject
);
60 /**@name Construction */
62 /** Create a new smart pointer instance and have it point to the specified
63 #PSmartObject# instance.
66 PSmartObject
* obj
= NULL
/// Smart object to point to.
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.
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
81 virtual ~PSmartPointer();
83 /** Assign this pointer to the value specified in the #ptr#
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
93 PSmartPointer
& operator=(
94 const PSmartPointer
& ptr
/// Smart pointer to assign.
98 /**@name Overrides from class PObject */
100 /** Determine the relative rank of the pointers. This is identical to
101 determining the relative rank of the integer values represented by the
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.
114 /**@name Pointer access functions */
116 /** Determine if the smart pointer has been set to point to an actual
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.
127 pointer to object instance.
129 PSmartObject
* GetObject() const { return object
; }
134 /// Object the smart pointer points to.
135 PSmartObject
* object
;
139 /** This macro is used to declare a smart pointer class members.
140 The class #cls# is the smart pointer, descended from the #par# class, to the
143 The macro declares in the class the following functions:
145 PCLASSINFO(cls, par);
148 type * operator->() const;
149 Access to the members of the smart object in the smart pointer.
151 type & operator*() const;
152 Access to the value of the smart object in the smart pointer.
155 #define PSMART_POINTER_INFO(cls, par, type) \
156 PCLASSINFO(cls, par) \
158 type * operator->() const \
159 { return (type *)PAssertNULL(object); } \
160 type & operator*() const \
161 { return *(type *)PAssertNULL(object); } \
162 operator type*() const \
163 { return (type *)object; }