Ptr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // Copyright (c) 2008-2014 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 "../Container/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. /// Detach without destroying the object even if the refcount goes zero. To be used for scripting language interoperation.
  91. void Detach()
  92. {
  93. if (ptr_)
  94. {
  95. RefCount* refCount = RefCountPtr();
  96. ++refCount->refs_; // 2 refs
  97. Reset(); // 1 ref
  98. --refCount->refs_; // 0 refs
  99. }
  100. }
  101. /// Perform a static cast from a shared pointer of another type.
  102. template <class U> void StaticCast(const SharedPtr<U>& rhs)
  103. {
  104. ReleaseRef();
  105. ptr_ = static_cast<T*>(rhs.Get());
  106. AddRef();
  107. }
  108. /// Perform a dynamic cast from a shared pointer of another type.
  109. template <class U> void DynamicCast(const SharedPtr<U>& rhs)
  110. {
  111. ReleaseRef();
  112. ptr_ = dynamic_cast<T*>(rhs.Get());
  113. AddRef();
  114. }
  115. /// Check if the pointer is null.
  116. bool Null() const { return ptr_ == 0; }
  117. /// Check if the pointer is not null.
  118. bool NotNull() const { return ptr_ != 0; }
  119. /// Return the raw pointer.
  120. T* Get() const { return ptr_; }
  121. /// Return the object's reference count, or 0 if the pointer is null.
  122. int Refs() const { return ptr_ ? ptr_->Refs() : 0; }
  123. /// Return the object's weak reference count, or 0 if the pointer is null.
  124. int WeakRefs() const { return ptr_ ? ptr_->WeakRefs() : 0; }
  125. /// Return pointer to the RefCount structure.
  126. RefCount* RefCountPtr() const { return ptr_ ? ptr_->RefCountPtr() : 0; }
  127. /// Return hash value for HashSet & HashMap.
  128. unsigned ToHash() const { return ((unsigned)(size_t)ptr_) / sizeof(T); }
  129. private:
  130. /// Prevent direct assignment from a shared pointer of another type.
  131. template <class U> SharedPtr<T>& operator = (const SharedPtr<U>& rhs);
  132. /// Add a reference to the object pointed to.
  133. void AddRef()
  134. {
  135. if (ptr_)
  136. ptr_->AddRef();
  137. }
  138. /// Release the object reference and delete it if necessary.
  139. void ReleaseRef()
  140. {
  141. if (ptr_)
  142. {
  143. ptr_->ReleaseRef();
  144. ptr_ = 0;
  145. }
  146. }
  147. /// Pointer to the object.
  148. T* ptr_;
  149. };
  150. /// Perform a static cast from one shared pointer type to another.
  151. template <class T, class U> SharedPtr<T> StaticCast(const SharedPtr<U>& ptr)
  152. {
  153. SharedPtr<T> ret;
  154. ret.StaticCast(ptr);
  155. return ret;
  156. }
  157. /// Perform a dynamic cast from one weak pointer type to another.
  158. template <class T, class U> SharedPtr<T> DynamicCast(const SharedPtr<U>& ptr)
  159. {
  160. SharedPtr<T> ret;
  161. ret.DynamicCast(ptr);
  162. return ret;
  163. }
  164. /// Weak pointer template class with intrusive reference counting. Does not keep the object pointed to alive.
  165. template <class T> class WeakPtr
  166. {
  167. public:
  168. /// Construct a null weak pointer.
  169. WeakPtr() :
  170. ptr_(0),
  171. refCount_(0)
  172. {
  173. }
  174. /// Copy-construct from another weak pointer.
  175. WeakPtr(const WeakPtr<T>& rhs) :
  176. ptr_(rhs.ptr_),
  177. refCount_(rhs.refCount_)
  178. {
  179. AddRef();
  180. }
  181. /// Construct from a shared pointer.
  182. WeakPtr(const SharedPtr<T>& rhs) :
  183. ptr_(rhs.Get()),
  184. refCount_(rhs.RefCountPtr())
  185. {
  186. AddRef();
  187. }
  188. /// Construct from a raw pointer.
  189. explicit WeakPtr(T* ptr) :
  190. ptr_(ptr),
  191. refCount_(ptr ? ptr->RefCountPtr() : 0)
  192. {
  193. AddRef();
  194. }
  195. /// Destruct. Release the weak reference to the object.
  196. ~WeakPtr()
  197. {
  198. ReleaseRef();
  199. }
  200. /// Assign from a shared pointer.
  201. WeakPtr<T>& operator = (const SharedPtr<T>& rhs)
  202. {
  203. if (ptr_ == rhs.Get() && refCount_ == rhs.RefCountPtr())
  204. return *this;
  205. ReleaseRef();
  206. ptr_ = rhs.Get();
  207. refCount_ = rhs.RefCountPtr();
  208. AddRef();
  209. return *this;
  210. }
  211. /// Assign from a weak pointer.
  212. WeakPtr<T>& operator = (const WeakPtr<T>& rhs)
  213. {
  214. if (ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_)
  215. return *this;
  216. ReleaseRef();
  217. ptr_ = rhs.ptr_;
  218. refCount_ = rhs.refCount_;
  219. AddRef();
  220. return *this;
  221. }
  222. /// Assign from a raw pointer.
  223. WeakPtr<T>& operator = (T* ptr)
  224. {
  225. RefCount* refCount = ptr ? ptr->RefCountPtr() : 0;
  226. if (ptr_ == ptr && refCount_ == refCount)
  227. return *this;
  228. ReleaseRef();
  229. ptr_ = ptr;
  230. refCount_ = refCount;
  231. AddRef();
  232. return *this;
  233. }
  234. /// Convert to a shared pointer. If expired, return a null shared pointer.
  235. SharedPtr<T> Lock() const
  236. {
  237. if (Expired())
  238. return SharedPtr<T>();
  239. else
  240. return SharedPtr<T>(ptr_);
  241. }
  242. /// Return raw pointer. If expired, return null.
  243. T* Get() const
  244. {
  245. if (Expired())
  246. return 0;
  247. else
  248. return ptr_;
  249. }
  250. /// Point to the object.
  251. T* operator -> () const
  252. {
  253. T* rawPtr = Get();
  254. assert(rawPtr);
  255. return rawPtr;
  256. }
  257. /// Dereference the object.
  258. T& operator * () const
  259. {
  260. T* rawPtr = Get();
  261. assert(rawPtr);
  262. return *rawPtr;
  263. }
  264. /// Subscript the object if applicable.
  265. T& operator [] (const int index)
  266. {
  267. T* rawPtr = Get();
  268. assert(rawPtr);
  269. return (*rawPtr)[index];
  270. }
  271. /// Test for equality with another weak pointer.
  272. bool operator == (const WeakPtr<T>& rhs) const { return ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_; }
  273. /// Test for inequality with another weak pointer.
  274. bool operator != (const WeakPtr<T>& rhs) const { return ptr_ != rhs.ptr_ || refCount_ != rhs.refCount_; }
  275. /// Test for less than with another weak pointer.
  276. bool operator < (const WeakPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
  277. /// Convert to a raw pointer, null if the object is expired.
  278. operator T* () const { return Get(); }
  279. /// Reset to null and release the weak reference.
  280. void Reset() { ReleaseRef(); }
  281. /// Perform a static cast from a weak pointer of another type.
  282. template <class U> void StaticCast(const WeakPtr<U>& rhs)
  283. {
  284. ReleaseRef();
  285. ptr_ = static_cast<T*>(rhs.Get());
  286. refCount_ = rhs.refCount_;
  287. AddRef();
  288. }
  289. /// Perform a dynamic cast from a weak pointer of another type.
  290. template <class U> void DynamicCast(const WeakPtr<U>& rhs)
  291. {
  292. ReleaseRef();
  293. ptr_ = dynamic_cast<T*>(rhs.Get());
  294. if (ptr_)
  295. {
  296. refCount_ = rhs.refCount_;
  297. AddRef();
  298. }
  299. else
  300. refCount_ = 0;
  301. }
  302. /// Check if the pointer is null.
  303. bool Null() const { return refCount_ == 0; }
  304. /// Check if the pointer is not null.
  305. bool NotNull() const { return refCount_ != 0; }
  306. /// Return the object's reference count, or 0 if null pointer or if object has expired.
  307. int Refs() const { return (refCount_ && refCount_->refs_ >= 0) ? refCount_->refs_ : 0; }
  308. /// Return the object's weak reference count.
  309. int WeakRefs() const
  310. {
  311. if (!Expired())
  312. return ptr_->WeakRefs();
  313. else
  314. return refCount_ ? refCount_->weakRefs_ : 0;
  315. }
  316. /// Return whether the object has expired. If null pointer, always return true.
  317. bool Expired() const { return refCount_ ? refCount_->refs_ < 0 : true; }
  318. /// Return pointer to the RefCount structure.
  319. RefCount* RefCountPtr() const { return refCount_; }
  320. /// Return hash value for HashSet & HashMap.
  321. unsigned ToHash() const { return ((unsigned)(size_t)ptr_) / sizeof(T); }
  322. private:
  323. /// Prevent direct assignment from a weak pointer of different type.
  324. template <class U> WeakPtr<T>& operator = (const WeakPtr<U>& rhs);
  325. /// Add a weak reference to the object pointed to.
  326. void AddRef()
  327. {
  328. if (refCount_)
  329. {
  330. assert(refCount_->weakRefs_ >= 0);
  331. ++(refCount_->weakRefs_);
  332. }
  333. }
  334. /// Release the weak reference. Delete the Refcount structure if necessary.
  335. void ReleaseRef()
  336. {
  337. if (refCount_)
  338. {
  339. assert(refCount_->weakRefs_ > 0);
  340. --(refCount_->weakRefs_);
  341. if (Expired() && !refCount_->weakRefs_)
  342. delete refCount_;
  343. }
  344. ptr_ = 0;
  345. refCount_ = 0;
  346. }
  347. /// Pointer to the object.
  348. T* ptr_;
  349. /// Pointer to the RefCount structure.
  350. RefCount* refCount_;
  351. };
  352. /// Perform a static cast from one weak pointer type to another.
  353. template <class T, class U> WeakPtr<T> StaticCast(const WeakPtr<U>& ptr)
  354. {
  355. WeakPtr<T> ret;
  356. ret.StaticCast(ptr);
  357. return ret;
  358. }
  359. /// Perform a dynamic cast from one weak pointer type to another.
  360. template <class T, class U> WeakPtr<T> DynamicCast(const WeakPtr<U>& ptr)
  361. {
  362. WeakPtr<T> ret;
  363. ret.DynamicCast(ptr);
  364. return ret;
  365. }
  366. }