ArrayPtr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/HashBase.h"
  5. #include "../Container/RefCounted.h"
  6. #include <cassert>
  7. namespace Urho3D
  8. {
  9. /// Shared array pointer template class. Uses non-intrusive reference counting.
  10. template <class T> class SharedArrayPtr
  11. {
  12. public:
  13. /// Construct a null shared array pointer.
  14. SharedArrayPtr() :
  15. ptr_(nullptr),
  16. refCount_(nullptr)
  17. {
  18. }
  19. /// Copy-construct from another shared array pointer.
  20. SharedArrayPtr(const SharedArrayPtr<T>& rhs) :
  21. ptr_(rhs.ptr_),
  22. refCount_(rhs.refCount_)
  23. {
  24. AddRef();
  25. }
  26. /// Construct from a raw pointer.
  27. explicit SharedArrayPtr(T* ptr) :
  28. ptr_(ptr),
  29. refCount_(new RefCount())
  30. {
  31. AddRef();
  32. }
  33. /// Destruct. Release the array reference.
  34. ~SharedArrayPtr()
  35. {
  36. ReleaseRef();
  37. }
  38. /// Assign from another shared array pointer.
  39. SharedArrayPtr<T>& operator =(const SharedArrayPtr<T>& rhs)
  40. {
  41. if (ptr_ == rhs.ptr_)
  42. return *this;
  43. ReleaseRef();
  44. ptr_ = rhs.ptr_;
  45. refCount_ = rhs.refCount_;
  46. AddRef();
  47. return *this;
  48. }
  49. /// Assign from a raw pointer.
  50. SharedArrayPtr<T>& operator =(T* ptr)
  51. {
  52. if (ptr_ == ptr)
  53. return *this;
  54. ReleaseRef();
  55. if (ptr)
  56. {
  57. ptr_ = ptr;
  58. refCount_ = new RefCount();
  59. AddRef();
  60. }
  61. return *this;
  62. }
  63. /// Point to the array.
  64. T* operator ->() const
  65. {
  66. assert(ptr_);
  67. return ptr_;
  68. }
  69. /// Dereference the array.
  70. T& operator *() const
  71. {
  72. assert(ptr_);
  73. return *ptr_;
  74. }
  75. /// Subscript the array.
  76. T& operator [](int index)
  77. {
  78. assert(ptr_);
  79. return ptr_[index];
  80. }
  81. /// Test for equality with another shared array pointer.
  82. bool operator ==(const SharedArrayPtr<T>& rhs) const { return ptr_ == rhs.ptr_; }
  83. /// Test for inequality with another shared array pointer.
  84. bool operator !=(const SharedArrayPtr<T>& rhs) const { return ptr_ != rhs.ptr_; }
  85. /// Test for less than with another array pointer.
  86. bool operator <(const SharedArrayPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
  87. /// Convert to a raw pointer.
  88. operator T*() const { return ptr_; } // NOLINT(google-explicit-constructor)
  89. /// Reset to null and release the array reference.
  90. void Reset() { ReleaseRef(); }
  91. /// Perform a static cast from a shared array pointer of another type.
  92. template <class U> void StaticCast(const SharedArrayPtr<U>& rhs)
  93. {
  94. ReleaseRef();
  95. ptr_ = static_cast<T*>(rhs.Get());
  96. refCount_ = rhs.RefCountPtr();
  97. AddRef();
  98. }
  99. /// Perform a reinterpret cast from a shared array pointer of another type.
  100. template <class U> void ReinterpretCast(const SharedArrayPtr<U>& rhs)
  101. {
  102. ReleaseRef();
  103. ptr_ = reinterpret_cast<T*>(rhs.Get());
  104. refCount_ = rhs.RefCountPtr();
  105. AddRef();
  106. }
  107. /// Check if the pointer is null.
  108. bool Null() const { return ptr_ == 0; }
  109. /// Check if the pointer is not null.
  110. bool NotNull() const { return ptr_ != 0; }
  111. /// Return the raw pointer.
  112. T* Get() const { return ptr_; }
  113. /// Return the array's reference count, or 0 if the pointer is null.
  114. int Refs() const { return refCount_ ? refCount_->refs_ : 0; }
  115. /// Return the array's weak reference count, or 0 if the pointer is null.
  116. int WeakRefs() const { return refCount_ ? refCount_->weakRefs_ : 0; }
  117. /// Return pointer to the RefCount structure.
  118. RefCount* RefCountPtr() const { return refCount_; }
  119. /// Return hash value for HashSet & HashMap.
  120. hash32 ToHash() const { return (hash32)((size_t)ptr_ / sizeof(T)); }
  121. private:
  122. /// Prevent direct assignment from a shared array pointer of different type.
  123. template <class U> SharedArrayPtr<T>& operator =(const SharedArrayPtr<U>& rhs);
  124. /// Add a reference to the array pointed to.
  125. void AddRef()
  126. {
  127. if (refCount_)
  128. {
  129. assert(refCount_->refs_ >= 0);
  130. ++(refCount_->refs_);
  131. }
  132. }
  133. /// Release the array reference and delete it and the RefCount structure if necessary.
  134. void ReleaseRef()
  135. {
  136. if (refCount_)
  137. {
  138. assert(refCount_->refs_ > 0);
  139. --(refCount_->refs_);
  140. if (!refCount_->refs_)
  141. {
  142. refCount_->refs_ = -1;
  143. delete[] ptr_;
  144. }
  145. if (refCount_->refs_ < 0 && !refCount_->weakRefs_)
  146. delete refCount_;
  147. }
  148. ptr_ = nullptr;
  149. refCount_ = nullptr;
  150. }
  151. /// Pointer to the array.
  152. T* ptr_;
  153. /// Pointer to the RefCount structure.
  154. RefCount* refCount_;
  155. };
  156. /// Perform a static cast from one shared array pointer type to another.
  157. template <class T, class U> SharedArrayPtr<T> StaticCast(const SharedArrayPtr<U>& ptr)
  158. {
  159. SharedArrayPtr<T> ret;
  160. ret.StaticCast(ptr);
  161. return ret;
  162. }
  163. /// Perform a reinterpret cast from one shared array pointer type to another.
  164. template <class T, class U> SharedArrayPtr<T> ReinterpretCast(const SharedArrayPtr<U>& ptr)
  165. {
  166. SharedArrayPtr<T> ret;
  167. ret.ReinterpretCast(ptr);
  168. return ret;
  169. }
  170. /// Weak array pointer template class. Uses non-intrusive reference counting.
  171. template <class T> class WeakArrayPtr
  172. {
  173. public:
  174. /// Construct a null weak array pointer.
  175. WeakArrayPtr() :
  176. ptr_(0),
  177. refCount_(nullptr)
  178. {
  179. }
  180. /// Copy-construct from another weak array pointer.
  181. WeakArrayPtr(const WeakArrayPtr<T>& rhs) :
  182. ptr_(rhs.ptr_),
  183. refCount_(rhs.refCount_)
  184. {
  185. AddRef();
  186. }
  187. /// Construct from a shared array pointer.
  188. explicit WeakArrayPtr(const SharedArrayPtr<T>& rhs) :
  189. ptr_(rhs.Get()),
  190. refCount_(rhs.RefCountPtr())
  191. {
  192. AddRef();
  193. }
  194. /// Destruct. Release the weak reference to the array.
  195. ~WeakArrayPtr()
  196. {
  197. ReleaseRef();
  198. }
  199. /// Assign from a shared array pointer.
  200. WeakArrayPtr<T>& operator =(const SharedArrayPtr<T>& rhs)
  201. {
  202. if (ptr_ == rhs.Get() && refCount_ == rhs.RefCountPtr())
  203. return *this;
  204. ReleaseRef();
  205. ptr_ = rhs.Get();
  206. refCount_ = rhs.RefCountPtr();
  207. AddRef();
  208. return *this;
  209. }
  210. /// Assign from another weak array pointer.
  211. WeakArrayPtr<T>& operator =(const WeakArrayPtr<T>& rhs)
  212. {
  213. if (ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_)
  214. return *this;
  215. ReleaseRef();
  216. ptr_ = rhs.ptr_;
  217. refCount_ = rhs.refCount_;
  218. AddRef();
  219. return *this;
  220. }
  221. /// Convert to shared array pointer. If expired, return a null shared array pointer.
  222. SharedArrayPtr<T> Lock() const
  223. {
  224. if (Expired())
  225. return SharedArrayPtr<T>();
  226. else
  227. return SharedArrayPtr<T>(ptr_, refCount_);
  228. }
  229. /// Return raw pointer. If expired, return null.
  230. T* Get() const
  231. {
  232. if (Expired())
  233. return 0;
  234. else
  235. return ptr_;
  236. }
  237. /// Point to the array.
  238. T* operator ->() const
  239. {
  240. T* rawPtr = Get();
  241. assert(rawPtr);
  242. return rawPtr;
  243. }
  244. /// Dereference the array.
  245. T& operator *() const
  246. {
  247. T* rawPtr = Get();
  248. assert(rawPtr);
  249. return *rawPtr;
  250. }
  251. /// Subscript the array.
  252. T& operator [](const int index)
  253. {
  254. T* rawPtr = Get();
  255. assert(rawPtr);
  256. return (*rawPtr)[index];
  257. }
  258. /// Test for equality with another weak array pointer.
  259. bool operator ==(const WeakArrayPtr<T>& rhs) const { return ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_; }
  260. /// Test for inequality with another weak array pointer.
  261. bool operator !=(const WeakArrayPtr<T>& rhs) const { return ptr_ != rhs.ptr_ || refCount_ != rhs.refCount_; }
  262. /// Test for less than with another weak array pointer.
  263. bool operator <(const WeakArrayPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
  264. /// Convert to a raw pointer, null if array is expired.
  265. operator T*() const { return Get(); } // NOLINT(google-explicit-constructor)
  266. /// Reset to null and release the weak reference.
  267. void Reset() { ReleaseRef(); }
  268. /// Perform a static cast from a weak array pointer of another type.
  269. template <class U> void StaticCast(const WeakArrayPtr<U>& rhs)
  270. {
  271. ReleaseRef();
  272. ptr_ = static_cast<T*>(rhs.Get());
  273. refCount_ = rhs.refCount_;
  274. AddRef();
  275. }
  276. /// Perform a dynamic cast from a weak array pointer of another type.
  277. template <class U> void DynamicCast(const WeakArrayPtr<U>& rhs)
  278. {
  279. ReleaseRef();
  280. ptr_ = dynamic_cast<T*>(rhs.Get());
  281. if (ptr_)
  282. {
  283. refCount_ = rhs.refCount_;
  284. AddRef();
  285. }
  286. else
  287. refCount_ = 0;
  288. }
  289. /// Check if the pointer is null.
  290. bool Null() const { return refCount_ == 0; }
  291. /// Check if the pointer is not null.
  292. bool NotNull() const { return refCount_ != 0; }
  293. /// Return the array's reference count, or 0 if null pointer or if array has expired.
  294. int Refs() const { return (refCount_ && refCount_->refs_ >= 0) ? refCount_->refs_ : 0; }
  295. /// Return the array's weak reference count.
  296. int WeakRefs() const { return refCount_ ? refCount_->weakRefs_ : 0; }
  297. /// Return whether the array has expired. If null pointer, always return true.
  298. bool Expired() const { return refCount_ ? refCount_->refs_ < 0 : true; }
  299. /// Return pointer to RefCount structure.
  300. RefCount* RefCountPtr() const { return refCount_; }
  301. /// Return hash value for HashSet & HashMap.
  302. hash32 ToHash() const { return (hash32)((size_t)ptr_ / sizeof(T)); }
  303. private:
  304. /// Prevent direct assignment from a weak array pointer of different type.
  305. template <class U> WeakArrayPtr<T>& operator =(const WeakArrayPtr<U>& rhs);
  306. /// Add a weak reference to the array pointed to.
  307. void AddRef()
  308. {
  309. if (refCount_)
  310. {
  311. assert(refCount_->weakRefs_ >= 0);
  312. ++(refCount_->weakRefs_);
  313. }
  314. }
  315. /// Release the weak reference. Delete the Refcount structure if necessary.
  316. void ReleaseRef()
  317. {
  318. if (refCount_)
  319. {
  320. assert(refCount_->weakRefs_ >= 0);
  321. if (refCount_->weakRefs_ > 0)
  322. --(refCount_->weakRefs_);
  323. if (Expired() && !refCount_->weakRefs_)
  324. delete refCount_;
  325. }
  326. ptr_ = 0;
  327. refCount_ = 0;
  328. }
  329. /// Pointer to the array.
  330. T* ptr_;
  331. /// Pointer to the RefCount structure.
  332. RefCount* refCount_;
  333. };
  334. /// Perform a static cast from one weak array pointer type to another.
  335. template <class T, class U> WeakArrayPtr<T> StaticCast(const WeakArrayPtr<U>& ptr)
  336. {
  337. WeakArrayPtr<T> ret;
  338. ret.StaticCast(ptr);
  339. return ret;
  340. }
  341. /// Perform a reinterpret cast from one weak pointer type to another.
  342. template <class T, class U> WeakArrayPtr<T> ReinterpretCast(const WeakArrayPtr<U>& ptr)
  343. {
  344. WeakArrayPtr<T> ret;
  345. ret.ReinterpretCast(ptr);
  346. return ret;
  347. }
  348. }