PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
psharedptr.h
Go to the documentation of this file.
1 /*
2  * psharedptr.h
3  *
4  * SharedPtr template
5  *
6  * Portable Windows Library
7  *
8  * Copyright (C) 2004 Post Increment
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 Post Increment
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 24177 $
27  * $Author: rjongbloed $
28  * $Date: 2010-04-05 21:52:04 +1000 (Mon, 05 Apr 2010) $
29  */
30 
31 #ifndef PTLIB_SHAREDPTR_H
32 #define PTLIB_SHAREDPTR_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib.h>
39 #include <memory>
40 
49 template <class T>
50 class PSharedPtr : public PContainer
51 {
52  PCLASSINFO(PSharedPtr, PContainer);
53  public:
54  typedef T element_type;
55 
57  { ptr = p; }
58 
60  : PContainer(c)
61  { CopyContents(c); }
62 
63  PSharedPtr(std::auto_ptr<element_type> & v)
64  { ptr = v.release(); }
65 
67  { AssignContents(c); return *this; }
68 
69  virtual ~PSharedPtr()
70  { Destruct(); }
71 
72  virtual PBoolean MakeUnique()
73  { if (PContainer::MakeUnique()) return true; CloneContents(this); return false; }
74 
75  PBoolean SetSize(PINDEX)
76  { return false; }
77 
78  T * Get() const
79  { return ptr; }
80 
81  void Reset() const
83 
84  T & operator*() const
85  { return *ptr; }
86 
87  T * operator->() const
88  { return ptr; }
89 
90 
91  protected:
92  PSharedPtr(int dummy, const PSharedPtr * c)
93  : PContainer(dummy, c)
94  { CloneContents(c); }
95 
96  void AssignContents(const PContainer & c)
98 
100  { delete(ptr); }
101 
102  void CloneContents(const PContainer * src)
103  { ptr = new element_type(*((const PSharedPtr *)src)->ptr); }
104 
105  void CopyContents(const PContainer & c)
106  { ptr = ((const PSharedPtr &)c).ptr; }
107 
108  protected:
109  T * ptr;
110 };
111 
112 
113 #endif // PTLIB_SHAREDPTR_H
114 
115 
116 // End Of File ///////////////////////////////////////////////////////////////