Ptr.h 12 KB

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