OgreSharedPtr.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __SharedPtr_H__
29 #define __SharedPtr_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreAtomicScalar.h"
33 
34 namespace Ogre {
44  {
50  SPFM_FREE
51  };
52 
53  struct SharedPtrInfo {
54  inline SharedPtrInfo()
55  : useCount(1)
56  {}
57 
58  virtual ~SharedPtrInfo() {}
59 
61  };
62 
63  template <class T>
65  {
66  T* mObject;
67  public:
68  inline SharedPtrInfoDelete(T* o) : mObject(o) {}
69 
71  {
73  }
74  };
75 
76  template <class T>
78  {
79  T* mObject;
80  public:
81  inline SharedPtrInfoDeleteT(T* o) : mObject(o) {}
82 
84  {
86  }
87  };
88 
89  template <class T>
91  {
92  T* mObject;
93  public:
94  inline SharedPtrInfoFree(T* o) : mObject(o) {}
95 
97  {
99  }
100  };
101 
102 
111  template<class T> class SharedPtr
112  {
113  template<typename Y> friend class SharedPtr;
114  protected:
115  /* DO NOT ADD MEMBERS TO THIS CLASS!
116  *
117  * The average Ogre application has *thousands* of them. Every extra
118  * member causes extra memory use in general, and causes extra padding
119  * to be added to a multitude of structures.
120  *
121  * Everything you need to do can be acomplished by creatively working
122  * with the SharedPtrInfo object.
123  *
124  * There is no reason for this object to ever have more than two members.
125  */
126 
127  T* pRep;
129 
130  SharedPtr(T* rep, SharedPtrInfo* info) : pRep(rep), pInfo(info)
131  {
132  }
133 
134  public:
139  SharedPtr() : pRep(0), pInfo(0)
140  {}
141 
142  private:
144  {
145  switch(method) {
149  }
150  assert(!"Bad method");
151  return 0;
152  }
153  public:
154 
159  template< class Y>
160  explicit SharedPtr(Y* rep, SharedPtrFreeMethod inFreeMethod = SPFM_DELETE)
161  : pRep(rep)
162  , pInfo(rep ? createInfoForMethod(rep, inFreeMethod) : 0)
163  {
164  }
165 
167  : pRep(r.pRep)
168  , pInfo(r.pInfo)
169  {
170  if (pRep)
171  {
172  ++pInfo->useCount;
173  }
174  }
175 
177  if (pRep == r.pRep)
178  {
179  assert(pInfo == r.pInfo);
180  return *this;
181  }
182  // Swap current data into a local copy
183  // this ensures we deal with rhs and this being dependent
184  SharedPtr<T> tmp(r);
185  swap(tmp);
186  return *this;
187  }
188 
189  /* For C++11 compilers, use enable_if to only expose functions when viable
190  *
191  * MSVC 2012 and earlier only claim conformance to C++98. This is fortunate,
192  * because they don't support default template parameters
193  */
194 #if __cplusplus >= 201103L
195  template<class Y,
196  class = typename std::enable_if<std::is_convertible<Y*, T*>::value>::type>
197 #else
198  template<class Y>
199 #endif
201  : pRep(r.getPointer())
202  , pInfo(r.pInfo)
203  {
204  if (pRep)
205  {
206  ++pInfo->useCount;
207  }
208  }
209 
210 
211 #if __cplusplus >= 201103L
212  template<class Y,
213  class = typename std::enable_if<std::is_assignable<T*, Y*>::value>::type>
214 #else
215  template<class Y>
216 #endif
218  {
219  if (pRep == r.pRep)
220  return *this;
221  // Swap current data into a local copy
222  // this ensures we deal with rhs and this being dependent
223  SharedPtr<T> tmp(r);
224  swap(tmp);
225  return *this;
226  }
227 
229  release();
230  }
231 
232 
233  template<typename Y>
234  inline SharedPtr<Y> staticCast() const
235  {
236  if(pRep) {
237  ++pInfo->useCount;
238  return SharedPtr<Y>(static_cast<Y*>(pRep), pInfo);
239  } else return SharedPtr<Y>();
240  }
241 
242  template<typename Y>
243  inline SharedPtr<Y> dynamicCast() const
244  {
245  if(pRep) {
246  Y* rep = dynamic_cast<Y*>(pRep);
247  ++pInfo->useCount;
248  return SharedPtr<Y>(rep, pInfo);
249  } else return SharedPtr<Y>();
250  }
251 
252  inline T& operator*() const { assert(pRep); return *pRep; }
253  inline T* operator->() const { assert(pRep); return pRep; }
254  inline T* get() const { return pRep; }
255 
263  void bind(T* rep, SharedPtrFreeMethod inFreeMethod = SPFM_DELETE) {
264  assert(!pRep && !pInfo);
265  pInfo = createInfoForMethod(rep, inFreeMethod);
266  pRep = rep;
267  }
268 
269  inline bool unique() const { assert(pInfo && pInfo->useCount.get()); return pInfo->useCount.get() == 1; }
270  inline unsigned int useCount() const { assert(pInfo && pInfo->useCount.get()); return pInfo->useCount.get(); }
271  inline void setUseCount(unsigned value) { assert(pInfo); pInfo->useCount = value; }
272 
273  inline T* getPointer() const { return pRep; }
274 
275  inline bool isNull(void) const { return pRep == 0; }
276 
277  inline void setNull(void) {
278  if (pRep)
279  {
280  release();
281  }
282  }
283 
284  protected:
285 
286  inline void release(void)
287  {
288  if (pRep)
289  {
290  assert(pInfo);
291  if(--pInfo->useCount == 0)
292  destroy();
293  }
294 
295  pRep = 0;
296  pInfo = 0;
297  }
298 
303  inline void destroy(void)
304  {
305  assert(pRep && pInfo);
307  }
308 
309  inline void swap(SharedPtr<T> &other)
310  {
311  std::swap(pRep, other.pRep);
312  std::swap(pInfo, other.pInfo);
313  }
314  };
315 
316  template<class T, class U> inline bool operator==(SharedPtr<T> const& a, SharedPtr<U> const& b)
317  {
318  return a.get() == b.get();
319  }
320 
321  template<class T, class U> inline bool operator!=(SharedPtr<T> const& a, SharedPtr<U> const& b)
322  {
323  return a.get() != b.get();
324  }
325 
326  template<class T, class U> inline bool operator<(SharedPtr<T> const& a, SharedPtr<U> const& b)
327  {
328  return std::less<const void*>()(a.get(), b.get());
329  }
332 }
333 
334 
335 
336 #endif
Ogre::SPFM_DELETE
@ SPFM_DELETE
Use OGRE_DELETE to free the memory.
Definition: OgreSharedPtr.h:46
OGRE_DELETE_T
#define OGRE_DELETE_T(ptr, T, category)
Free the memory allocated with OGRE_NEW_T. Category is required to be restated to ensure the matching...
Definition: OgreMemoryAllocatorConfig.h:437
Ogre::SharedPtr::setNull
void setNull(void)
Definition: OgreSharedPtr.h:277
Ogre::SharedPtrInfoFree::mObject
T * mObject
Definition: OgreSharedPtr.h:92
Ogre::SharedPtrInfoDelete::SharedPtrInfoDelete
SharedPtrInfoDelete(T *o)
Definition: OgreSharedPtr.h:68
Ogre::SharedPtrInfoDeleteT::~SharedPtrInfoDeleteT
virtual ~SharedPtrInfoDeleteT()
Definition: OgreSharedPtr.h:83
Ogre
Definition: OgreAndroidLogListener.h:35
Ogre::SharedPtrInfoDeleteT::mObject
T * mObject
Definition: OgreSharedPtr.h:79
Ogre::SPFM_FREE
@ SPFM_FREE
Use OGRE_FREE to free (only MEMCATEGORY_GENERAL supported)
Definition: OgreSharedPtr.h:50
Ogre::SPFM_DELETE_T
@ SPFM_DELETE_T
Use OGRE_DELETE_T to free (only MEMCATEGORY_GENERAL supported)
Definition: OgreSharedPtr.h:48
Ogre::SharedPtr::pInfo
SharedPtrInfo * pInfo
Definition: OgreSharedPtr.h:128
OGRE_FREE
#define OGRE_FREE(ptr, category)
Free the memory allocated with OGRE_MALLOC or OGRE_ALLOC_T. Category is required to be restated to en...
Definition: OgreMemoryAllocatorConfig.h:430
OGRE_DELETE
#define OGRE_DELETE
Definition: OgreMemoryAllocatorConfig.h:474
Ogre::AtomicScalar< unsigned >
Ogre::SharedPtr::pRep
T * pRep
Definition: OgreSharedPtr.h:127
Ogre::SharedPtr::operator->
T * operator->() const
Definition: OgreSharedPtr.h:253
Ogre::SharedPtr::release
void release(void)
Definition: OgreSharedPtr.h:286
Ogre::SharedPtr::operator=
SharedPtr & operator=(const SharedPtr &r)
Definition: OgreSharedPtr.h:176
Ogre::SharedPtr::SharedPtr
SharedPtr(const SharedPtr< Y > &r)
Definition: OgreSharedPtr.h:200
Ogre::SharedPtr::operator=
SharedPtr & operator=(const SharedPtr< Y > &r)
Definition: OgreSharedPtr.h:217
Ogre::SharedPtr::swap
void swap(SharedPtr< T > &other)
Definition: OgreSharedPtr.h:309
Ogre::SharedPtrInfo
Definition: OgreSharedPtr.h:53
Ogre::SharedPtr::useCount
unsigned int useCount() const
Definition: OgreSharedPtr.h:270
Ogre::operator!=
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
Definition: OgreMemorySTLAllocator.h:202
Ogre::SharedPtr::SharedPtr
SharedPtr(T *rep, SharedPtrInfo *info)
Definition: OgreSharedPtr.h:130
Ogre::SharedPtr::isNull
bool isNull(void) const
Definition: OgreSharedPtr.h:275
Ogre::SharedPtrInfo::useCount
AtomicScalar< unsigned > useCount
Definition: OgreSharedPtr.h:60
Ogre::SharedPtr::dynamicCast
SharedPtr< Y > dynamicCast() const
Definition: OgreSharedPtr.h:243
Ogre::SharedPtr::get
T * get() const
Definition: OgreSharedPtr.h:254
OgrePrerequisites.h
Ogre::SharedPtr::destroy
void destroy(void)
IF YOU GET A CRASH HERE, YOU FORGOT TO FREE UP POINTERS BEFORE SHUTTING OGRE DOWN Use setNull() befor...
Definition: OgreSharedPtr.h:303
Ogre::SharedPtr::bind
void bind(T *rep, SharedPtrFreeMethod inFreeMethod=SPFM_DELETE)
Binds rep to the SharedPtr.
Definition: OgreSharedPtr.h:263
Ogre::SharedPtrInfoDeleteT::SharedPtrInfoDeleteT
SharedPtrInfoDeleteT(T *o)
Definition: OgreSharedPtr.h:81
Ogre::SharedPtrInfoDelete::mObject
T * mObject
Definition: OgreSharedPtr.h:66
Ogre::SharedPtr::unique
bool unique() const
Definition: OgreSharedPtr.h:269
Ogre::SharedPtr::~SharedPtr
~SharedPtr()
Definition: OgreSharedPtr.h:228
Ogre::SharedPtrInfoFree
Definition: OgreSharedPtr.h:91
Ogre::SharedPtrInfo::SharedPtrInfo
SharedPtrInfo()
Definition: OgreSharedPtr.h:54
Ogre::AtomicScalar::get
T get(void) const
Definition: OgreAtomicScalar.h:417
Ogre::SharedPtrInfo::~SharedPtrInfo
virtual ~SharedPtrInfo()
Definition: OgreSharedPtr.h:58
Ogre::MEMCATEGORY_GENERAL
@ MEMCATEGORY_GENERAL
General purpose.
Definition: OgreMemoryAllocatorConfig.h:162
Ogre::SharedPtr::SharedPtr
SharedPtr(Y *rep, SharedPtrFreeMethod inFreeMethod=SPFM_DELETE)
Constructor.
Definition: OgreSharedPtr.h:160
Ogre::SharedPtrInfoFree::~SharedPtrInfoFree
virtual ~SharedPtrInfoFree()
Definition: OgreSharedPtr.h:96
Ogre::SharedPtrInfoDelete::~SharedPtrInfoDelete
virtual ~SharedPtrInfoDelete()
Definition: OgreSharedPtr.h:70
Ogre::SharedPtr
Reference-counted shared pointer, used for objects where implicit destruction is required.
Definition: OgreSharedPtr.h:112
Ogre::SharedPtr::SharedPtr
SharedPtr(const SharedPtr &r)
Definition: OgreSharedPtr.h:166
Ogre::SharedPtr::createInfoForMethod
static SharedPtrInfo * createInfoForMethod(T *rep, SharedPtrFreeMethod method)
Definition: OgreSharedPtr.h:143
std::swap
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: OgreSmallVector.h:802
Ogre::SharedPtrInfoDelete
Definition: OgreSharedPtr.h:65
Ogre::SharedPtr::staticCast
SharedPtr< Y > staticCast() const
Definition: OgreSharedPtr.h:234
Ogre::SharedPtrInfoFree::SharedPtrInfoFree
SharedPtrInfoFree(T *o)
Definition: OgreSharedPtr.h:94
Ogre::SharedPtr::setUseCount
void setUseCount(unsigned value)
Definition: OgreSharedPtr.h:271
Ogre::SharedPtr::SharedPtr
SharedPtr()
Constructor, does not initialise the SharedPtr.
Definition: OgreSharedPtr.h:139
Ogre::SharedPtrInfoDeleteT
Definition: OgreSharedPtr.h:78
Ogre::operator==
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
Definition: OgreMemorySTLAllocator.h:184
Ogre::SharedPtr::operator*
T & operator*() const
Definition: OgreSharedPtr.h:252
Ogre::operator<
bool operator<(SharedPtr< T > const &a, SharedPtr< U > const &b)
Definition: OgreSharedPtr.h:326
Ogre::SharedPtrFreeMethod
SharedPtrFreeMethod
The method to use to free memory on destruction.
Definition: OgreSharedPtr.h:44
OGRE_NEW_T
#define OGRE_NEW_T(T, category)
Allocate space for one primitive type, external type or non-virtual type with constructor parameters.
Definition: OgreMemoryAllocatorConfig.h:433
Ogre::SharedPtr::getPointer
T * getPointer() const
Definition: OgreSharedPtr.h:273
OgreAtomicScalar.h

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.