PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
smartptr.h
Go to the documentation of this file.
1 /*
2  * smartptr.h
3  *
4  * Smart pointer template class.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  */
30 
31 #ifndef PTLIB_SMARTPTR_H
32 #define PTLIB_SMARTPTR_H
33 
34 #include <ptlib.h>
35 #include <ptlib/object.h>
36 
38 // "Smart" pointers.
39 
49 class PSmartObject : public PObject
50 {
51  PCLASSINFO(PSmartObject, PObject);
52 
53  protected:
58  : referenceCount(1)
59  { }
60 
61  PSmartObject(const PSmartObject & other)
62  : PObject(other)
63  , referenceCount(1)
64  { }
65 
70 
71  private:
72  void operator=(const PSmartObject&) { }
73 
74  friend class PSmartPointer;
75 };
76 
77 
111 class PSmartPointer : public PObject
112 {
113  PCLASSINFO(PSmartPointer, PObject);
114 
115  public:
122  PSmartObject * obj = NULL
123  ) { object = obj; }
124 
130  const PSmartPointer & ptr
131  );
132 
137  virtual ~PSmartPointer();
138 
150  const PSmartPointer & ptr
151  );
153 
165  virtual Comparison Compare(
166  const PObject & obj
167  ) const;
169 
178  PBoolean IsNULL() const { return object == NULL; }
179 
185  PSmartObject * GetObject() const { return object; }
187 
188  protected:
189  // Member variables
192 };
193 
194 
197 template <class T> class PSmartPtr : public PSmartPointer
198 {
199  PCLASSINFO(PSmartPtr, PSmartPointer);
200  public:
202  PSmartPtr(T * ptr = NULL)
203  : PSmartPointer(ptr) { }
204 
206  T * operator->() const
207  { return (T *)PAssertNULL(object); }
208 
210  T & operator*() const
211  { return *(T *)PAssertNULL(object); }
212 
214  operator T*() const
215  { return (T *)object; }
216 };
217 
218 
219 #endif // PTLIB_SMARTPTR_H
220 
221 
222 // End Of File ///////////////////////////////////////////////////////////////
virtual ~PSmartPointer()
Destroy the smart pointer and decrement the reference count on the object being pointed to...
virtual Comparison Compare(const PObject &obj) const
Determine the relative rank of the pointers.
PSmartObject * object
Object the smart pointer points to.
Definition: smartptr.h:191
PSmartObject(const PSmartObject &other)
Definition: smartptr.h:61
PSmartPointer & operator=(const PSmartPointer &ptr)
Assign this pointer to the value specified in the ptr parameter.
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
PSmartObject()
Construct a new smart object, subject to a PSmartPointer instance referencing it. ...
Definition: smartptr.h:57
This is the class for pointers to objects that use the smart pointer system.
Definition: smartptr.h:111
PSmartPtr(T *ptr=NULL)
Constructor.
Definition: smartptr.h:202
T * operator->() const
Access to the members of the smart object in the smart pointer.
Definition: smartptr.h:206
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:428
bool PBoolean
Definition: object.h:174
PBoolean IsNULL() const
Determine if the smart pointer has been set to point to an actual object instance.
Definition: smartptr.h:178
This template class creates a type safe version of PSmartPointer.
Definition: smartptr.h:197
T & operator*() const
Access to the dereferenced smart object in the smart pointer.
Definition: smartptr.h:210
This is the base class for objects that use the smart pointer system.
Definition: smartptr.h:49
atomic< uint32_t > referenceCount
Count of number of instances of PSmartPointer that currently reference the object instance...
Definition: smartptr.h:69
PSmartPointer(PSmartObject *obj=NULL)
Create a new smart pointer instance and have it point to the specified PSmartObject instance...
Definition: smartptr.h:121
PSmartObject * GetObject() const
Get the current value if the internal smart object pointer.
Definition: smartptr.h:185
Ultimate parent class for all objects in the class library.
Definition: object.h:2204