FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
sharedptr.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 #ifndef FIFE_SHARED_PTR_H_
22 #define FIFE_SHARED_PTR_H_
23 
24 // Standard C++ library includes
25 #include <cassert>
26 #include <functional>
27 
28 // 3rd party library includes
29 
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "fife_stdint.h"
35 
36 namespace FIFE {
41  template <typename T>
42  class SharedPtr {
43  public:
44 
50  : m_ptr(0), m_refCount(0) {
51 
52  }
53 
59  template <typename U>
60  explicit SharedPtr(U *ptr)
61  : m_ptr(ptr), m_refCount(ptr ? new uint32_t(1) : 0) {
62 
63  }
64 
68  SharedPtr(const SharedPtr& rhs)
69  : m_ptr(rhs.m_ptr), m_refCount(rhs.m_refCount) {
70  // increase reference count
71  incRefCount();
72  }
73 
79  template <typename U>
80  SharedPtr(const SharedPtr<U>& rhs) {
81  m_ptr = rhs.get();
82  m_refCount = rhs.useCountPtr();
83  incRefCount();
84  }
85 
91  // decrement reference count
92  decRefCount();
93 
94  // check to see if we need to delete
95  if (m_refCount && *m_refCount == 0) {
96  // delete and set pointers to null
97  delete m_ptr;
98  delete m_refCount;
99  m_ptr = 0;
100  m_refCount = 0;
101  }
102  }
103 
107  // handle self assignment
108  if (rhs.get() == m_ptr) {
109  return *this;
110  }
111 
112  // store in temporary (which causes a ref count increase)
113  // and swap with this object
114  SharedPtr<T> temp(rhs);
115  swap(temp);
116  return *this;
117  }
118 
123  template <typename U>
125  // handle self assignment
126  if (rhs.get() == m_ptr) {
127  return *this;
128  }
129 
130  // store in temporary (which causes a ref count increase)
131  // and swap with this object
132  SharedPtr<T> temp(rhs);
133  swap(temp);
134  return *this;
135  }
136 
140  inline T& operator*() const {
141  assert(m_ptr);
142  return *m_ptr;
143  }
144 
148  inline T* operator->() const {
149  assert(m_ptr);
150  return m_ptr;
151  }
152 
155  inline T* get() const {
156  return m_ptr;
157  }
158 
164  inline void reset(T* ptr = 0) {
165  assert(ptr == 0 || ptr != m_ptr);
166  SharedPtr<T>(ptr).swap(*this);
167  }
168 
173  inline uint32_t useCount() const {
174  assert(m_refCount);
175 
176  if (!m_refCount) {
177  return 0;
178  }
179 
180  return *m_refCount;
181  }
182 
187  inline uint32_t* useCountPtr() const {
188  return m_refCount;
189  }
190 
197  inline bool unique() const {
198  assert(m_refCount);
199  return (*m_refCount == 1);
200  }
201 
207  operator bool() const {
208  return (m_ptr != 0);
209  }
210 
213  bool operator!() const {
214  return (m_ptr == 0);
215  }
216 
217  private:
218 
222  inline void swap(SharedPtr<T>& rhs) {
223  std::swap(m_ptr, rhs.m_ptr);
224  std::swap(m_refCount, rhs.m_refCount);
225  }
226 
230  inline void incRefCount() {
231  if (m_refCount) {
232  ++(*m_refCount);
233  }
234  }
235 
239  inline void decRefCount() {
240  if (m_refCount) {
241  --(*m_refCount);
242  }
243  }
244 
245  private:
246  T* m_ptr;
248  };
249 
252  template <typename T, typename U>
253  inline bool operator==(const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) {
254  return (lhs.get() == rhs.get());
255  }
256 
259  template <typename T, typename U>
260  inline bool operator!=(const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) {
261  return (lhs.get() != rhs.get());
262  }
263 
266  template<class T, class U>
267  inline bool operator<(SharedPtr<T> const& lhs, SharedPtr<U> const& rhs) {
268  return std::less<const void*>()(lhs.get(), rhs.get());
269  }
270 
274  template <typename T>
276  return SharedPtr<T>(ptr);
277  }
278 } //FIFE
279 
280 #endif //FIFE_SHARED_PTR_H_
SharedPtr< T > make_shared(T *ptr)
convenience function for making a shared pointer can be used anytime a shared pointer should be creat...
Definition: sharedptr.h:275
T * operator->() const
allows dereferencing of shared pointer to act identical to dereferencing the underlying pointer ...
Definition: sharedptr.h:148
uint32_t useCount() const
returns the current reference count this should only be called on a non-null shared pointer ...
Definition: sharedptr.h:173
SharedPtr(U *ptr)
Constructor takes over ownership of the provided pointer and will delete it automatically when last r...
Definition: sharedptr.h:60
void reset(T *ptr=0)
reset this pointer to a null shared pointer this can be used to lower the reference count of the shar...
Definition: sharedptr.h:164
uint32_t * m_refCount
Definition: sharedptr.h:247
void decRefCount()
decreases the reference count for this shared resource, used internally
Definition: sharedptr.h:239
SharedPtr(const SharedPtr< U > &rhs)
Constructor shares ownership with the value passed into rhs the pointer type passed in must be conver...
Definition: sharedptr.h:80
T & operator*() const
allows dereferencing of shared pointer to act identical to dereferencing the underlying pointer ...
Definition: sharedptr.h:140
void incRefCount()
increases the reference count for this shared resource, used internally
Definition: sharedptr.h:230
SharedPtr()
Constructor default constructor creates a null shared pointer.
Definition: sharedptr.h:49
void swap(SharedPtr< T > &rhs)
provides swapping function between two shared pointers, used internally
Definition: sharedptr.h:222
bool operator!=(const SharedPtr< T > &lhs, const SharedPtr< U > &rhs)
provides inequality operator for shared pointers
Definition: sharedptr.h:260
uint32_t * useCountPtr() const
returns the current reference count provides direct access to the user count pointer this should real...
Definition: sharedptr.h:187
~SharedPtr()
Destructor handles deletion of underlying pointer if the reference count reaches 0.
Definition: sharedptr.h:90
shared pointer implementation to provide automatic reference counting and deletion when last referenc...
Definition: sharedptr.h:42
bool operator!() const
negation operator overload
Definition: sharedptr.h:213
bool operator==(const SharedPtr< T > &lhs, const SharedPtr< U > &rhs)
provides equality operator for shared pointers
Definition: sharedptr.h:253
SharedPtr & operator=(const SharedPtr &rhs)
provides functionality for the equality operator
Definition: sharedptr.h:106
SharedPtr(const SharedPtr &rhs)
Copy Constructor provides ability to properly copy a shared resource.
Definition: sharedptr.h:68
bool unique() const
provides the ability to see if a shared resource is currently only held by a single shared pointer th...
Definition: sharedptr.h:197
T * get() const
allows direct access to underlying pointer
Definition: sharedptr.h:155
SharedPtr & operator=(const SharedPtr< U > &rhs)
provides functionality for the equality operator the passed in pointer type must be convertible to th...
Definition: sharedptr.h:124
unsigned int uint32_t
Definition: core.h:40