Ptr.h 13 KB

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