ArrayPtr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/HashBase.h"
  24. #include "../Container/RefCounted.h"
  25. #include <cassert>
  26. namespace Atomic
  27. {
  28. /// Shared array pointer template class. Uses non-intrusive reference counting.
  29. template <class T> class SharedArrayPtr
  30. {
  31. public:
  32. /// Construct a null shared array pointer.
  33. SharedArrayPtr() :
  34. ptr_(0),
  35. refCount_(0)
  36. {
  37. }
  38. /// Copy-construct from another shared array pointer.
  39. SharedArrayPtr(const SharedArrayPtr<T>& rhs) :
  40. ptr_(rhs.ptr_),
  41. refCount_(rhs.refCount_)
  42. {
  43. AddRef();
  44. }
  45. /// Construct from a raw pointer.
  46. explicit SharedArrayPtr(T* ptr) :
  47. ptr_(ptr),
  48. refCount_(new RefCount())
  49. {
  50. AddRef();
  51. }
  52. /// Destruct. Release the array reference.
  53. ~SharedArrayPtr()
  54. {
  55. ReleaseRef();
  56. }
  57. /// Assign from another shared array pointer.
  58. SharedArrayPtr<T>& operator =(const SharedArrayPtr<T>& rhs)
  59. {
  60. if (ptr_ == rhs.ptr_)
  61. return *this;
  62. ReleaseRef();
  63. ptr_ = rhs.ptr_;
  64. refCount_ = rhs.refCount_;
  65. AddRef();
  66. return *this;
  67. }
  68. /// Assign from a raw pointer.
  69. SharedArrayPtr<T>& operator =(T* ptr)
  70. {
  71. if (ptr_ == ptr)
  72. return *this;
  73. ReleaseRef();
  74. if (ptr)
  75. {
  76. ptr_ = ptr;
  77. refCount_ = new RefCount();
  78. AddRef();
  79. }
  80. return *this;
  81. }
  82. /// Point to the array.
  83. T* operator ->() const
  84. {
  85. assert(ptr_);
  86. return ptr_;
  87. }
  88. /// Dereference the array.
  89. T& operator *() const
  90. {
  91. assert(ptr_);
  92. return *ptr_;
  93. }
  94. /// Subscript the array.
  95. T& operator [](const int index)
  96. {
  97. assert(ptr_);
  98. return ptr_[index];
  99. }
  100. /// Test for equality with another shared array pointer.
  101. bool operator ==(const SharedArrayPtr<T>& rhs) const { return ptr_ == rhs.ptr_; }
  102. /// Test for inequality with another shared array pointer.
  103. bool operator !=(const SharedArrayPtr<T>& rhs) const { return ptr_ != rhs.ptr_; }
  104. /// Test for less than with another array pointer.
  105. bool operator <(const SharedArrayPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
  106. /// Convert to a raw pointer.
  107. operator T*() const { return ptr_; }
  108. /// Reset to null and release the array reference.
  109. void Reset() { ReleaseRef(); }
  110. /// Perform a static cast from a shared array pointer of another type.
  111. template <class U> void StaticCast(const SharedArrayPtr<U>& rhs)
  112. {
  113. ReleaseRef();
  114. ptr_ = static_cast<T*>(rhs.Get());
  115. refCount_ = rhs.RefCountPtr();
  116. AddRef();
  117. }
  118. /// Perform a reinterpret cast from a shared array pointer of another type.
  119. template <class U> void ReinterpretCast(const SharedArrayPtr<U>& rhs)
  120. {
  121. ReleaseRef();
  122. ptr_ = reinterpret_cast<T*>(rhs.Get());
  123. refCount_ = rhs.RefCountPtr();
  124. AddRef();
  125. }
  126. /// Check if the pointer is null.
  127. bool Null() const { return ptr_ == 0; }
  128. /// Check if the pointer is not null.
  129. bool NotNull() const { return ptr_ != 0; }
  130. /// Return the raw pointer.
  131. T* Get() const { return ptr_; }
  132. /// Return the array's reference count, or 0 if the pointer is null.
  133. int Refs() const { return refCount_ ? refCount_->refs_ : 0; }
  134. /// Return the array's weak reference count, or 0 if the pointer is null.
  135. int WeakRefs() const { return refCount_ ? refCount_->weakRefs_ : 0; }
  136. /// Return pointer to the RefCount structure.
  137. RefCount* RefCountPtr() const { return refCount_; }
  138. /// Return hash value for HashSet & HashMap.
  139. unsigned ToHash() const { return (unsigned)((size_t)ptr_ / sizeof(T)); }
  140. private:
  141. /// Prevent direct assignment from a shared array pointer of different type.
  142. template <class U> SharedArrayPtr<T>& operator =(const SharedArrayPtr<U>& rhs);
  143. /// Add a reference to the array pointed to.
  144. void AddRef()
  145. {
  146. if (refCount_)
  147. {
  148. assert(refCount_->refs_ >= 0);
  149. ++(refCount_->refs_);
  150. }
  151. }
  152. /// Release the array reference and delete it and the RefCount structure if necessary.
  153. void ReleaseRef()
  154. {
  155. if (refCount_)
  156. {
  157. assert(refCount_->refs_ > 0);
  158. --(refCount_->refs_);
  159. if (!refCount_->refs_)
  160. {
  161. refCount_->refs_ = -1;
  162. delete[] ptr_;
  163. }
  164. if (refCount_->refs_ < 0 && !refCount_->weakRefs_)
  165. delete refCount_;
  166. }
  167. ptr_ = 0;
  168. refCount_ = 0;
  169. }
  170. /// Pointer to the array.
  171. T* ptr_;
  172. /// Pointer to the RefCount structure.
  173. RefCount* refCount_;
  174. };
  175. /// Perform a static cast from one shared array pointer type to another.
  176. template <class T, class U> SharedArrayPtr<T> StaticCast(const SharedArrayPtr<U>& ptr)
  177. {
  178. SharedArrayPtr<T> ret;
  179. ret.StaticCast(ptr);
  180. return ret;
  181. }
  182. /// Perform a reinterpret cast from one shared array pointer type to another.
  183. template <class T, class U> SharedArrayPtr<T> ReinterpretCast(const SharedArrayPtr<U>& ptr)
  184. {
  185. SharedArrayPtr<T> ret;
  186. ret.ReinterpretCast(ptr);
  187. return ret;
  188. }
  189. /// Weak array pointer template class. Uses non-intrusive reference counting.
  190. template <class T> class WeakArrayPtr
  191. {
  192. public:
  193. /// Construct a null weak array pointer.
  194. WeakArrayPtr() :
  195. ptr_(0),
  196. refCount_(0)
  197. {
  198. }
  199. /// Copy-construct from another weak array pointer.
  200. WeakArrayPtr(const WeakArrayPtr<T>& rhs) :
  201. ptr_(rhs.ptr_),
  202. refCount_(rhs.refCount_)
  203. {
  204. AddRef();
  205. }
  206. /// Construct from a shared array pointer.
  207. WeakArrayPtr(const SharedArrayPtr<T>& rhs) :
  208. ptr_(rhs.Get()),
  209. refCount_(rhs.RefCountPtr())
  210. {
  211. AddRef();
  212. }
  213. /// Destruct. Release the weak reference to the array.
  214. ~WeakArrayPtr()
  215. {
  216. ReleaseRef();
  217. }
  218. /// Assign from a shared array pointer.
  219. WeakArrayPtr<T>& operator =(const SharedArrayPtr<T>& rhs)
  220. {
  221. if (ptr_ == rhs.Get() && refCount_ == rhs.RefCountPtr())
  222. return *this;
  223. ReleaseRef();
  224. ptr_ = rhs.Get();
  225. refCount_ = rhs.RefCountPtr();
  226. AddRef();
  227. return *this;
  228. }
  229. /// Assign from another weak array pointer.
  230. WeakArrayPtr<T>& operator =(const WeakArrayPtr<T>& rhs)
  231. {
  232. if (ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_)
  233. return *this;
  234. ReleaseRef();
  235. ptr_ = rhs.ptr_;
  236. refCount_ = rhs.refCount_;
  237. AddRef();
  238. return *this;
  239. }
  240. /// Convert to shared array pointer. If expired, return a null shared array pointer.
  241. SharedArrayPtr<T> Lock() const
  242. {
  243. if (Expired())
  244. return SharedArrayPtr<T>();
  245. else
  246. return SharedArrayPtr<T>(ptr_, refCount_);
  247. }
  248. /// Return raw pointer. If expired, return null.
  249. T* Get() const
  250. {
  251. if (Expired())
  252. return 0;
  253. else
  254. return ptr_;
  255. }
  256. /// Point to the array.
  257. T* operator ->() const
  258. {
  259. T* rawPtr = Get();
  260. assert(rawPtr);
  261. return rawPtr;
  262. }
  263. /// Dereference the array.
  264. T& operator *() const
  265. {
  266. T* rawPtr = Get();
  267. assert(rawPtr);
  268. return *rawPtr;
  269. }
  270. /// Subscript the array.
  271. T& operator [](const int index)
  272. {
  273. T* rawPtr = Get();
  274. assert(rawPtr);
  275. return (*rawPtr)[index];
  276. }
  277. /// Test for equality with another weak array pointer.
  278. bool operator ==(const WeakArrayPtr<T>& rhs) const { return ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_; }
  279. /// Test for inequality with another weak array pointer.
  280. bool operator !=(const WeakArrayPtr<T>& rhs) const { return ptr_ != rhs.ptr_ || refCount_ != rhs.refCount_; }
  281. /// Test for less than with another weak array pointer.
  282. bool operator <(const WeakArrayPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
  283. /// Convert to a raw pointer, null if array is expired.
  284. operator T*() const { return Get(); }
  285. /// Reset to null and release the weak reference.
  286. void Reset() { ReleaseRef(); }
  287. /// Perform a static cast from a weak array pointer of another type.
  288. template <class U> void StaticCast(const WeakArrayPtr<U>& rhs)
  289. {
  290. ReleaseRef();
  291. ptr_ = static_cast<T*>(rhs.Get());
  292. refCount_ = rhs.refCount_;
  293. AddRef();
  294. }
  295. /// Perform a dynamic cast from a weak array pointer of another type.
  296. template <class U> void DynamicCast(const WeakArrayPtr<U>& rhs)
  297. {
  298. ReleaseRef();
  299. ptr_ = dynamic_cast<T*>(rhs.Get());
  300. if (ptr_)
  301. {
  302. refCount_ = rhs.refCount_;
  303. AddRef();
  304. }
  305. else
  306. refCount_ = 0;
  307. }
  308. /// Check if the pointer is null.
  309. bool Null() const { return refCount_ == 0; }
  310. /// Check if the pointer is not null.
  311. bool NotNull() const { return refCount_ != 0; }
  312. /// Return the array's reference count, or 0 if null pointer or if array has expired.
  313. int Refs() const { return (refCount_ && refCount_->refs_ >= 0) ? refCount_->refs_ : 0; }
  314. /// Return the array's weak reference count.
  315. int WeakRefs() const { return refCount_ ? refCount_->weakRefs_ : 0; }
  316. /// Return whether the array has expired. If null pointer, always return true.
  317. bool Expired() const { return refCount_ ? refCount_->refs_ < 0 : true; }
  318. /// Return pointer to RefCount structure.
  319. RefCount* RefCountPtr() const { return refCount_; }
  320. /// Return hash value for HashSet & HashMap.
  321. unsigned ToHash() const { return (unsigned)((size_t)ptr_ / sizeof(T)); }
  322. private:
  323. /// Prevent direct assignment from a weak array pointer of different type.
  324. template <class U> WeakArrayPtr<T>& operator =(const WeakArrayPtr<U>& rhs);
  325. /// Add a weak reference to the array pointed to.
  326. void AddRef()
  327. {
  328. if (refCount_)
  329. {
  330. assert(refCount_->weakRefs_ >= 0);
  331. ++(refCount_->weakRefs_);
  332. }
  333. }
  334. /// Release the weak reference. Delete the Refcount structure if necessary.
  335. void ReleaseRef()
  336. {
  337. if (refCount_)
  338. {
  339. assert(refCount_->weakRefs_ >= 0);
  340. if (refCount_->weakRefs_ > 0)
  341. --(refCount_->weakRefs_);
  342. if (Expired() && !refCount_->weakRefs_)
  343. delete refCount_;
  344. }
  345. ptr_ = 0;
  346. refCount_ = 0;
  347. }
  348. /// Pointer to the array.
  349. T* ptr_;
  350. /// Pointer to the RefCount structure.
  351. RefCount* refCount_;
  352. };
  353. /// Perform a static cast from one weak array pointer type to another.
  354. template <class T, class U> WeakArrayPtr<T> StaticCast(const WeakArrayPtr<U>& ptr)
  355. {
  356. WeakArrayPtr<T> ret;
  357. ret.StaticCast(ptr);
  358. return ret;
  359. }
  360. /// Perform a reinterpret cast from one weak pointer type to another.
  361. template <class T, class U> WeakArrayPtr<T> ReinterpretCast(const WeakArrayPtr<U>& ptr)
  362. {
  363. WeakArrayPtr<T> ret;
  364. ret.ReinterpretCast(ptr);
  365. return ret;
  366. }
  367. }