SharedArrayPtr.h 13 KB

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