Ptr.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Allocator.h>
  7. namespace anki {
  8. /// @addtogroup util_memory
  9. /// @{
  10. /// Common code for all pointers
  11. template<typename T>
  12. class PtrBase
  13. {
  14. template<typename Y>
  15. friend class PtrBase;
  16. public:
  17. /// Dereference
  18. T& operator*() const
  19. {
  20. ANKI_ASSERT(m_ptr);
  21. return *m_ptr;
  22. }
  23. /// Dereference
  24. T* operator->() const
  25. {
  26. ANKI_ASSERT(m_ptr);
  27. return m_ptr;
  28. }
  29. T* get()
  30. {
  31. ANKI_ASSERT(m_ptr);
  32. return m_ptr;
  33. }
  34. const T* get() const
  35. {
  36. ANKI_ASSERT(m_ptr);
  37. return m_ptr;
  38. }
  39. Bool isCreated() const
  40. {
  41. return m_ptr != nullptr;
  42. }
  43. /// @name Compare operators
  44. /// @{
  45. explicit operator Bool() const
  46. {
  47. return isCreated();
  48. }
  49. Bool operator==(const PtrBase& other) const
  50. {
  51. return m_ptr == other.m_ptr;
  52. }
  53. Bool operator!=(const PtrBase& other) const
  54. {
  55. return m_ptr != other.m_ptr;
  56. }
  57. Bool operator<(const PtrBase& other) const
  58. {
  59. return m_ptr < other.m_ptr;
  60. }
  61. Bool operator<=(const PtrBase& other) const
  62. {
  63. return m_ptr <= other.m_ptr;
  64. }
  65. Bool operator>(const PtrBase& other) const
  66. {
  67. return m_ptr > other.m_ptr;
  68. }
  69. Bool operator>=(const PtrBase& other) const
  70. {
  71. return m_ptr >= other.m_ptr;
  72. }
  73. /// @}
  74. protected:
  75. T* m_ptr;
  76. PtrBase()
  77. : m_ptr(nullptr)
  78. {
  79. }
  80. PtrBase(T* ptr)
  81. : m_ptr(ptr)
  82. {
  83. }
  84. };
  85. /// A simple template class to wrap simple pointers.
  86. template<typename T>
  87. class WeakPtr : public PtrBase<T>
  88. {
  89. template<typename>
  90. friend class WeakPtr;
  91. public:
  92. using Base = PtrBase<T>;
  93. WeakPtr()
  94. : Base()
  95. {
  96. }
  97. explicit WeakPtr(T* ptr)
  98. : Base(ptr)
  99. {
  100. }
  101. WeakPtr(const WeakPtr& other)
  102. : Base(other.m_ptr)
  103. {
  104. }
  105. template<typename Y>
  106. WeakPtr(const WeakPtr<Y>& other)
  107. : Base(other.m_ptr)
  108. {
  109. }
  110. WeakPtr(WeakPtr&& other)
  111. : Base(other.m_ptr)
  112. {
  113. other.m_ptr = nullptr;
  114. }
  115. /// Destroy.
  116. ~WeakPtr()
  117. {
  118. Base::m_ptr = nullptr;
  119. }
  120. /// Copy.
  121. WeakPtr& operator=(const WeakPtr& other)
  122. {
  123. Base::m_ptr = other.m_ptr;
  124. return *this;
  125. }
  126. /// Copy.
  127. template<typename Y>
  128. WeakPtr& operator=(const WeakPtr<Y>& other)
  129. {
  130. Base::m_ptr = other.m_ptr;
  131. return *this;
  132. }
  133. /// Copy.
  134. WeakPtr& operator=(T* ptr)
  135. {
  136. Base::m_ptr = ptr;
  137. return *this;
  138. }
  139. /// Copy.
  140. template<typename Y>
  141. WeakPtr& operator=(Y* ptr)
  142. {
  143. Base::m_ptr = ptr;
  144. return *this;
  145. }
  146. /// Move.
  147. WeakPtr& operator=(WeakPtr&& other)
  148. {
  149. Base::m_ptr = other.m_ptr;
  150. other.m_ptr = nullptr;
  151. return *this;
  152. }
  153. /// Cast to the pointer type.
  154. operator T*()
  155. {
  156. return Base::m_ptr;
  157. }
  158. /// Cast to the pointer type.
  159. operator const T*() const
  160. {
  161. return Base::m_ptr;
  162. }
  163. /// @name Arithmetic operators
  164. /// @{
  165. WeakPtr& operator++()
  166. {
  167. ANKI_ASSERT(Base::m_ptr);
  168. ++Base::m_ptr;
  169. return *this;
  170. }
  171. WeakPtr& operator--()
  172. {
  173. ANKI_ASSERT(Base::m_ptr);
  174. --Base::m_ptr;
  175. return *this;
  176. }
  177. WeakPtr operator+(const WeakPtr& other) const
  178. {
  179. ANKI_ASSERT(Base::m_ptr);
  180. WeakPtr out(Base::m_ptr + other.m_ptr);
  181. return out;
  182. }
  183. WeakPtr& operator+=(const WeakPtr& other)
  184. {
  185. ANKI_ASSERT(Base::m_ptr);
  186. Base::m_ptr += other.m_ptr;
  187. return *this;
  188. }
  189. WeakPtr operator-(const WeakPtr& other) const
  190. {
  191. ANKI_ASSERT(Base::m_ptr);
  192. WeakPtr out(Base::m_ptr - other.m_ptr);
  193. return out;
  194. }
  195. WeakPtr& operator-=(const WeakPtr& other)
  196. {
  197. ANKI_ASSERT(Base::m_ptr);
  198. Base::m_ptr -= other.m_ptr;
  199. return *this;
  200. }
  201. T& operator[](const PtrSize i)
  202. {
  203. return Base::m_ptr[i];
  204. }
  205. const T& operator[](const PtrSize i) const
  206. {
  207. return Base::m_ptr[i];
  208. }
  209. /// @}
  210. };
  211. /// UniquePtr default deleter.
  212. template<typename T>
  213. class DefaultPtrDeleter
  214. {
  215. public:
  216. void operator()(T* x)
  217. {
  218. auto alloc = x->getAllocator();
  219. alloc.template deleteInstance<T>(x);
  220. }
  221. };
  222. /// UniquePtr alternative deleter.
  223. template<typename T>
  224. class AllocatorPtrDeleter
  225. {
  226. public:
  227. GenericMemoryPoolAllocator<U8> m_allocator;
  228. AllocatorPtrDeleter()
  229. {
  230. }
  231. AllocatorPtrDeleter(GenericMemoryPoolAllocator<U8> alloc)
  232. : m_allocator(alloc)
  233. {
  234. }
  235. void operator()(T* x)
  236. {
  237. m_allocator.template deleteInstance<T>(x);
  238. }
  239. };
  240. /// A unique pointer.
  241. template<typename T, typename TDeleter = DefaultPtrDeleter<T>>
  242. class UniquePtr : public PtrBase<T>
  243. {
  244. public:
  245. using Base = PtrBase<T>;
  246. using Deleter = TDeleter;
  247. using Base::m_ptr;
  248. UniquePtr()
  249. : Base()
  250. {
  251. }
  252. explicit UniquePtr(T* ptr, const Deleter& deleter = Deleter())
  253. : Base(ptr)
  254. , m_deleter(deleter)
  255. {
  256. }
  257. /// Non-copyable.
  258. UniquePtr(const UniquePtr& other) = delete;
  259. /// Move.
  260. UniquePtr(UniquePtr&& other)
  261. : Base()
  262. {
  263. move(other);
  264. }
  265. /// Destroy.
  266. ~UniquePtr()
  267. {
  268. destroy();
  269. }
  270. /// Non-copyable.
  271. UniquePtr& operator=(const UniquePtr& other) = delete;
  272. /// Move.
  273. UniquePtr& operator=(UniquePtr&& other)
  274. {
  275. move(other);
  276. return *this;
  277. }
  278. /// Set a new pointer. Will destroy the previous.
  279. void reset(T* ptr, const Deleter& deleter = Deleter())
  280. {
  281. destroy();
  282. m_ptr = ptr;
  283. m_deleter = deleter;
  284. }
  285. /// Move the ownership of the pointer outside the UniquePtr.
  286. void moveAndReset(T*& ptr)
  287. {
  288. ptr = m_ptr;
  289. m_ptr = nullptr;
  290. m_deleter = Deleter();
  291. }
  292. private:
  293. Deleter m_deleter;
  294. void destroy()
  295. {
  296. if(m_ptr)
  297. {
  298. m_deleter(m_ptr);
  299. m_ptr = nullptr;
  300. m_deleter = Deleter();
  301. }
  302. }
  303. void move(UniquePtr& b)
  304. {
  305. reset(b.m_ptr, b.m_deleter);
  306. b.m_ptr = nullptr;
  307. }
  308. };
  309. /// An intrusive pointer.
  310. template<typename T, typename TDeleter = DefaultPtrDeleter<T>>
  311. class IntrusivePtr : public PtrBase<T>
  312. {
  313. template<typename Y, typename TD>
  314. friend class IntrusivePtr;
  315. public:
  316. using Base = PtrBase<T>;
  317. using Deleter = TDeleter;
  318. using Base::m_ptr;
  319. IntrusivePtr()
  320. : Base()
  321. {
  322. }
  323. explicit IntrusivePtr(T* ptr)
  324. : Base()
  325. {
  326. reset(ptr);
  327. }
  328. /// Copy.
  329. IntrusivePtr(const IntrusivePtr& other)
  330. : Base()
  331. {
  332. reset(other.m_ptr);
  333. }
  334. /// Move.
  335. IntrusivePtr(IntrusivePtr&& other)
  336. : Base()
  337. {
  338. move(other);
  339. }
  340. /// Copy, compatible pointer.
  341. template<typename Y>
  342. IntrusivePtr(const IntrusivePtr<Y, TDeleter>& other)
  343. : Base()
  344. {
  345. reset(other.m_ptr);
  346. }
  347. /// Decrease refcount and delete the pointer if refcount is zero.
  348. ~IntrusivePtr()
  349. {
  350. destroy();
  351. }
  352. /// Copy.
  353. IntrusivePtr& operator=(const IntrusivePtr& other)
  354. {
  355. reset(other.m_ptr);
  356. return *this;
  357. }
  358. /// Move.
  359. IntrusivePtr& operator=(IntrusivePtr&& other)
  360. {
  361. destroy();
  362. move(other);
  363. return *this;
  364. }
  365. /// Copy, compatible.
  366. template<typename Y, typename YDeleter>
  367. IntrusivePtr& operator=(const IntrusivePtr<Y, YDeleter>& other)
  368. {
  369. reset(other.m_ptr);
  370. return *this;
  371. }
  372. /// Set a new pointer. Will destroy the previous.
  373. void reset(T* ptr)
  374. {
  375. if(ptr != m_ptr)
  376. {
  377. destroy();
  378. if(ptr)
  379. {
  380. ptr->getRefcount().fetchAdd(1);
  381. m_ptr = ptr;
  382. }
  383. }
  384. }
  385. private:
  386. void destroy()
  387. {
  388. if(m_ptr)
  389. {
  390. auto count = m_ptr->getRefcount().fetchSub(1);
  391. if(ANKI_UNLIKELY(count == 1))
  392. {
  393. TDeleter deleter;
  394. deleter(m_ptr);
  395. }
  396. m_ptr = nullptr;
  397. }
  398. }
  399. void move(IntrusivePtr& b)
  400. {
  401. ANKI_ASSERT(m_ptr == nullptr);
  402. m_ptr = b.m_ptr;
  403. b.m_ptr = nullptr;
  404. }
  405. };
  406. /// @}
  407. } // end namespace anki