SharedPtr.h 12 KB

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