PTLib  Version 2.14.3
 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: 31903 $
30  * $Author: rjongbloed $
31  * $Date: 2014-05-15 16:44:15 +1000 (Thu, 15 May 2014) $
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 
90 class PSmartPointer : public PObject
91 {
92  PCLASSINFO(PSmartPointer, PObject);
93 
94  public:
101  PSmartObject * obj = NULL
102  ) { object = obj; }
103 
109  const PSmartPointer & ptr
110  );
111 
116  virtual ~PSmartPointer();
117 
129  const PSmartPointer & ptr
130  );
132 
144  virtual Comparison Compare(
145  const PObject & obj
146  ) const;
148 
157  PBoolean IsNULL() const { return object == NULL; }
158 
164  PSmartObject * GetObject() const { return object; }
166 
167  protected:
168  // Member variables
171 };
172 
173 
176 template <class T> class PSmartPtr : public PSmartPointer
177 {
178  PCLASSINFO(PSmartPtr, PSmartPointer);
179  public:
181  PSmartPtr(T * ptr = NULL)
182  : PSmartPointer(ptr) { }
183 
185  T * operator->() const
186  { return (T *)PAssertNULL(object); }
187 
189  T & operator*() const
190  { return *(T *)PAssertNULL(object); }
191 
193  operator T*() const
194  { return (T *)object; }
195 };
196 
197 
198 #endif // PTLIB_SMARTPTR_H
199 
200 
201 // End Of File ///////////////////////////////////////////////////////////////