ArrayPtr.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. ++(refCount_->refs_);
  44. }
  45. /// Construct from a raw pointer.
  46. explicit SharedArrayPtr(T* ptr) :
  47. ptr_(ptr),
  48. refCount_(new RefCount())
  49. {
  50. ++(refCount_->refs_);
  51. }
  52. /// Destruct. Release the array reference.
  53. ~SharedArrayPtr()
  54. {
  55. Release();
  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. Release();
  63. ptr_ = rhs.ptr_;
  64. refCount_ = rhs.refCount_;
  65. if (refCount_)
  66. ++(refCount_->refs_);
  67. return *this;
  68. }
  69. /// Assign from a raw pointer.
  70. SharedArrayPtr<T>& operator = (T* ptr)
  71. {
  72. if (ptr_ == ptr)
  73. return *this;
  74. Release();
  75. if (ptr)
  76. {
  77. ptr_ = ptr;
  78. refCount_ = new RefCount();
  79. ++(refCount_->refs_);
  80. }
  81. return *this;
  82. }
  83. /// Point to the array.
  84. T* operator -> () const { assert(ptr_); return ptr_; }
  85. /// Dereference the array.
  86. T& operator * () const { assert(ptr_); return *ptr_; }
  87. /// Subscript the array.
  88. T& operator [] (const int index) { assert(ptr_); return ptr_[index]; }
  89. /// Test for equality with another shared array pointer.
  90. bool operator == (const SharedArrayPtr<T>& rhs) const { return ptr_ == rhs.ptr_; }
  91. /// Test for inequality with another shared array pointer.
  92. bool operator != (const SharedArrayPtr<T>& rhs) const { return ptr_ != rhs.ptr_; }
  93. /// Test for less than with another array pointer.
  94. bool operator < (const SharedArrayPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
  95. /// Return true if points to an array.
  96. operator bool () const { return ptr_ != 0; }
  97. /// Convert to a raw pointer.
  98. operator T* () const { return ptr_; }
  99. /// Reset to null and release the array reference.
  100. void Reset() { Release(); }
  101. /// Perform a static cast from a shared array pointer of another type.
  102. template <class U> void StaticCast(const SharedArrayPtr<U>& rhs)
  103. {
  104. Release();
  105. ptr_ = static_cast<T*>(rhs.Get());
  106. refCount_ = rhs.RefCountPtr();
  107. if (refCount_)
  108. ++(refCount_->refs_);
  109. }
  110. /// Perform a dynatic cast from a shared array pointer of another type.
  111. template <class U> void DynamicCast(const SharedArrayPtr<U>& rhs)
  112. {
  113. Release();
  114. ptr_ = dynamic_cast<T*>(rhs.Get());
  115. if (ptr_)
  116. {
  117. refCount_ = rhs.RefCountPtr();
  118. if (refCount_)
  119. ++(refCount_->refs_);
  120. }
  121. else
  122. refCount_ = 0;
  123. }
  124. /// Check if the pointer is null.
  125. bool Null() const { return ptr_ == 0; }
  126. /// Check if the pointer is not null.
  127. bool NotNull() const { return ptr_ != 0; }
  128. /// Return the raw pointer.
  129. T* Get() const { return ptr_; }
  130. /// Return the array's reference count, or 0 if the pointer is null.
  131. unsigned Refs() const { return refCount_ ? refCount_->refs_ : 0; }
  132. /// Return the array's weak reference count, or 0 if the pointer is null.
  133. unsigned WeakRefs() const { return refCount_ ? refCount_->weakRefs_ : 0; }
  134. /// Return pointer to the RefCount structure.
  135. RefCount* RefCountPtr() const { return refCount_; }
  136. /// Return hash value for HashSet & HashMap.
  137. unsigned ToHash() const { return ((unsigned)ptr_) / sizeof(T); }
  138. private:
  139. /// Prevent direct assignment from a shared array pointer of different type.
  140. template <class U> SharedArrayPtr<T>& operator = (const SharedArrayPtr<U>& rhs);
  141. /// Release the array reference and delete it and the RefCount structure if necessary.
  142. void Release()
  143. {
  144. if (refCount_)
  145. {
  146. if (refCount_->refs_)
  147. {
  148. --(refCount_->refs_);
  149. if (!refCount_->refs_)
  150. {
  151. refCount_->expired_ = true;
  152. delete[] ptr_;
  153. }
  154. }
  155. if (!refCount_->refs_ && !refCount_->weakRefs_)
  156. delete refCount_;
  157. }
  158. ptr_ = 0;
  159. refCount_ = 0;
  160. }
  161. /// Pointer to the array.
  162. T* ptr_;
  163. /// Pointer to the RefCount structure.
  164. RefCount* refCount_;
  165. };
  166. /// Perform a static cast from one shared array pointer type to another.
  167. template <class T, class U> SharedArrayPtr<T> StaticCast(const SharedArrayPtr<U>& ptr)
  168. {
  169. SharedArrayPtr<T> ret;
  170. ret.StaticCast(ptr);
  171. return ret;
  172. }
  173. /// Perform a dynamic cast from one shared array pointer type to another.
  174. template <class T, class U> SharedArrayPtr<T> DynamicCast(const SharedArrayPtr<U>& ptr)
  175. {
  176. SharedArrayPtr<T> ret;
  177. ret.DynamicCast(ptr);
  178. return ret;
  179. }
  180. /// Weak array pointer template class. Uses non-intrusive reference counting.
  181. template <class T> class WeakArrayPtr
  182. {
  183. public:
  184. /// Construct a null weak array pointer.
  185. WeakArrayPtr() :
  186. ptr_(0),
  187. refCount_(0)
  188. {
  189. }
  190. /// Construct from a shared array pointer.
  191. WeakArrayPtr(const SharedArrayPtr<T>& rhs) :
  192. ptr_(rhs.Get()),
  193. refCount_(rhs.RefCountPtr())
  194. {
  195. if (refCount_)
  196. ++(refCount_->weakRefs_);
  197. }
  198. /// Copy-construct from another weak array pointer.
  199. WeakArrayPtr(const WeakArrayPtr<T>& rhs) :
  200. ptr_(rhs.ptr_),
  201. refCount_(rhs.refCount_)
  202. {
  203. if (refCount_)
  204. ++(refCount_->weakRefs_);
  205. }
  206. /// Destruct. Release the weak reference to the array.
  207. ~WeakArrayPtr()
  208. {
  209. Release();
  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. Release();
  217. ptr_ = rhs.Get();
  218. refCount_ = rhs.RefCountPtr();
  219. if (refCount_)
  220. ++(refCount_->weakRefs_);
  221. return *this;
  222. }
  223. /// Assign from another weak array pointer.
  224. WeakArrayPtr<T>& operator = (const WeakArrayPtr<T>& rhs)
  225. {
  226. if (ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_)
  227. return *this;
  228. Release();
  229. ptr_ = rhs.ptr_;
  230. refCount_ = rhs.refCount_;
  231. if (refCount_)
  232. ++(refCount_->weakRefs_);
  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() { Release(); }
  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. Release();
  288. ptr_ = static_cast<T*>(rhs.Get());
  289. refCount_ = rhs.refCount_;
  290. if (refCount_)
  291. ++(refCount_->weakRefs_);
  292. }
  293. /// Perform a dynamic cast from a weak array pointer of another type.
  294. template <class U> void DynamicCast(const WeakArrayPtr<U>& rhs)
  295. {
  296. Release();
  297. ptr_ = dynamic_cast<T*>(rhs.Get());
  298. if (ptr_)
  299. {
  300. refCount_ = rhs.refCount_;
  301. if (refCount_)
  302. ++(refCount_->weakRefs_);
  303. }
  304. else
  305. refCount_ = 0;
  306. }
  307. /// Check if the pointer is null.
  308. bool Null() const { return refCount_ == 0; }
  309. /// Check if the pointer is not null.
  310. bool NotNull() const { return refCount_ != 0; }
  311. /// Return the array's reference count, or 0 if null pointer or if array is expired.
  312. unsigned Refs() const { return refCount_ ? refCount_->refs_ : 0; }
  313. /// Return the array's weak reference count.
  314. unsigned WeakRefs() const { return refCount_ ? refCount_->weakRefs_ : 0; }
  315. /// Return whether the array has expired. If null pointer, always return true.
  316. bool Expired() const { return refCount_ ? refCount_->expired_ : true; }
  317. /// Return pointer to RefCount structure.
  318. RefCount* RefCountPtr() const { return refCount_; }
  319. /// Return hash value for HashSet & HashMap.
  320. unsigned ToHash() const { return ((unsigned)ptr_) / sizeof(T); }
  321. private:
  322. /// Prevent direct assignment from a weak array pointer of different type.
  323. template <class U> WeakArrayPtr<T>& operator = (const WeakArrayPtr<U>& rhs);
  324. /// Release the weak reference. Delete the Refcount structure if necessary.
  325. void Release()
  326. {
  327. if (refCount_)
  328. {
  329. if (refCount_->weakRefs_)
  330. --(refCount_->weakRefs_);
  331. if (!refCount_->refs_ && !refCount_->weakRefs_)
  332. delete refCount_;
  333. }
  334. ptr_ = 0;
  335. refCount_ = 0;
  336. }
  337. /// Pointer to the array.
  338. T* ptr_;
  339. /// Pointer to the RefCount structure.
  340. RefCount* refCount_;
  341. };
  342. /// Perform a static cast from one weak array pointer type to another.
  343. template <class T, class U> WeakArrayPtr<T> StaticCast(const WeakArrayPtr<U>& ptr)
  344. {
  345. WeakArrayPtr<T> ret;
  346. ret.StaticCast(ptr);
  347. return ret;
  348. }
  349. /// Perform a dynamic cast from one weak pointer type to another.
  350. template <class T, class U> WeakArrayPtr<T> DynamicCast(const WeakArrayPtr<U>& ptr)
  351. {
  352. WeakArrayPtr<T> ret;
  353. ret.DynamicCast(ptr);
  354. return ret;
  355. }