SharedPtr.h 12 KB

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