smartptr.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_CRYCOMMON_SMARTPTR_H
  9. #define CRYINCLUDE_CRYCOMMON_SMARTPTR_H
  10. #pragma once
  11. #include <platform.h>
  12. #include <type_traits>
  13. void CryFatalError(const char*, ...) PRINTF_PARAMS(1, 2);
  14. #if defined(APPLE)
  15. #include <cstddef>
  16. #endif
  17. #include <AzCore/std/parallel/atomic.h>
  18. //////////////////////////////////////////////////////////////////
  19. // SMART POINTER
  20. //////////////////////////////////////////////////////////////////
  21. template <class _I>
  22. class _smart_ptr
  23. {
  24. private:
  25. _I* p;
  26. public:
  27. _smart_ptr()
  28. : p(NULL) {}
  29. _smart_ptr(_I* p_)
  30. {
  31. p = p_;
  32. if (p)
  33. {
  34. p->AddRef();
  35. }
  36. }
  37. _smart_ptr(const _smart_ptr& p_)
  38. {
  39. p = p_.p;
  40. if (p)
  41. {
  42. p->AddRef();
  43. }
  44. }
  45. template <typename _Y>
  46. _smart_ptr(const _smart_ptr<_Y>& p_)
  47. {
  48. p = p_.get();
  49. if (p)
  50. {
  51. p->AddRef();
  52. }
  53. }
  54. ~_smart_ptr()
  55. {
  56. if (p)
  57. {
  58. p->Release();
  59. }
  60. }
  61. operator _I*() const {
  62. return p;
  63. }
  64. _I& operator*() const { return *p; }
  65. _I* operator->(void) const { return p; }
  66. _I* get() const { return p; }
  67. _smart_ptr& operator=(_I* newp)
  68. {
  69. if (newp)
  70. {
  71. newp->AddRef();
  72. }
  73. if (p)
  74. {
  75. p->Release();
  76. }
  77. p = newp;
  78. return *this;
  79. }
  80. void reset()
  81. {
  82. _smart_ptr<_I>().swap(*this);
  83. }
  84. void reset(_I* ptr)
  85. {
  86. _smart_ptr<_I>(ptr).swap(*this);
  87. }
  88. void attach(_I* p_)
  89. {
  90. p = p_;
  91. }
  92. _smart_ptr& operator=(const _smart_ptr& newp)
  93. {
  94. if (newp.p)
  95. {
  96. newp.p->AddRef();
  97. }
  98. if (p)
  99. {
  100. p->Release();
  101. }
  102. p = newp.p;
  103. return *this;
  104. }
  105. template <typename _Y>
  106. _smart_ptr& operator=(const _smart_ptr<_Y>& newp)
  107. {
  108. _I* const p2 = newp.get();
  109. if (p2)
  110. {
  111. p2->AddRef();
  112. }
  113. if (p)
  114. {
  115. p->Release();
  116. }
  117. p = p2;
  118. return *this;
  119. }
  120. // set our contained pointer to null, but don't call Release()
  121. // useful for when we want to do that ourselves, or when we know that
  122. // the contained pointer is invalid
  123. friend _I* ReleaseOwnership(_smart_ptr<_I>& ptr)
  124. {
  125. _I* ret = ptr.p;
  126. ptr.p = 0;
  127. return ret;
  128. }
  129. void swap(_smart_ptr<_I>& other)
  130. {
  131. std::swap(p, other.p);
  132. }
  133. };
  134. template <typename T>
  135. ILINE void swap(_smart_ptr<T>& a, _smart_ptr<T>& b)
  136. {
  137. a.swap(b);
  138. }
  139. // reference target without vtable for smart pointer
  140. // implements AddRef() and Release() strategy using reference counter of the specified type
  141. template <typename TDerived, typename Counter = int>
  142. class _reference_target_no_vtable
  143. {
  144. public:
  145. _reference_target_no_vtable()
  146. : m_nRefCounter (0)
  147. {
  148. }
  149. ~_reference_target_no_vtable()
  150. {
  151. //assert (!m_nRefCounter);
  152. }
  153. void AddRef()
  154. {
  155. AZ_Assert(m_nRefCounter >= 0, "Invalid ref count");
  156. ++m_nRefCounter;
  157. }
  158. void Release()
  159. {
  160. AZ_Assert(m_nRefCounter > 0, "Invalid ref count");
  161. if (--m_nRefCounter == 0)
  162. {
  163. delete static_cast<TDerived*>(this);
  164. }
  165. else if (m_nRefCounter < 0)
  166. {
  167. assert(0);
  168. CryFatalError("Deleting Reference Counted Object Twice");
  169. }
  170. }
  171. // Warning: use for debugging/statistics purposes only!
  172. Counter NumRefs()
  173. {
  174. return m_nRefCounter;
  175. }
  176. protected:
  177. Counter m_nRefCounter;
  178. };
  179. // reference target with vtable for smart pointer
  180. // implements AddRef() and Release() strategy using reference counter of the specified type
  181. template <typename Counter>
  182. class _reference_target
  183. {
  184. public:
  185. _reference_target()
  186. : m_nRefCounter (0)
  187. {
  188. }
  189. virtual ~_reference_target()
  190. {
  191. //assert (!m_nRefCounter);
  192. }
  193. void AddRef()
  194. {
  195. AZ_Assert(m_nRefCounter >= 0, "Invalid ref count");
  196. ++m_nRefCounter;
  197. }
  198. void Release()
  199. {
  200. AZ_Assert(m_nRefCounter > 0, "Invalid ref count");
  201. if (--m_nRefCounter == 0)
  202. {
  203. delete this;
  204. }
  205. else if (m_nRefCounter < 0)
  206. {
  207. assert(0);
  208. CryFatalError("Deleting Reference Counted Object Twice");
  209. }
  210. }
  211. // Warning: use for debugging/statistics purposes only!
  212. Counter NumRefs()
  213. {
  214. return m_nRefCounter;
  215. }
  216. protected:
  217. Counter m_nRefCounter;
  218. };
  219. // default implementation is int counter - for better alignment
  220. typedef _reference_target<int> _reference_target_t;
  221. // reference target for smart pointer with configurable destruct function
  222. // implements AddRef() and Release() strategy using reference counter of the specified type
  223. template <typename T, typename Counter = int>
  224. class _cfg_reference_target
  225. {
  226. public:
  227. typedef void (* DeleteFncPtr)(void*);
  228. _cfg_reference_target()
  229. : m_nRefCounter (0)
  230. , m_pDeleteFnc(operator delete)
  231. {
  232. }
  233. explicit _cfg_reference_target(DeleteFncPtr pDeleteFnc)
  234. : m_nRefCounter (0)
  235. , m_pDeleteFnc(pDeleteFnc)
  236. {
  237. }
  238. virtual ~_cfg_reference_target()
  239. {
  240. }
  241. void AddRef()
  242. {
  243. AZ_Assert(m_nRefCounter >= 0, "Invalid ref count");
  244. ++m_nRefCounter;
  245. }
  246. void Release()
  247. {
  248. AZ_Assert(m_nRefCounter > 0, "Invalid ref count");
  249. if (--m_nRefCounter == 0)
  250. {
  251. assert(m_pDeleteFnc);
  252. static_cast<T*>(this)->~T();
  253. m_pDeleteFnc(this);
  254. }
  255. else if (m_nRefCounter < 0)
  256. {
  257. assert(0);
  258. CryFatalError("Deleting Reference Counted Object Twice");
  259. }
  260. }
  261. // Sets the delete function with which this object is supposed to be deleted
  262. void SetDeleteFnc(DeleteFncPtr pDeleteFnc) { m_pDeleteFnc = pDeleteFnc; }
  263. // Warning: use for debugging/statistics purposes only!
  264. Counter NumRefs()
  265. {
  266. return m_nRefCounter;
  267. }
  268. protected:
  269. Counter m_nRefCounter;
  270. DeleteFncPtr m_pDeleteFnc;
  271. };
  272. // base class for interfaces implementing reference counting
  273. // derive your interface from this class and the descendants won't have to implement
  274. // the reference counting logic
  275. template <typename Counter>
  276. class _i_reference_target
  277. {
  278. public:
  279. _i_reference_target()
  280. : m_nRefCounter (0)
  281. {
  282. }
  283. virtual ~_i_reference_target()
  284. {
  285. }
  286. virtual void AddRef()
  287. {
  288. ++m_nRefCounter;
  289. }
  290. virtual void Release()
  291. {
  292. if (--m_nRefCounter == 0)
  293. {
  294. delete this;
  295. }
  296. else if (m_nRefCounter < 0)
  297. {
  298. assert(0);
  299. CryFatalError("Deleting Reference Counted Object Twice");
  300. }
  301. }
  302. // Warning: use for debugging/statistics purposes only!
  303. Counter NumRefs() const
  304. {
  305. return m_nRefCounter;
  306. }
  307. protected:
  308. Counter m_nRefCounter;
  309. };
  310. class CMultiThreadRefCount
  311. {
  312. public:
  313. CMultiThreadRefCount() {}
  314. virtual ~CMultiThreadRefCount() {}
  315. inline int AddRef()
  316. {
  317. return m_count.fetch_add(1, AZStd::memory_order_acq_rel) + 1; // because we get the original value back
  318. }
  319. inline int Release()
  320. {
  321. const int nCount = m_count.fetch_sub(1, AZStd::memory_order_acq_rel) - 1; // because we get the original value back
  322. AZ_Assert(nCount >= 0, "Deleting Reference Counted Object Twice");
  323. if (nCount == 0)
  324. {
  325. delete this;
  326. }
  327. return nCount;
  328. }
  329. inline int GetRefCount() const { return m_count.load(AZStd::memory_order_acquire); }
  330. protected:
  331. // Allows the memory for the object to be deallocated in the dynamic module where it was originally constructed, as it may use different memory manager (Debug/Release configurations)
  332. virtual void DeleteThis() { delete this; }
  333. private:
  334. AZStd::atomic_int m_count{ 0 };
  335. };
  336. // base class for interfaces implementing reference counting that needs to be thread-safe
  337. // derive your interface from this class and the descendants won't have to implement
  338. // the reference counting logic
  339. template <typename Counter>
  340. class _i_multithread_reference_target
  341. {
  342. public:
  343. _i_multithread_reference_target()
  344. : m_nRefCounter(0)
  345. {
  346. }
  347. virtual ~_i_multithread_reference_target()
  348. {
  349. }
  350. virtual void AddRef()
  351. {
  352. m_nRefCounter.fetch_add(1, AZStd::memory_order_acq_rel);
  353. }
  354. virtual void Release()
  355. {
  356. const int nCount = m_nRefCounter.fetch_sub(1, AZStd::memory_order_acq_rel) - 1; // because we get the original value back
  357. AZ_Assert(nCount >= 0, "Deleting Reference Counted Object Twice");
  358. if (nCount == 0)
  359. {
  360. delete this;
  361. }
  362. }
  363. Counter NumRefs() const { return m_nRefCounter.load(AZStd::memory_order_acquire); }
  364. protected:
  365. AZStd::atomic<Counter> m_nRefCounter{ 0 };
  366. };
  367. typedef _i_reference_target<int> _i_reference_target_t;
  368. typedef _i_multithread_reference_target<int> _i_multithread_reference_target_t;
  369. // TYPEDEF_AUTOPTR macro, declares Class_AutoPtr, which is the smart pointer to the given class,
  370. // and Class_AutoArray, which is the array(STL vector) of autopointers
  371. #ifdef ENABLE_NAIIVE_AUTOPTR
  372. // naiive autopointer makes it easier for Visual Assist to parse the declaration and sometimes is easier for debug
  373. #define TYPEDEF_AUTOPTR(T) typedef T* T##_AutoPtr; typedef std::vector<T##_AutoPtr> T##_AutoArray;
  374. #else
  375. #define TYPEDEF_AUTOPTR(T) typedef _smart_ptr<T> T##_AutoPtr; typedef std::vector<T##_AutoPtr> T##_AutoArray;
  376. #endif
  377. #endif // CRYINCLUDE_CRYCOMMON_SMARTPTR_H