ArrayPtr.h 13 KB

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