PTLib  Version 2.18.8
 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  */
27 
28 #ifndef PTLIB_SHAREDPTR_H
29 #define PTLIB_SHAREDPTR_H
30 
31 #ifdef P_USE_PRAGMA
32 #pragma interface
33 #endif
34 
35 #include <ptlib.h>
36 #include <memory>
37 
38 #if __cplusplus < 201103L
39 
48 template <class T>
49 class PSharedPtr : public PContainer
50 {
51  PCLASSINFO(PSharedPtr, PContainer);
52  public:
53  typedef T element_type;
54 
56  { ptr = p; }
57 
59  : PContainer(c)
60  { CopyContents(c); }
61 
62  PSharedPtr(std::auto_ptr<element_type> & v)
63  { ptr = v.release(); }
64 
66  { AssignContents(c); return *this; }
67 
68  virtual ~PSharedPtr()
69  { Destruct(); }
70 
71  virtual PBoolean MakeUnique()
72  { if (PContainer::MakeUnique()) return true; CloneContents(this); return false; }
73 
74  PBoolean SetSize(PINDEX)
75  { return false; }
76 
77  element_type * get() const
78  { return ptr; }
79 
81  { return ptr; }
82 
83  void reset(element_type * p = NULL)
84  { AssignContents(PSharedPtr(p)); }
85 
88 
90  { return *ptr; }
91 
93  { return ptr; }
94 
95 
96  protected:
97  PSharedPtr(int dummy, const PSharedPtr * c)
98  : PContainer(dummy, c)
99  { CloneContents(c); }
100 
101  void AssignContents(const PContainer & c)
103 
105  { delete(ptr); }
106 
107  void CloneContents(const PContainer * src)
108  { ptr = ((const PSharedPtr *)src)->ptr; }
109 
110  void CopyContents(const PContainer & c)
111  { ptr = ((const PSharedPtr &)c).ptr; }
112 
113  protected:
115 };
116 
117 
118 #else // __cplusplus
119 
120 template <typename T> using PSharedPtr = std::shared_ptr<T>;
121 
122 #endif // __cplusplus
123 
124 #endif // PTLIB_SHAREDPTR_H
125 
126 
127 // End Of File ///////////////////////////////////////////////////////////////
virtual PBoolean MakeUnique()
Make this instance to be the one and only reference to the container contents.
PSharedPtr(std::auto_ptr< element_type > &v)
Definition: psharedptr.h:62
void DestroyContents()
Destroy the container contents.
Definition: psharedptr.h:104
void Destruct()
Internal function called from container destructors.
PBoolean SetSize(PINDEX)
Set the new current size of the container.
Definition: psharedptr.h:74
PSharedPtr(const PSharedPtr &c)
Definition: psharedptr.h:58
element_type * operator->() const
Definition: psharedptr.h:92
virtual PBoolean MakeUnique()
Make this instance to be the one and only reference to the container contents.
Definition: psharedptr.h:71
PSharedPtr & operator=(const PSharedPtr &c)
Definition: psharedptr.h:65
virtual void AssignContents(const PContainer &c)
Copy the container contents.
element_type & operator*() const
Definition: psharedptr.h:89
PSharedPtr(element_type *p=NULL)
Definition: psharedptr.h:55
bool PBoolean
Definition: object.h:174
P_DEPRECATED void Reset()
Definition: psharedptr.h:86
void CopyContents(const PContainer &c)
Definition: psharedptr.h:110
Abstract class to embody the base functionality of a container.
Definition: contain.h:99
void CloneContents(const PContainer *src)
Definition: psharedptr.h:107
PSharedPtr(int dummy, const PSharedPtr *c)
Definition: psharedptr.h:97
virtual ~PSharedPtr()
Definition: psharedptr.h:68
#define P_DEPRECATED
Definition: object.h:141
T element_type
Definition: psharedptr.h:53
element_type * ptr
Definition: psharedptr.h:114
void reset(element_type *p=NULL)
Definition: psharedptr.h:83
void AssignContents(const PContainer &c)
Copy the container contents.
Definition: psharedptr.h:101
These templates implement an pointner class with an integral reference count based on the PContainer ...
Definition: psharedptr.h:49
P_DEPRECATED element_type * Get() const
Definition: psharedptr.h:80