ArrayPtr.h 13 KB

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