PTLib  Version 2.12.9
 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  * $Revision: 24177 $
30  * $Author: rjongbloed $
31  * $Date: 2010-04-05 21:52:04 +1000 (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 {
90  PCLASSINFO(PSmartPointer, PObject);
91 
92  public:
99  PSmartObject * obj = NULL
100  ) { object = obj; }
101 
107  const PSmartPointer & ptr
108  );
109 
114  virtual ~PSmartPointer();
115 
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 {
176  PCLASSINFO(PSmartPtr, PSmartPointer);
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 ///////////////////////////////////////////////////////////////