PTLib  Version 2.10.11
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  * $Revision: 24177 $
30  * $Author: rjongbloed $
31  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
32  */
33 
34 #ifndef PTLIB_SMARTPTR_H
35 #define PTLIB_SMARTPTR_H
36 
37 #include <ptlib.h>
38 #include <ptlib/object.h>
39 
41 // "Smart" pointers.
42 
52 class PSmartObject : public PObject
53 {
54  PCLASSINFO(PSmartObject, PObject);
55 
56  public:
61  :referenceCount(1) { }
62 
63  protected:
68 
69 
70  friend class PSmartPointer;
71 };
72 
73 
88 class PSmartPointer : public PObject
89 {
91 
92  public:
99  PSmartObject * obj = NULL
100  ) { object = obj; }
101 
107  const PSmartPointer & ptr
108  );
109 
114  virtual ~PSmartPointer();
115 
126  PSmartPointer & operator=(
127  const PSmartPointer & ptr
128  );
130 
142  virtual Comparison Compare(
143  const PObject & obj
144  ) const;
146 
155  PBoolean IsNULL() const { return object == NULL; }
156 
162  PSmartObject * GetObject() const { return object; }
164 
165  protected:
166  // Member variables
169 };
170 
171 
174 template <class T> class PSmartPtr : public PSmartPointer
175 {
177  public:
179  PSmartPtr(T * ptr = NULL)
180  : PSmartPointer(ptr) { }
181 
183  T * operator->() const
184  { return (T *)PAssertNULL(object); }
185 
187  T & operator*() const
188  { return *(T *)PAssertNULL(object); }
189 
191  operator T*() const
192  { return (T *)object; }
193 };
194 
195 
196 #endif // PTLIB_SMARTPTR_H
197 
198 
199 // End Of File ///////////////////////////////////////////////////////////////
virtual Comparison Compare(const PObject &obj) const
Compare the two objects and return their relative rank.
PSmartObject * object
Object the smart pointer points to.
Definition: smartptr.h:168
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
PSmartObject()
Construct a new smart object, subject to a PSmartPointer instance referencing it. ...
Definition: smartptr.h:60
This is the class for pointers to objects that use the smart pointer system.
Definition: smartptr.h:88
PSmartPtr(T *ptr=NULL)
Constructor.
Definition: smartptr.h:179
T & operator*() const
Access to the dereferenced smart object in the smart pointer.
Definition: smartptr.h:187
BOOL PBoolean
Definition: object.h:102
This class implements an integer that can be atomically incremented and decremented in a thread-safe ...
Definition: critsec.h:171
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:220
friend class PSmartPointer
Definition: smartptr.h:70
PSmartObject * GetObject() const
Get the current value if the internal smart object pointer.
Definition: smartptr.h:162
T * operator->() const
Access to the members of the smart object in the smart pointer.
Definition: smartptr.h:183
This template class creates a type safe version of PSmartPointer.
Definition: smartptr.h:174
PBoolean IsNULL() const
Determine if the smart pointer has been set to point to an actual object instance.
Definition: smartptr.h:155
This is the base class for objects that use the smart pointer system.
Definition: smartptr.h:52
PSmartPointer(PSmartObject *obj=NULL)
Create a new smart pointer instance and have it point to the specified PSmartObject instance...
Definition: smartptr.h:98
PAtomicInteger referenceCount
Count of number of instances of PSmartPointer that currently reference the object instance...
Definition: smartptr.h:67
Ultimate parent class for all objects in the class library.
Definition: object.h:1118