ArrayPtr.h 13 KB

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