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-2019 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 #include <stddef.h>
37 
38 /**
39  * \brief Base class for reference countable objects.
40  *
41  * Usage:<pre>
42  * refcount_ptr<MyClass> myPtr = new MyClass(...);
43  * </pre>
44  * where MyClass should have increase_refcount() and
45  * decrease_refcount(), e.g.<pre>
46  * class MyClass : public ReferenceCountable
47  * {
48  * ...
49  * }</pre>
50  *
51  * Note: Although MyClass objects can be created using the following
52  * syntax:<pre>
53  * MyClass myobject(...);
54  * </pre>
55  * this causes the object to have a reference count of 0. That is, the
56  * address should not be taken of such an object and exposed to the
57  * outside.
58  *
59  * Implementation note: The counter itself is mutable, and the
60  * increase_refcount() and decrease_refcount() member functions are marked as
61  * const. This is because const objects also need to be properly
62  * reference counted.
63  */
65 {
66 public:
67  /**
68  * \brief Default constructor, which initializes the reference
69  * count to zero.
70  */
72  : m_refCount(0)
73  {
74  }
75 
77  {
78  if (m_refCount != 0) {
79  std::cerr << "TODO: ~ReferenceCountable count != 0!\n";
80  std::terminate();
81  }
82  }
83 
84 private:
85  template<class T> friend class refcount_ptr;
86 
87  /**
88  * \brief Increases the reference count of the object.
89  *
90  * @return The reference count after increasing it.
91  */
92  int increase_refcount() const
93  {
94  return (++ m_refCount);
95  }
96 
97  /**
98  * \brief Decreases the reference count of the object.
99  *
100  * @return The reference count after decreasing it. If the
101  * value is zero, the caller should delete the object.
102  */
103  int decrease_refcount() const
104  {
105  return (-- m_refCount);
106  }
107 
108 private:
109  mutable int m_refCount;
110 };
111 
112 
113 /**
114  * \brief A template class representing a reference counted pointer.
115  *
116  * Basically, when a pointer assigned to the reference counted pointer,
117  * it increases the reference count of the pointed-to object.
118  * When the reference counted pointer is destroyed (or NULL is
119  * assigned to it), it decreases the reference count of the pointed-to
120  * object. If the reference count reaches zero, the object is deleted.
121  */
122 template <class T>
123 class refcount_ptr
124 {
125 public:
126  /**
127  * Constructor for a reference counted pointer.
128  *
129  * @param p Pointer to an object; default is NULL.
130  */
131  refcount_ptr(T* p = NULL)
132  : m_p(p)
133  {
134  if (m_p != NULL)
135  m_p->increase_refcount();
136  }
137 
138  /**
139  * The destructor causes the reference count to be decreased
140  * by one. If the reference count of the object reaches zero,
141  * it is deleted (freed).
142  */
144  {
145  release();
146  }
147 
148  /**
149  * Copy constructor, which causes the reference count of the
150  * pointed-to object to be increased.
151  *
152  * @param other The reference counted pointer to copy from.
153  */
155  : m_p(other.m_p)
156  {
157  if (m_p != NULL)
158  m_p->increase_refcount();
159  }
160 
161  /**
162  * Assignment operator. If an object is already referenced,
163  * it is released (i.e. its reference is decreased, and if it
164  * is zero, it is freed). The object referenced to by the
165  * other reference counted pointer then gets its reference
166  * count increased.
167  *
168  * @param other The reference counted pointer to assign from.
169  */
171  {
172  if (this != &other) {
173  release();
174  if ((m_p = other.m_p) != NULL)
175  m_p->increase_refcount();
176  }
177 
178  return *this;
179  }
180 
181  operator T* ()
182  {
183  return m_p;
184  }
185 
187  {
188  return *m_p;
189  }
190 
192  {
193  return m_p;
194  }
195 
196  operator const T* () const
197  {
198  return m_p;
199  }
200 
201  const T& operator *() const
202  {
203  return *m_p;
204  }
205 
206  const T* operator ->() const
207  {
208  return m_p;
209  }
210 
211  /**
212  * \brief Checks whether or not an object is referenced
213  * by the reference counted pointer.
214  *
215  * @return true if the reference counted pointer is not
216  * referencing any object, false otherwise.
217  */
218  bool IsNULL() const
219  {
220  return m_p == NULL;
221  }
222 
223  /**
224  * \brief Less-than operator, e.g. for sorting.
225  *
226  * @param other The reference counted pointer to
227  * compare this object to.
228  * @return true if the plain pointer of this object is
229  * less than the plain pointer of the other object.
230  */
231  bool operator < (const refcount_ptr& other) const
232  {
233  std::ptrdiff_t diff = m_p - other.m_p;
234  return diff < 0;
235  }
236 
237  /**
238  * \brief Equals operator.
239  *
240  * @param other The reference counted pointer to
241  * compare this object to.
242  * @return true if the pointed to objects have the same
243  * address, false otherwise.
244  */
245  bool operator == (const refcount_ptr& other) const
246  {
247  return m_p == other.m_p;
248  }
249 
250  /**
251  * \brief Not-Equals operator.
252  *
253  * @param other The reference counted pointer to
254  * compare this object to.
255  * @return true if the pointed to objects have
256  * different address, false otherwise.
257  */
258  bool operator != (const refcount_ptr& other) const
259  {
260  return m_p != other.m_p;
261  }
262 
263 private:
264  /**
265  * Releases the currently references object, if any, by
266  * decreasing its reference count. If the count reaches zero,
267  * that means that no others have pointers to the object,
268  * and it is freed.
269  */
270  void release()
271  {
272  if (m_p != NULL) {
273  if (m_p->decrease_refcount() <= 0)
274  delete m_p;
275  m_p = NULL;
276  }
277  }
278 
279 private:
280  T* m_p;
281 };
282 
283 #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:218
refcount_ptr::~refcount_ptr
~refcount_ptr()
Definition: refcount_ptr.h:143
refcount_ptr::operator*
T & operator*()
Definition: refcount_ptr.h:186
refcount_ptr::refcount_ptr
refcount_ptr(const refcount_ptr &other)
Definition: refcount_ptr.h:154
refcount_ptr::operator<
bool operator<(const refcount_ptr &other) const
Less-than operator, e.g. for sorting.
Definition: refcount_ptr.h:231
refcount_ptr::operator!=
bool operator!=(const refcount_ptr &other) const
Not-Equals operator.
Definition: refcount_ptr.h:258
ReferenceCountable::~ReferenceCountable
~ReferenceCountable()
Definition: refcount_ptr.h:76
refcount_ptr::refcount_ptr
refcount_ptr(T *p=NULL)
Definition: refcount_ptr.h:131
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:170
refcount_ptr::operator->
T * operator->()
Definition: refcount_ptr.h:191
refcount_ptr::operator==
bool operator==(const refcount_ptr &other) const
Equals operator.
Definition: refcount_ptr.h:245
ReferenceCountable::ReferenceCountable
ReferenceCountable()
Default constructor, which initializes the reference count to zero.
Definition: refcount_ptr.h:71
ReferenceCountable
Base class for reference countable objects.
Definition: refcount_ptr.h:65

Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18