CCRefPtr.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /****************************************************************************
  2. Copyright (c) 2014 PlayFirst Inc.
  3. Copyright (c) 2014-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_REF_PTR_H__
  23. #define __CC_REF_PTR_H__
  24. /// @cond DO_NOT_SHOW
  25. #include "base/CCRef.h"
  26. #include "base/ccMacros.h"
  27. #include <functional>
  28. #include <type_traits>
  29. NS_CC_BEGIN
  30. /**
  31. * Utility/support macros. Defined to enable RefPtr<T> to contain types like 'const T' because we do not
  32. * regard retain()/release() as affecting mutability of state.
  33. */
  34. #define CC_REF_PTR_SAFE_RETAIN(ptr)\
  35. \
  36. do\
  37. {\
  38. if (ptr)\
  39. {\
  40. const_cast<Ref*>(static_cast<const Ref*>(ptr))->retain();\
  41. }\
  42. \
  43. } while (0);
  44. #define CC_REF_PTR_SAFE_RELEASE(ptr)\
  45. \
  46. do\
  47. {\
  48. if (ptr)\
  49. {\
  50. const_cast<Ref*>(static_cast<const Ref*>(ptr))->release();\
  51. }\
  52. \
  53. } while (0);
  54. #define CC_REF_PTR_SAFE_RELEASE_NULL(ptr)\
  55. \
  56. do\
  57. {\
  58. if (ptr)\
  59. {\
  60. const_cast<Ref*>(static_cast<const Ref*>(ptr))->release();\
  61. ptr = nullptr;\
  62. }\
  63. \
  64. } while (0);
  65. /**
  66. * Wrapper class which maintains a strong reference to a cocos2dx cocos2d::Ref* type object.
  67. * Similar in concept to a boost smart pointer.
  68. *
  69. * Enables the use of the RAII idiom with Cocos2dx objects and helps automate some of the more
  70. * mundane tasks of pointer initialization and cleanup.
  71. *
  72. * The class itself is modelled on C++ 11 std::shared_ptr, and trys to keep some of the methods
  73. * and functionality consistent with std::shared_ptr.
  74. */
  75. template <typename T> class RefPtr
  76. {
  77. public:
  78. RefPtr()
  79. : _ptr(nullptr)
  80. {
  81. }
  82. RefPtr(RefPtr<T> && other)
  83. {
  84. _ptr = other._ptr;
  85. other._ptr = nullptr;
  86. }
  87. RefPtr(T * ptr)
  88. : _ptr(ptr)
  89. {
  90. CC_REF_PTR_SAFE_RETAIN(_ptr);
  91. }
  92. RefPtr(std::nullptr_t ptr)
  93. : _ptr(nullptr)
  94. {
  95. }
  96. RefPtr(const RefPtr<T> & other)
  97. : _ptr(other._ptr)
  98. {
  99. CC_REF_PTR_SAFE_RETAIN(_ptr);
  100. }
  101. ~RefPtr()
  102. {
  103. CC_REF_PTR_SAFE_RELEASE_NULL(_ptr);
  104. }
  105. RefPtr<T> & operator = (const RefPtr<T> & other)
  106. {
  107. if (other._ptr != _ptr)
  108. {
  109. CC_REF_PTR_SAFE_RETAIN(other._ptr);
  110. CC_REF_PTR_SAFE_RELEASE(_ptr);
  111. _ptr = other._ptr;
  112. }
  113. return *this;
  114. }
  115. RefPtr<T> & operator = (RefPtr<T> && other)
  116. {
  117. if (&other != this)
  118. {
  119. CC_REF_PTR_SAFE_RELEASE(_ptr);
  120. _ptr = other._ptr;
  121. other._ptr = nullptr;
  122. }
  123. return *this;
  124. }
  125. RefPtr<T> & operator = (T * other)
  126. {
  127. if (other != _ptr)
  128. {
  129. CC_REF_PTR_SAFE_RETAIN(other);
  130. CC_REF_PTR_SAFE_RELEASE(_ptr);
  131. _ptr = other;
  132. }
  133. return *this;
  134. }
  135. RefPtr<T> & operator = (std::nullptr_t other)
  136. {
  137. CC_REF_PTR_SAFE_RELEASE_NULL(_ptr);
  138. return *this;
  139. }
  140. operator T * () const { return _ptr; }
  141. T & operator * () const
  142. {
  143. CCASSERT(_ptr, "Attempt to dereference a null pointer!");
  144. return *_ptr;
  145. }
  146. T * operator->() const
  147. {
  148. CCASSERT(_ptr, "Attempt to dereference a null pointer!");
  149. return _ptr;
  150. }
  151. T * get() const { return _ptr; }
  152. bool operator == (const RefPtr<T> & other) const { return _ptr == other._ptr; }
  153. bool operator == (const T * other) const { return _ptr == other; }
  154. bool operator == (typename std::remove_const<T>::type * other) const { return _ptr == other; }
  155. bool operator == (const std::nullptr_t other) const { return _ptr == other; }
  156. bool operator != (const RefPtr<T> & other) const { return _ptr != other._ptr; }
  157. bool operator != (const T * other) const { return _ptr != other; }
  158. bool operator != (typename std::remove_const<T>::type * other) const { return _ptr != other; }
  159. bool operator != (const std::nullptr_t other) const { return _ptr != other; }
  160. bool operator > (const RefPtr<T> & other) const { return _ptr > other._ptr; }
  161. bool operator > (const T * other) const { return _ptr > other; }
  162. bool operator > (typename std::remove_const<T>::type * other) const { return _ptr > other; }
  163. bool operator < (const RefPtr<T> & other) const { return _ptr < other._ptr; }
  164. bool operator < (const T * other) const { return _ptr < other; }
  165. bool operator < (typename std::remove_const<T>::type * other) const { return _ptr < other; }
  166. bool operator >= (const RefPtr<T> & other) const { return _ptr >= other._ptr; }
  167. bool operator >= (const T * other) const { return _ptr >= other; }
  168. bool operator >= (typename std::remove_const<T>::type * other) const { return _ptr >= other; }
  169. bool operator <= (const RefPtr<T> & other) const { return _ptr <= other._ptr; }
  170. bool operator <= (const T * other) const { return _ptr <= other; }
  171. bool operator <= (typename std::remove_const<T>::type * other) const { return _ptr <= other; }
  172. operator bool() const { return _ptr != nullptr; }
  173. void reset()
  174. {
  175. CC_REF_PTR_SAFE_RELEASE_NULL(_ptr);
  176. }
  177. void swap(RefPtr<T> & other)
  178. {
  179. if (&other != this)
  180. {
  181. T * tmp = _ptr;
  182. _ptr = other._ptr;
  183. other._ptr = tmp;
  184. }
  185. }
  186. /**
  187. * This function assigns to this RefPtr<T> but does not increase the reference count of the object pointed to.
  188. * Useful for assigning an object created through the 'new' operator to a RefPtr<T>. Basically used in scenarios
  189. * where the RefPtr<T> has the initial ownership of the object.
  190. *
  191. * E.G:
  192. * RefPtr<cocos2d::Image> image;
  193. * image.weakAssign(new cocos2d::Image());
  194. *
  195. * Instead of:
  196. * RefPtr<cocos2d::Image> image;
  197. * image = new cocos2d::Image();
  198. * image->release(); // Required because new'd object already has a reference count of '1'.
  199. */
  200. void weakAssign(const RefPtr<T> & other)
  201. {
  202. CC_REF_PTR_SAFE_RELEASE(_ptr);
  203. _ptr = other._ptr;
  204. }
  205. private:
  206. T * _ptr;
  207. // NOTE: We can ensure T is derived from cocos2d::Ref at compile time here.
  208. static_assert(std::is_base_of<Ref, typename std::remove_const<T>::type>::value, "T must be derived from Ref");
  209. };
  210. template <class T> inline
  211. RefPtr<T> makeRef(T *ptr)
  212. {
  213. return RefPtr<T>(ptr);
  214. }
  215. template<class T> inline
  216. bool operator<(const RefPtr<T>& r, std::nullptr_t)
  217. {
  218. return std::less<T*>()(r.get(), nullptr);
  219. }
  220. template<class T> inline
  221. bool operator<(std::nullptr_t, const RefPtr<T>& r)
  222. {
  223. return std::less<T*>()(nullptr, r.get());
  224. }
  225. template<class T> inline
  226. bool operator>(const RefPtr<T>& r, std::nullptr_t)
  227. {
  228. return nullptr < r;
  229. }
  230. template<class T> inline
  231. bool operator>(std::nullptr_t, const RefPtr<T>& r)
  232. {
  233. return r < nullptr;
  234. }
  235. template<class T> inline
  236. bool operator<=(const RefPtr<T>& r, std::nullptr_t)
  237. {
  238. return !(nullptr < r);
  239. }
  240. template<class T> inline
  241. bool operator<=(std::nullptr_t, const RefPtr<T>& r)
  242. {
  243. return !(r < nullptr);
  244. }
  245. template<class T> inline
  246. bool operator>=(const RefPtr<T>& r, std::nullptr_t)
  247. {
  248. return !(r < nullptr);
  249. }
  250. template<class T> inline
  251. bool operator>=(std::nullptr_t, const RefPtr<T>& r)
  252. {
  253. return !(nullptr < r);
  254. }
  255. /**
  256. * Cast between RefPtr types statically.
  257. */
  258. template<class T, class U> RefPtr<T> static_pointer_cast(const RefPtr<U> & r)
  259. {
  260. return RefPtr<T>(static_cast<T*>(r.get()));
  261. }
  262. /**
  263. * Cast between RefPtr types dynamically.
  264. */
  265. template<class T, class U> RefPtr<T> dynamic_pointer_cast(const RefPtr<U> & r)
  266. {
  267. return RefPtr<T>(dynamic_cast<T*>(r.get()));
  268. }
  269. /**
  270. * Done with these macros.
  271. */
  272. #undef CC_REF_PTR_SAFE_RETAIN
  273. #undef CC_REF_PTR_SAFE_RELEASE
  274. #undef CC_REF_PTR_SAFE_RELEASE_NULL
  275. NS_CC_END
  276. /// @endcond
  277. #endif // __CC_REF_PTR_H__