SharedArrayPtr.h 12 KB

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