Ptr.h 12 KB

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