RefPtr.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. // RefPtr and PassRefPtr are documented at http://webkit.org/coding/RefPtr.html
  21. #ifndef WTF_RefPtr_h
  22. #define WTF_RefPtr_h
  23. #include <algorithm>
  24. #include <utility>
  25. #include <wtf/FastAllocBase.h>
  26. #include <wtf/PassRefPtr.h>
  27. namespace WTF {
  28. enum PlacementNewAdoptType { PlacementNewAdopt };
  29. template<typename T> class PassRefPtr;
  30. enum HashTableDeletedValueType { HashTableDeletedValue };
  31. template<typename T> class RefPtr {
  32. WTF_MAKE_FAST_ALLOCATED;
  33. public:
  34. ALWAYS_INLINE RefPtr() : m_ptr(0) { }
  35. ALWAYS_INLINE RefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); }
  36. ALWAYS_INLINE RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { refIfNotNull(m_ptr); }
  37. template<typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { refIfNotNull(m_ptr); }
  38. #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
  39. ALWAYS_INLINE RefPtr(RefPtr&& o) : m_ptr(o.release().leakRef()) { }
  40. template<typename U> RefPtr(RefPtr<U>&& o) : m_ptr(o.release().leakRef()) { }
  41. #endif
  42. // See comments in PassRefPtr.h for an explanation of why this takes a const reference.
  43. template<typename U> RefPtr(const PassRefPtr<U>&);
  44. // Special constructor for cases where we overwrite an object in place.
  45. ALWAYS_INLINE RefPtr(PlacementNewAdoptType) { }
  46. // Hash table deleted values, which are only constructed and never copied or destroyed.
  47. RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
  48. bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
  49. ALWAYS_INLINE ~RefPtr() { derefIfNotNull(m_ptr); }
  50. T* get() const { return m_ptr; }
  51. void clear();
  52. PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; }
  53. T& operator*() const { return *m_ptr; }
  54. ALWAYS_INLINE T* operator->() const { return m_ptr; }
  55. bool operator!() const { return !m_ptr; }
  56. // This conversion operator allows implicit conversion to bool but not to other integer types.
  57. typedef T* (RefPtr::*UnspecifiedBoolType);
  58. operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0; }
  59. RefPtr& operator=(const RefPtr&);
  60. RefPtr& operator=(T*);
  61. RefPtr& operator=(const PassRefPtr<T>&);
  62. #if !COMPILER_SUPPORTS(CXX_NULLPTR)
  63. RefPtr& operator=(std::nullptr_t) { clear(); return *this; }
  64. #endif
  65. template<typename U> RefPtr& operator=(const RefPtr<U>&);
  66. template<typename U> RefPtr& operator=(const PassRefPtr<U>&);
  67. #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
  68. RefPtr& operator=(RefPtr&&);
  69. template<typename U> RefPtr& operator=(RefPtr<U>&&);
  70. #endif
  71. void swap(RefPtr&);
  72. static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
  73. private:
  74. T* m_ptr;
  75. };
  76. template<typename T> template<typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
  77. : m_ptr(o.leakRef())
  78. {
  79. }
  80. template<typename T> inline void RefPtr<T>::clear()
  81. {
  82. T* ptr = m_ptr;
  83. m_ptr = 0;
  84. derefIfNotNull(ptr);
  85. }
  86. template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
  87. {
  88. T* optr = o.get();
  89. refIfNotNull(optr);
  90. T* ptr = m_ptr;
  91. m_ptr = optr;
  92. derefIfNotNull(ptr);
  93. return *this;
  94. }
  95. template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
  96. {
  97. T* optr = o.get();
  98. refIfNotNull(optr);
  99. T* ptr = m_ptr;
  100. m_ptr = optr;
  101. derefIfNotNull(ptr);
  102. return *this;
  103. }
  104. template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
  105. {
  106. refIfNotNull(optr);
  107. T* ptr = m_ptr;
  108. m_ptr = optr;
  109. derefIfNotNull(ptr);
  110. return *this;
  111. }
  112. template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
  113. {
  114. T* ptr = m_ptr;
  115. m_ptr = o.leakRef();
  116. derefIfNotNull(ptr);
  117. return *this;
  118. }
  119. template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
  120. {
  121. T* ptr = m_ptr;
  122. m_ptr = o.leakRef();
  123. derefIfNotNull(ptr);
  124. return *this;
  125. }
  126. #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
  127. template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(RefPtr<T>&& o)
  128. {
  129. RefPtr<T> ptr = std::move(o);
  130. swap(ptr);
  131. return *this;
  132. }
  133. template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(RefPtr<U>&& o)
  134. {
  135. RefPtr<T> ptr = std::move(o);
  136. swap(ptr);
  137. return *this;
  138. }
  139. #endif
  140. template<class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
  141. {
  142. std::swap(m_ptr, o.m_ptr);
  143. }
  144. template<class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
  145. {
  146. a.swap(b);
  147. }
  148. template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
  149. {
  150. return a.get() == b.get();
  151. }
  152. template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
  153. {
  154. return a.get() == b;
  155. }
  156. template<typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
  157. {
  158. return a == b.get();
  159. }
  160. template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
  161. {
  162. return a.get() != b.get();
  163. }
  164. template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
  165. {
  166. return a.get() != b;
  167. }
  168. template<typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
  169. {
  170. return a != b.get();
  171. }
  172. template<typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
  173. {
  174. return RefPtr<T>(static_cast<T*>(p.get()));
  175. }
  176. template<typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
  177. {
  178. return RefPtr<T>(const_cast<T*>(p.get()));
  179. }
  180. template<typename T> inline T* getPtr(const RefPtr<T>& p)
  181. {
  182. return p.get();
  183. }
  184. } // namespace WTF
  185. using WTF::RefPtr;
  186. using WTF::static_pointer_cast;
  187. using WTF::const_pointer_cast;
  188. #endif // WTF_RefPtr_h