ArrayPtr.h 13 KB

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