Ptr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. /// Return true if the pointer is not null.
  87. operator bool () const { return ptr_ != 0; }
  88. /// Convert to a raw pointer.
  89. operator T* () const { return ptr_; }
  90. /// Reset to null and release the object reference.
  91. void Reset() { ReleaseRef(); }
  92. /// Perform a static cast from a shared pointer of another type.
  93. template <class U> void StaticCast(const SharedPtr<U>& rhs)
  94. {
  95. ReleaseRef();
  96. ptr_ = static_cast<T*>(rhs.Get());
  97. AddRef();
  98. }
  99. /// Perform a dynamic cast from a shared pointer of another type.
  100. template <class U> void DynamicCast(const SharedPtr<U>& rhs)
  101. {
  102. ReleaseRef();
  103. ptr_ = dynamic_cast<T*>(rhs.Get());
  104. AddRef();
  105. }
  106. /// Check if the pointer is null.
  107. bool Null() const { return ptr_ == 0; }
  108. /// Check if the pointer is not null.
  109. bool NotNull() const { return ptr_ != 0; }
  110. /// Return the raw pointer.
  111. T* Get() const { return ptr_; }
  112. /// Return the object's reference count, or 0 if the pointer is null.
  113. int Refs() const { return ptr_ ? ptr_->Refs() : 0; }
  114. /// Return the object's weak reference count, or 0 if the pointer is null.
  115. int WeakRefs() const { return ptr_ ? ptr_->WeakRefs() : 0; }
  116. /// Return pointer to the RefCount structure.
  117. RefCount* RefCountPtr() const { return ptr_ ? ptr_->RefCountPtr() : 0; }
  118. /// Return hash value for HashSet & HashMap.
  119. unsigned ToHash() const { return ((unsigned)(size_t)ptr_) / sizeof(T); }
  120. private:
  121. /// Prevent direct assignment from a shared pointer of another type.
  122. template <class U> SharedPtr<T>& operator = (const SharedPtr<U>& rhs);
  123. /// Add a reference to the object pointed to.
  124. void AddRef()
  125. {
  126. if (ptr_)
  127. ptr_->AddRef();
  128. }
  129. /// Release the object reference and delete it if necessary.
  130. void ReleaseRef()
  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 with intrusive reference counting. Does not keep the object pointed to 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. /// Copy-construct from another weak pointer.
  166. WeakPtr(const WeakPtr<T>& rhs) :
  167. ptr_(rhs.ptr_),
  168. refCount_(rhs.refCount_)
  169. {
  170. AddRef();
  171. }
  172. /// Construct from a shared pointer.
  173. WeakPtr(const SharedPtr<T>& rhs) :
  174. ptr_(rhs.Get()),
  175. refCount_(rhs.RefCountPtr())
  176. {
  177. AddRef();
  178. }
  179. /// Construct from a raw pointer.
  180. explicit WeakPtr(T* ptr) :
  181. ptr_(ptr),
  182. refCount_(ptr ? ptr->RefCountPtr() : 0)
  183. {
  184. AddRef();
  185. }
  186. /// Destruct. Release the weak reference to the object.
  187. ~WeakPtr()
  188. {
  189. ReleaseRef();
  190. }
  191. /// Assign from a shared pointer.
  192. WeakPtr<T>& operator = (const SharedPtr<T>& rhs)
  193. {
  194. if (ptr_ == rhs.Get() && refCount_ == rhs.RefCountPtr())
  195. return *this;
  196. ReleaseRef();
  197. ptr_ = rhs.Get();
  198. refCount_ = rhs.RefCountPtr();
  199. AddRef();
  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. ReleaseRef();
  208. ptr_ = rhs.ptr_;
  209. refCount_ = rhs.refCount_;
  210. AddRef();
  211. return *this;
  212. }
  213. /// Assign from a raw pointer.
  214. WeakPtr<T>& operator = (T* ptr)
  215. {
  216. RefCount* refCount = ptr ? ptr->RefCountPtr() : 0;
  217. if (ptr_ == ptr && refCount_ == refCount)
  218. return *this;
  219. ReleaseRef();
  220. ptr_ = ptr;
  221. refCount_ = refCount;
  222. AddRef();
  223. return *this;
  224. }
  225. /// Convert to a shared pointer. If expired, return a null shared pointer.
  226. SharedPtr<T> Lock() const
  227. {
  228. if (Expired())
  229. return SharedPtr<T>();
  230. else
  231. return SharedPtr<T>(ptr_);
  232. }
  233. /// Return raw pointer. If expired, return null.
  234. T* Get() const
  235. {
  236. if (Expired())
  237. return 0;
  238. else
  239. return ptr_;
  240. }
  241. /// Point to the object.
  242. T* operator -> () const
  243. {
  244. T* rawPtr = Get();
  245. assert(rawPtr);
  246. return rawPtr;
  247. }
  248. /// Dereference the object.
  249. T& operator * () const
  250. {
  251. T* rawPtr = Get();
  252. assert(rawPtr);
  253. return *rawPtr;
  254. }
  255. /// Subscript the object if applicable.
  256. T& operator [] (const int index)
  257. {
  258. T* rawPtr = Get();
  259. assert(rawPtr);
  260. return (*rawPtr)[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 !Expired(); }
  270. /// Convert to a raw pointer, null if the object is expired.
  271. operator T* () const { return Get(); }
  272. /// Reset to null and release the weak reference.
  273. void Reset() { ReleaseRef(); }
  274. /// Perform a static cast from a weak pointer of another type.
  275. template <class U> void StaticCast(const WeakPtr<U>& rhs)
  276. {
  277. ReleaseRef();
  278. ptr_ = static_cast<T*>(rhs.Get());
  279. refCount_ = rhs.refCount_;
  280. AddRef();
  281. }
  282. /// Perform a dynamic cast from a weak pointer of another type.
  283. template <class U> void DynamicCast(const WeakPtr<U>& rhs)
  284. {
  285. ReleaseRef();
  286. ptr_ = dynamic_cast<T*>(rhs.Get());
  287. if (ptr_)
  288. {
  289. refCount_ = rhs.refCount_;
  290. AddRef();
  291. }
  292. else
  293. refCount_ = 0;
  294. }
  295. /// Check if the pointer is null.
  296. bool Null() const { return refCount_ == 0; }
  297. /// Check if the pointer is not null.
  298. bool NotNull() const { return refCount_ != 0; }
  299. /// Return the object's reference count, or 0 if null pointer or if object has expired.
  300. int Refs() const { return (refCount_ && refCount_->refs_ >= 0) ? refCount_->refs_ : 0; }
  301. /// Return the object's weak reference count.
  302. int WeakRefs() const
  303. {
  304. if (!Expired())
  305. return ptr_->WeakRefs();
  306. else
  307. return refCount_ ? refCount_->weakRefs_ : 0;
  308. }
  309. /// Return whether the object has expired. If null pointer, always return true.
  310. bool Expired() const { return refCount_ ? refCount_->refs_ < 0 : true; }
  311. /// Return pointer to the RefCount structure.
  312. RefCount* RefCountPtr() const { return refCount_; }
  313. /// Return hash value for HashSet & HashMap.
  314. unsigned ToHash() const { return ((unsigned)(size_t)ptr_) / sizeof(T); }
  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. /// Add a weak reference to the object pointed to.
  319. void AddRef()
  320. {
  321. if (refCount_)
  322. {
  323. assert(refCount_->weakRefs_ >= 0);
  324. ++(refCount_->weakRefs_);
  325. }
  326. }
  327. /// Release the weak reference. Delete the Refcount structure if necessary.
  328. void ReleaseRef()
  329. {
  330. if (refCount_)
  331. {
  332. assert(refCount_->weakRefs_ > 0);
  333. --(refCount_->weakRefs_);
  334. if (Expired() && !refCount_->weakRefs_)
  335. delete refCount_;
  336. }
  337. ptr_ = 0;
  338. refCount_ = 0;
  339. }
  340. /// Pointer to the object.
  341. T* ptr_;
  342. /// Pointer to the RefCount structure.
  343. RefCount* refCount_;
  344. };
  345. /// Perform a static cast from one weak pointer type to another.
  346. template <class T, class U> WeakPtr<T> StaticCast(const WeakPtr<U>& ptr)
  347. {
  348. WeakPtr<T> ret;
  349. ret.StaticCast(ptr);
  350. return ret;
  351. }
  352. /// Perform a dynamic cast from one weak pointer type to another.
  353. template <class T, class U> WeakPtr<T> DynamicCast(const WeakPtr<U>& ptr)
  354. {
  355. WeakPtr<T> ret;
  356. ret.DynamicCast(ptr);
  357. return ret;
  358. }
  359. }