ArrayPtr.h 13 KB

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