Ptr.h 5.8 KB

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