OpenAB  1.0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SmartPtr.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  */
10 #ifndef SMARTPTR_HPP_
11 #define SMARTPTR_HPP_
12 
13 #include <helpers/Log.hpp>
14 
18 namespace OpenAB {
31 template<typename __C>
32 class SmartPtr
33 {
34  public:
39  : ptr(0),
40  refCount(0)
41  {
42  refCount = new unsigned int;
43  *refCount = 1;
44  }
45 
51  SmartPtr(__C* p) :
52  ptr(p),
53  refCount(0)
54  {
55  refCount = new unsigned int;
56  *refCount = 1;
57  }
58 
64  SmartPtr(const SmartPtr& other) :
65  ptr(other.ptr),
66  refCount(other.refCount)
67  {
68  (*refCount)++;
69  }
70 
76  {
77  (*refCount)--;
78  if(*refCount == 0)
79  {
80  delete refCount;
81  if (ptr)
82  delete ptr;
83  }
84  }
85 
91  SmartPtr& operator=(const SmartPtr& other)
92  {
93  if (this != &other)
94  {
95  (*refCount)--;
96  if(*refCount == 0)
97  {
98  delete refCount;
99  if (ptr)
100  delete ptr;
101  }
102 
103  ptr = other.ptr;
104  refCount = other.refCount;
105  (*refCount)++;
106  }
107  return *this;
108  }
109 
110  bool operator==(const SmartPtr& other) const
111  {
112  if (NULL == ptr || NULL == other.ptr)
113  {
114  if (NULL == ptr && NULL == other.ptr)
115  {
116  return true;
117  }
118  return false;
119  }
120  return ((*ptr) == (*other.ptr));
121  }
122 
123  bool operator!=(const SmartPtr& other) const
124  {
125  if (NULL == ptr || NULL == other.ptr)
126  {
127  if (NULL == ptr && NULL == other.ptr)
128  {
129  return false;
130  }
131  return true;
132  }
133  return ((*ptr) != (*other.ptr));
134  }
135 
136  bool operator<(const SmartPtr& other) const
137  {
138  if (NULL == ptr || NULL == other.ptr)
139  {
140  if (NULL == ptr && NULL == other.ptr)
141  {
142  return false;
143  }
144  if(!ptr)
145  {
146  return true;
147  }
148  else
149  {
150  return false;
151  }
152  }
153  return ((*ptr) < (*other.ptr));
154  }
155 
156  __C* operator->() const {return ptr;}
157  __C& operator*() const {return *ptr;}
158  __C* getPointer() const {return ptr;}
159 
160  operator __C*() const {return ptr;}
161  operator const __C*() const {return (const __C*) ptr;}
162  // operator __C() {return *ptr;}
163 
164  private:
165  __C* ptr;
166  unsigned int* refCount;
167 };
168 
169 } // namespace OpenAB
170 
171 #endif // SMARTPTR_HPP_
bool operator!=(const SmartPtr &other) const
Definition: SmartPtr.hpp:123
~SmartPtr()
Destructor. Decrements reference count, if it drops to 0 frees managed data.
Definition: SmartPtr.hpp:75
Smart pointer implementation for safely passing around dynamically created data. Smart pointers are r...
Definition: SmartPtr.hpp:32
SmartPtr()
Default constructor.
Definition: SmartPtr.hpp:38
SmartPtr & operator=(const SmartPtr &other)
Assignment operator Creates new SmartPtr from other instance of SmartPtr increasing its reference cou...
Definition: SmartPtr.hpp:91
SmartPtr(__C *p)
Constructor. Creates new SmartPtr from given pointer.
Definition: SmartPtr.hpp:51
bool operator==(const SmartPtr &other) const
Definition: SmartPtr.hpp:110
__C * operator->() const
Definition: SmartPtr.hpp:156
__C & operator*() const
Definition: SmartPtr.hpp:157
SmartPtr(const SmartPtr &other)
Copy constructor. Creates new SmartPtr from other instance of SmartPtr increasing its reference count...
Definition: SmartPtr.hpp:64
bool operator<(const SmartPtr &other) const
Definition: SmartPtr.hpp:136
__C * getPointer() const
Definition: SmartPtr.hpp:158