SharedPtr.h 12 KB

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