ArrayPtr.h 13 KB

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