CCRefPtr.h 9.1 KB

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