refcount_ptr.h Source File

Back to the index.

refcount_ptr.h
Go to the documentation of this file.
1 #ifndef REFCOUNT_PTR_H
2 #define REFCOUNT_PTR_H
3 
4 /*
5  * Copyright (C) 2007-2018 Anders Gavare. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 
32 template <class T>
34 
35 
36 /**
37  * \brief Base class for reference countable objects.
38  *
39  * Usage:<pre>
40  * refcount_ptr<MyClass> myPtr = new MyClass(...);
41  * </pre>
42  * where MyClass should have increase_refcount() and
43  * decrease_refcount(), e.g.<pre>
44  * class MyClass : public ReferenceCountable
45  * {
46  * ...
47  * }</pre>
48  *
49  * Note: Although MyClass objects can be created using the following
50  * syntax:<pre>
51  * MyClass myobject(...);
52  * </pre>
53  * this causes the object to have a reference count of 0. That is, the
54  * address should not be taken of such an object and exposed to the
55  * outside.
56  *
57  * Implementation note: The counter itself is mutable, and the
58  * increase_refcount() and decrease_refcount() member functions are marked as
59  * const. This is because const objects also need to be properly
60  * reference counted.
61  */
63 {
64 public:
65  /**
66  * \brief Default constructor, which initializes the reference
67  * count to zero.
68  */
70  : m_refCount(0)
71  {
72  }
73 
75  {
76  if (m_refCount != 0) {
77  std::cerr << "TODO: ~ReferenceCountable count != 0!\n";
78  std::terminate();
79  }
80  }
81 
82 private:
83  template<class T> friend class refcount_ptr;
84 
85  /**
86  * \brief Increases the reference count of the object.
87  *
88  * @return The reference count after increasing it.
89  */
90  int increase_refcount() const
91  {
92  return (++ m_refCount);
93  }
94 
95  /**
96  * \brief Decreases the reference count of the object.
97  *
98  * @return The reference count after decreasing it. If the
99  * value is zero, the caller should delete the object.
100  */
101  int decrease_refcount() const
102  {
103  return (-- m_refCount);
104  }
105 
106 private:
107  mutable int m_refCount;
108 };
109 
110 
111 /**
112  * \brief A template class representing a reference counted pointer.
113  *
114  * Basically, when a pointer assigned to the reference counted pointer,
115  * it increases the reference count of the pointed-to object.
116  * When the reference counted pointer is destroyed (or NULL is
117  * assigned to it), it decreases the reference count of the pointed-to
118  * object. If the reference count reaches zero, the object is deleted.
119  */
120 template <class T>
121 class refcount_ptr
122 {
123 public:
124  /**
125  * Constructor for a reference counted pointer.
126  *
127  * @param p Pointer to an object; default is NULL.
128  */
129  refcount_ptr(T* p = NULL)
130  : m_p(p)
131  {
132  if (m_p != NULL)
133  m_p->increase_refcount();
134  }
135 
136  /**
137  * The destructor causes the reference count to be decreased
138  * by one. If the reference count of the object reaches zero,
139  * it is deleted (freed).
140  */
142  {
143  release();
144  }
145 
146  /**
147  * Copy constructor, which causes the reference count of the
148  * pointed-to object to be increased.
149  *
150  * @param other The reference counted pointer to copy from.
151  */
153  : m_p(other.m_p)
154  {
155  if (m_p != NULL)
156  m_p->increase_refcount();
157  }
158 
159  /**
160  * Assignment operator. If an object is already referenced,
161  * it is released (i.e. its reference is decreased, and if it
162  * is zero, it is freed). The object referenced to by the
163  * other reference counted pointer then gets its reference
164  * count increased.
165  *
166  * @param other The reference counted pointer to assign from.
167  */
169  {
170  if (this != &other) {
171  release();
172  if ((m_p = other.m_p) != NULL)
173  m_p->increase_refcount();
174  }
175 
176  return *this;
177  }
178 
179  operator T* ()
180  {
181  return m_p;
182  }
183 
185  {
186  return *m_p;
187  }
188 
190  {
191  return m_p;
192  }
193 
194  operator const T* () const
195  {
196  return m_p;
197  }
198 
199  const T& operator *() const
200  {
201  return *m_p;
202  }
203 
204  const T* operator ->() const
205  {
206  return m_p;
207  }
208 
209  /**
210  * \brief Checks whether or not an object is referenced
211  * by the reference counted pointer.
212  *
213  * @return true if the reference counted pointer is not
214  * referencing any object, false otherwise.
215  */
216  bool IsNULL() const
217  {
218  return m_p == NULL;
219  }
220 
221  /**
222  * \brief Less-than operator, e.g. for sorting.
223  *
224  * @param other The reference counted pointer to
225  * compare this object to.
226  * @return true if the plain pointer of this object is
227  * less than the plain pointer of the other object.
228  */
229  bool operator < (const refcount_ptr& other) const
230  {
231  std::ptrdiff_t diff = m_p - other.m_p;
232  return diff < 0;
233  }
234 
235  /**
236  * \brief Equals operator.
237  *
238  * @param other The reference counted pointer to
239  * compare this object to.
240  * @return true if the pointed to objects have the same
241  * address, false otherwise.
242  */
243  bool operator == (const refcount_ptr& other) const
244  {
245  return m_p == other.m_p;
246  }
247 
248  /**
249  * \brief Not-Equals operator.
250  *
251  * @param other The reference counted pointer to
252  * compare this object to.
253  * @return true if the pointed to objects have
254  * different address, false otherwise.
255  */
256  bool operator != (const refcount_ptr& other) const
257  {
258  return m_p != other.m_p;
259  }
260 
261 private:
262  /**
263  * Releases the currently references object, if any, by
264  * decreasing its reference count. If the count reaches zero,
265  * that means that no others have pointers to the object,
266  * and it is freed.
267  */
268  void release()
269  {
270  if (m_p != NULL) {
271  if (m_p->decrease_refcount() <= 0)
272  delete m_p;
273  m_p = NULL;
274  }
275  }
276 
277 private:
278  T* m_p;
279 };
280 
281 #endif // REFCOUNT_PTR_H
refcount_ptr::IsNULL
bool IsNULL() const
Checks whether or not an object is referenced by the reference counted pointer.
Definition: refcount_ptr.h:216
refcount_ptr::~refcount_ptr
~refcount_ptr()
Definition: refcount_ptr.h:141
refcount_ptr::operator*
T & operator*()
Definition: refcount_ptr.h:184
refcount_ptr::refcount_ptr
refcount_ptr(const refcount_ptr &other)
Definition: refcount_ptr.h:152
refcount_ptr::operator<
bool operator<(const refcount_ptr &other) const
Less-than operator, e.g. for sorting.
Definition: refcount_ptr.h:229
refcount_ptr::operator!=
bool operator!=(const refcount_ptr &other) const
Not-Equals operator.
Definition: refcount_ptr.h:256
ReferenceCountable::~ReferenceCountable
~ReferenceCountable()
Definition: refcount_ptr.h:74
refcount_ptr::refcount_ptr
refcount_ptr(T *p=NULL)
Definition: refcount_ptr.h:129
refcount_ptr
A template class representing a reference counted pointer.
Definition: refcount_ptr.h:33
refcount_ptr::operator=
refcount_ptr & operator=(const refcount_ptr &other)
Definition: refcount_ptr.h:168
refcount_ptr::operator->
T * operator->()
Definition: refcount_ptr.h:189
refcount_ptr::operator==
bool operator==(const refcount_ptr &other) const
Equals operator.
Definition: refcount_ptr.h:243
ReferenceCountable::ReferenceCountable
ReferenceCountable()
Default constructor, which initializes the reference count to zero.
Definition: refcount_ptr.h:69
ReferenceCountable
Base class for reference countable objects.
Definition: refcount_ptr.h:62

Generated on Tue Mar 24 2020 14:04:48 for GXemul by doxygen 1.8.17