Ptr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "RefCounted.h"
  24. #include <cassert>
  25. #include <cstddef>
  26. namespace Urho3D
  27. {
  28. /// Shared pointer template class with intrusive reference counting.
  29. template <class T> class SharedPtr
  30. {
  31. public:
  32. /// Construct a null shared pointer.
  33. SharedPtr() :
  34. ptr_(0)
  35. {
  36. }
  37. /// Copy-construct from another shared pointer.
  38. SharedPtr(const SharedPtr<T>& rhs) :
  39. ptr_(rhs.ptr_)
  40. {
  41. AddRef();
  42. }
  43. /// Construct from a raw pointer.
  44. explicit SharedPtr(T* ptr) :
  45. ptr_(ptr)
  46. {
  47. AddRef();
  48. }
  49. /// Destruct. Release the object reference.
  50. ~SharedPtr()
  51. {
  52. ReleaseRef();
  53. }
  54. /// Assign from another shared pointer.
  55. SharedPtr<T>& operator = (const SharedPtr<T>& rhs)
  56. {
  57. if (ptr_ == rhs.ptr_)
  58. return *this;
  59. ReleaseRef();
  60. ptr_ = rhs.ptr_;
  61. 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. ReleaseRef();
  70. ptr_ = ptr;
  71. AddRef();
  72. return *this;
  73. }
  74. /// Point to the object.
  75. T* operator -> () const { assert(ptr_); return ptr_; }
  76. /// Dereference the object.
  77. T& operator * () const { assert(ptr_); return *ptr_; }
  78. /// Subscript the object if applicable.
  79. T& operator [] (const int index) { assert(ptr_); return ptr_[index]; }
  80. /// Test for less than with another shared pointer.
  81. bool operator < (const SharedPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
  82. /// Test for equality with another shared pointer.
  83. bool operator == (const SharedPtr<T>& rhs) const { return ptr_ == rhs.ptr_; }
  84. /// Test for inequality with another shared pointer.
  85. bool operator != (const SharedPtr<T>& rhs) const { return ptr_ != rhs.ptr_; }
  86. /// Convert to a raw pointer.
  87. operator T* () const { return ptr_; }
  88. /// Reset to null and release the object reference.
  89. void Reset() { ReleaseRef(); }
  90. /// Perform a static cast from a shared pointer of another type.
  91. template <class U> void StaticCast(const SharedPtr<U>& rhs)
  92. {
  93. ReleaseRef();
  94. ptr_ = static_cast<T*>(rhs.Get());
  95. AddRef();
  96. }
  97. /// Perform a dynamic cast from a shared pointer of another type.
  98. template <class U> void DynamicCast(const SharedPtr<U>& rhs)
  99. {
  100. ReleaseRef();
  101. ptr_ = dynamic_cast<T*>(rhs.Get());
  102. AddRef();
  103. }
  104. /// Check if the pointer is null.
  105. bool Null() const { return ptr_ == 0; }
  106. /// Check if the pointer is not null.
  107. bool NotNull() const { return ptr_ != 0; }
  108. /// Return the raw pointer.
  109. T* Get() const { return ptr_; }
  110. /// Return the object's reference count, or 0 if the pointer is null.
  111. int Refs() const { return ptr_ ? ptr_->Refs() : 0; }
  112. /// Return the object's weak reference count, or 0 if the pointer is null.
  113. int WeakRefs() const { return ptr_ ? ptr_->WeakRefs() : 0; }
  114. /// Return pointer to the RefCount structure.
  115. RefCount* RefCountPtr() const { return ptr_ ? ptr_->RefCountPtr() : 0; }
  116. /// Return hash value for HashSet & HashMap.
  117. unsigned ToHash() const { return ((unsigned)(size_t)ptr_) / sizeof(T); }
  118. private:
  119. /// Prevent direct assignment from a shared pointer of another type.
  120. template <class U> SharedPtr<T>& operator = (const SharedPtr<U>& rhs);
  121. /// Add a reference to the object pointed to.
  122. void AddRef()
  123. {
  124. if (ptr_)
  125. ptr_->AddRef();
  126. }
  127. /// Release the object reference and delete it if necessary.
  128. void ReleaseRef()
  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 with intrusive reference counting. Does not keep the object pointed to 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. /// Copy-construct from another weak pointer.
  164. WeakPtr(const WeakPtr<T>& rhs) :
  165. ptr_(rhs.ptr_),
  166. refCount_(rhs.refCount_)
  167. {
  168. AddRef();
  169. }
  170. /// Construct from a shared pointer.
  171. WeakPtr(const SharedPtr<T>& rhs) :
  172. ptr_(rhs.Get()),
  173. refCount_(rhs.RefCountPtr())
  174. {
  175. AddRef();
  176. }
  177. /// Construct from a raw pointer.
  178. explicit WeakPtr(T* ptr) :
  179. ptr_(ptr),
  180. refCount_(ptr ? ptr->RefCountPtr() : 0)
  181. {
  182. AddRef();
  183. }
  184. /// Destruct. Release the weak reference to the object.
  185. ~WeakPtr()
  186. {
  187. ReleaseRef();
  188. }
  189. /// Assign from a shared pointer.
  190. WeakPtr<T>& operator = (const SharedPtr<T>& rhs)
  191. {
  192. if (ptr_ == rhs.Get() && refCount_ == rhs.RefCountPtr())
  193. return *this;
  194. ReleaseRef();
  195. ptr_ = rhs.Get();
  196. refCount_ = rhs.RefCountPtr();
  197. AddRef();
  198. return *this;
  199. }
  200. /// Assign from a weak pointer.
  201. WeakPtr<T>& operator = (const WeakPtr<T>& rhs)
  202. {
  203. if (ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_)
  204. return *this;
  205. ReleaseRef();
  206. ptr_ = rhs.ptr_;
  207. refCount_ = rhs.refCount_;
  208. AddRef();
  209. return *this;
  210. }
  211. /// Assign from a raw pointer.
  212. WeakPtr<T>& operator = (T* ptr)
  213. {
  214. RefCount* refCount = ptr ? ptr->RefCountPtr() : 0;
  215. if (ptr_ == ptr && refCount_ == refCount)
  216. return *this;
  217. ReleaseRef();
  218. ptr_ = ptr;
  219. refCount_ = refCount;
  220. AddRef();
  221. return *this;
  222. }
  223. /// Convert to a shared pointer. If expired, return a null shared pointer.
  224. SharedPtr<T> Lock() const
  225. {
  226. if (Expired())
  227. return SharedPtr<T>();
  228. else
  229. return SharedPtr<T>(ptr_);
  230. }
  231. /// Return raw pointer. If expired, return null.
  232. T* Get() const
  233. {
  234. if (Expired())
  235. return 0;
  236. else
  237. return ptr_;
  238. }
  239. /// Point to the object.
  240. T* operator -> () const
  241. {
  242. T* rawPtr = Get();
  243. assert(rawPtr);
  244. return rawPtr;
  245. }
  246. /// Dereference the object.
  247. T& operator * () const
  248. {
  249. T* rawPtr = Get();
  250. assert(rawPtr);
  251. return *rawPtr;
  252. }
  253. /// Subscript the object if applicable.
  254. T& operator [] (const int index)
  255. {
  256. T* rawPtr = Get();
  257. assert(rawPtr);
  258. return (*rawPtr)[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. /// Convert to a raw pointer, null if the object is expired.
  267. operator T* () const { return Get(); }
  268. /// Reset to null and release the weak reference.
  269. void Reset() { ReleaseRef(); }
  270. /// Perform a static cast from a weak pointer of another type.
  271. template <class U> void StaticCast(const WeakPtr<U>& rhs)
  272. {
  273. ReleaseRef();
  274. ptr_ = static_cast<T*>(rhs.Get());
  275. refCount_ = rhs.refCount_;
  276. AddRef();
  277. }
  278. /// Perform a dynamic cast from a weak pointer of another type.
  279. template <class U> void DynamicCast(const WeakPtr<U>& rhs)
  280. {
  281. ReleaseRef();
  282. ptr_ = dynamic_cast<T*>(rhs.Get());
  283. if (ptr_)
  284. {
  285. refCount_ = rhs.refCount_;
  286. AddRef();
  287. }
  288. else
  289. refCount_ = 0;
  290. }
  291. /// Check if the pointer is null.
  292. bool Null() const { return refCount_ == 0; }
  293. /// Check if the pointer is not null.
  294. bool NotNull() const { return refCount_ != 0; }
  295. /// Return the object's reference count, or 0 if null pointer or if object has expired.
  296. int Refs() const { return (refCount_ && refCount_->refs_ >= 0) ? refCount_->refs_ : 0; }
  297. /// Return the object's weak reference count.
  298. int WeakRefs() const
  299. {
  300. if (!Expired())
  301. return ptr_->WeakRefs();
  302. else
  303. return refCount_ ? refCount_->weakRefs_ : 0;
  304. }
  305. /// Return whether the object has expired. If null pointer, always return true.
  306. bool Expired() const { return refCount_ ? refCount_->refs_ < 0 : true; }
  307. /// Return pointer to the RefCount structure.
  308. RefCount* RefCountPtr() const { return refCount_; }
  309. /// Return hash value for HashSet & HashMap.
  310. unsigned ToHash() const { return ((unsigned)(size_t)ptr_) / sizeof(T); }
  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. /// Add a weak reference to the object pointed to.
  315. void AddRef()
  316. {
  317. if (refCount_)
  318. {
  319. assert(refCount_->weakRefs_ >= 0);
  320. ++(refCount_->weakRefs_);
  321. }
  322. }
  323. /// Release the weak reference. Delete the Refcount structure if necessary.
  324. void ReleaseRef()
  325. {
  326. if (refCount_)
  327. {
  328. assert(refCount_->weakRefs_ > 0);
  329. --(refCount_->weakRefs_);
  330. if (Expired() && !refCount_->weakRefs_)
  331. delete refCount_;
  332. }
  333. ptr_ = 0;
  334. refCount_ = 0;
  335. }
  336. /// Pointer to the object.
  337. T* ptr_;
  338. /// Pointer to the RefCount structure.
  339. RefCount* refCount_;
  340. };
  341. /// Perform a static cast from one weak pointer type to another.
  342. template <class T, class U> WeakPtr<T> StaticCast(const WeakPtr<U>& ptr)
  343. {
  344. WeakPtr<T> ret;
  345. ret.StaticCast(ptr);
  346. return ret;
  347. }
  348. /// Perform a dynamic cast from one weak pointer type to another.
  349. template <class T, class U> WeakPtr<T> DynamicCast(const WeakPtr<U>& ptr)
  350. {
  351. WeakPtr<T> ret;
  352. ret.DynamicCast(ptr);
  353. return ret;
  354. }
  355. }