OgreSharedPtr.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __SharedPtr_H__
  25. #define __SharedPtr_H__
  26. #include "OgrePrerequisites.h"
  27. namespace Ogre {
  28. /** \addtogroup Core
  29. * @{
  30. */
  31. /** \addtogroup General
  32. * @{
  33. */
  34. /// The method to use to free memory on destruction
  35. enum SharedPtrFreeMethod
  36. {
  37. /// Use OGRE_DELETE to free the memory
  38. SPFM_DELETE,
  39. /// Use OGRE_DELETE_T to free (only MEMCATEGORY_GENERAL supported)
  40. SPFM_DELETE_T,
  41. /// Use OGRE_FREE to free (only MEMCATEGORY_GENERAL supported)
  42. SPFM_FREE
  43. };
  44. /** Reference-counted shared pointer, used for objects where implicit destruction is
  45. required.
  46. @remarks
  47. This is a standard shared pointer implementation which uses a reference
  48. count to work out when to delete the object.
  49. @par
  50. If OGRE_THREAD_SUPPORT is defined to be 1, use of this class is thread-safe.
  51. */
  52. template<class T> class SharedPtr
  53. {
  54. protected:
  55. T* pRep;
  56. unsigned int* pUseCount;
  57. SharedPtrFreeMethod useFreeMethod; // if we should use OGRE_FREE instead of OGRE_DELETE
  58. public:
  59. OGRE_AUTO_SHARED_MUTEX // public to allow external locking
  60. /** Constructor, does not initialise the SharedPtr.
  61. @remarks
  62. <b>Dangerous!</b> You have to call bind() before using the SharedPtr.
  63. */
  64. SharedPtr() : pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
  65. {
  66. OGRE_SET_AUTO_SHARED_MUTEX_NULL
  67. }
  68. /** Constructor.
  69. @param rep The pointer to take ownership of
  70. @param freeMode The mechanism to use to free the pointer
  71. */
  72. template< class Y>
  73. explicit SharedPtr(Y* rep, SharedPtrFreeMethod inFreeMethod = SPFM_DELETE)
  74. : pRep(rep)
  75. , pUseCount(rep ? OGRE_NEW_T(unsigned int, MEMCATEGORY_GENERAL)(1) : 0)
  76. , useFreeMethod(inFreeMethod)
  77. {
  78. OGRE_SET_AUTO_SHARED_MUTEX_NULL
  79. if (rep)
  80. {
  81. OGRE_NEW_AUTO_SHARED_MUTEX
  82. }
  83. }
  84. SharedPtr(const SharedPtr& r)
  85. : pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
  86. {
  87. // lock & copy other mutex pointer
  88. OGRE_SET_AUTO_SHARED_MUTEX_NULL
  89. OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
  90. {
  91. OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
  92. OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
  93. pRep = r.pRep;
  94. pUseCount = r.pUseCount;
  95. useFreeMethod = r.useFreeMethod;
  96. // Handle zero pointer gracefully to manage STL containers
  97. if(pUseCount)
  98. {
  99. ++(*pUseCount);
  100. }
  101. }
  102. }
  103. SharedPtr& operator=(const SharedPtr& r) {
  104. if (pRep == r.pRep)
  105. return *this;
  106. // Swap current data into a local copy
  107. // this ensures we deal with rhs and this being dependent
  108. SharedPtr<T> tmp(r);
  109. swap(tmp);
  110. return *this;
  111. }
  112. template< class Y>
  113. SharedPtr(const SharedPtr<Y>& r)
  114. : pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
  115. {
  116. // lock & copy other mutex pointer
  117. OGRE_SET_AUTO_SHARED_MUTEX_NULL
  118. OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
  119. {
  120. OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
  121. OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
  122. pRep = r.getPointer();
  123. pUseCount = r.useCountPointer();
  124. useFreeMethod = r.freeMethod();
  125. // Handle zero pointer gracefully to manage STL containers
  126. if(pUseCount)
  127. {
  128. ++(*pUseCount);
  129. }
  130. }
  131. }
  132. template< class Y>
  133. SharedPtr& operator=(const SharedPtr<Y>& r) {
  134. if (pRep == r.getPointer())
  135. return *this;
  136. // Swap current data into a local copy
  137. // this ensures we deal with rhs and this being dependent
  138. SharedPtr<T> tmp(r);
  139. swap(tmp);
  140. return *this;
  141. }
  142. virtual ~SharedPtr() {
  143. release();
  144. }
  145. inline T& operator*() const { assert(pRep); return *pRep; }
  146. inline T* operator->() const { assert(pRep); return pRep; }
  147. inline T* get() const { return pRep; }
  148. /** Binds rep to the SharedPtr.
  149. @remarks
  150. Assumes that the SharedPtr is uninitialised!
  151. */
  152. void bind(T* rep, SharedPtrFreeMethod inFreeMethod = SPFM_DELETE) {
  153. assert(!pRep && !pUseCount);
  154. OGRE_NEW_AUTO_SHARED_MUTEX
  155. OGRE_LOCK_AUTO_SHARED_MUTEX
  156. pUseCount = OGRE_NEW_T(unsigned int, MEMCATEGORY_GENERAL)(1);
  157. pRep = rep;
  158. useFreeMethod = inFreeMethod;
  159. }
  160. inline bool unique() const { OGRE_LOCK_AUTO_SHARED_MUTEX assert(pUseCount); return *pUseCount == 1; }
  161. inline unsigned int useCount() const { OGRE_LOCK_AUTO_SHARED_MUTEX assert(pUseCount); return *pUseCount; }
  162. inline unsigned int* useCountPointer() const { return pUseCount; }
  163. inline T* getPointer() const { return pRep; }
  164. inline SharedPtrFreeMethod freeMethod() const { return useFreeMethod; }
  165. inline bool isNull(void) const { return pRep == 0; }
  166. inline void setNull(void) {
  167. if (pRep)
  168. {
  169. // can't scope lock mutex before release in case deleted
  170. release();
  171. pRep = 0;
  172. pUseCount = 0;
  173. }
  174. }
  175. protected:
  176. inline void release(void)
  177. {
  178. bool destroyThis = false;
  179. /* If the mutex is not initialized to a non-zero value, then
  180. neither is pUseCount nor pRep.
  181. */
  182. OGRE_MUTEX_CONDITIONAL(OGRE_AUTO_MUTEX_NAME)
  183. {
  184. // lock own mutex in limited scope (must unlock before destroy)
  185. OGRE_LOCK_AUTO_SHARED_MUTEX
  186. if (pUseCount)
  187. {
  188. if (--(*pUseCount) == 0)
  189. {
  190. destroyThis = true;
  191. }
  192. }
  193. }
  194. if (destroyThis)
  195. destroy();
  196. OGRE_SET_AUTO_SHARED_MUTEX_NULL
  197. }
  198. virtual void destroy(void)
  199. {
  200. // IF YOU GET A CRASH HERE, YOU FORGOT TO FREE UP POINTERS
  201. // BEFORE SHUTTING OGRE DOWN
  202. // Use setNull() before shutdown or make sure your pointer goes
  203. // out of scope before OGRE shuts down to avoid this.
  204. switch(useFreeMethod)
  205. {
  206. case SPFM_DELETE:
  207. OGRE_DELETE pRep;
  208. break;
  209. case SPFM_DELETE_T:
  210. OGRE_DELETE_T(pRep, T, MEMCATEGORY_GENERAL);
  211. break;
  212. case SPFM_FREE:
  213. OGRE_FREE(pRep, MEMCATEGORY_GENERAL);
  214. break;
  215. };
  216. // use OGRE_FREE instead of OGRE_DELETE_T since 'unsigned int' isn't a destructor
  217. // we only used OGRE_NEW_T to be able to use constructor
  218. OGRE_FREE(pUseCount, MEMCATEGORY_GENERAL);
  219. OGRE_DELETE_AUTO_SHARED_MUTEX
  220. }
  221. virtual void swap(SharedPtr<T> &other)
  222. {
  223. std::swap(pRep, other.pRep);
  224. std::swap(pUseCount, other.pUseCount);
  225. std::swap(useFreeMethod, other.useFreeMethod);
  226. #if OGRE_THREAD_SUPPORT
  227. std::swap(OGRE_AUTO_MUTEX_NAME, other.OGRE_AUTO_MUTEX_NAME);
  228. #endif
  229. }
  230. };
  231. template<class T, class U> inline bool operator==(SharedPtr<T> const& a, SharedPtr<U> const& b)
  232. {
  233. return a.get() == b.get();
  234. }
  235. template<class T, class U> inline bool operator!=(SharedPtr<T> const& a, SharedPtr<U> const& b)
  236. {
  237. return a.get() != b.get();
  238. }
  239. template<class T, class U> inline bool operator<(SharedPtr<T> const& a, SharedPtr<U> const& b)
  240. {
  241. return std::less<const void*>()(a.get(), b.get());
  242. }
  243. /** @} */
  244. /** @} */
  245. }
  246. #endif