SharedArrayPtr.h 12 KB

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