GCPtr.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Traits.h>
  8. #include <AK/Types.h>
  9. namespace JS {
  10. template<typename T>
  11. class GCPtr;
  12. template<typename T>
  13. class NonnullGCPtr {
  14. public:
  15. NonnullGCPtr() = delete;
  16. NonnullGCPtr(T& ptr)
  17. : m_ptr(&ptr)
  18. {
  19. }
  20. template<typename U>
  21. NonnullGCPtr(U& ptr)
  22. requires(IsConvertible<U*, T*>)
  23. : m_ptr(&static_cast<T&>(ptr))
  24. {
  25. }
  26. template<typename U>
  27. NonnullGCPtr(NonnullGCPtr<U> const& other)
  28. requires(IsConvertible<U*, T*>)
  29. : m_ptr(other.ptr())
  30. {
  31. }
  32. template<typename U>
  33. NonnullGCPtr& operator=(NonnullGCPtr<U> const& other)
  34. requires(IsConvertible<U*, T*>)
  35. {
  36. m_ptr = static_cast<T*>(other.ptr());
  37. return *this;
  38. }
  39. NonnullGCPtr& operator=(T& other)
  40. {
  41. m_ptr = &other;
  42. return *this;
  43. }
  44. template<typename U>
  45. NonnullGCPtr& operator=(U& other)
  46. requires(IsConvertible<U*, T*>)
  47. {
  48. m_ptr = &static_cast<T&>(other);
  49. return *this;
  50. }
  51. RETURNS_NONNULL T* operator->() const { return m_ptr; }
  52. T& operator*() const { return *m_ptr; }
  53. RETURNS_NONNULL T* ptr() const { return m_ptr; }
  54. RETURNS_NONNULL operator T*() const { return m_ptr; }
  55. operator T&() const { return *m_ptr; }
  56. private:
  57. T* m_ptr { nullptr };
  58. };
  59. template<typename T>
  60. class GCPtr {
  61. public:
  62. constexpr GCPtr() = default;
  63. GCPtr(T& ptr)
  64. : m_ptr(&ptr)
  65. {
  66. }
  67. GCPtr(T* ptr)
  68. : m_ptr(ptr)
  69. {
  70. }
  71. template<typename U>
  72. GCPtr(GCPtr<U> const& other)
  73. requires(IsConvertible<U*, T*>)
  74. : m_ptr(other.ptr())
  75. {
  76. }
  77. GCPtr(NonnullGCPtr<T> const& other)
  78. : m_ptr(other.ptr())
  79. {
  80. }
  81. template<typename U>
  82. GCPtr(NonnullGCPtr<U> const& other)
  83. requires(IsConvertible<U*, T*>)
  84. : m_ptr(other.ptr())
  85. {
  86. }
  87. GCPtr(nullptr_t)
  88. : m_ptr(nullptr)
  89. {
  90. }
  91. template<typename U>
  92. GCPtr& operator=(GCPtr<U> const& other)
  93. requires(IsConvertible<U*, T*>)
  94. {
  95. m_ptr = static_cast<T*>(other.ptr());
  96. return *this;
  97. }
  98. GCPtr& operator=(NonnullGCPtr<T> const& other)
  99. {
  100. m_ptr = other.ptr();
  101. return *this;
  102. }
  103. template<typename U>
  104. GCPtr& operator=(NonnullGCPtr<U> const& other)
  105. requires(IsConvertible<U*, T*>)
  106. {
  107. m_ptr = static_cast<T*>(other.ptr());
  108. return *this;
  109. }
  110. GCPtr& operator=(T& other)
  111. {
  112. m_ptr = &other;
  113. return *this;
  114. }
  115. template<typename U>
  116. GCPtr& operator=(U& other)
  117. requires(IsConvertible<U*, T*>)
  118. {
  119. m_ptr = &static_cast<T&>(other);
  120. return *this;
  121. }
  122. GCPtr& operator=(T* other)
  123. {
  124. m_ptr = other;
  125. return *this;
  126. }
  127. template<typename U>
  128. GCPtr& operator=(U* other)
  129. requires(IsConvertible<U*, T*>)
  130. {
  131. m_ptr = static_cast<T*>(other);
  132. return *this;
  133. }
  134. T* operator->() const
  135. {
  136. VERIFY(m_ptr);
  137. return m_ptr;
  138. }
  139. T& operator*() const
  140. {
  141. VERIFY(m_ptr);
  142. return *m_ptr;
  143. }
  144. T* ptr() const { return m_ptr; }
  145. operator bool() const { return !!m_ptr; }
  146. bool operator!() const { return !m_ptr; }
  147. operator T*() const { return m_ptr; }
  148. private:
  149. T* m_ptr { nullptr };
  150. };
  151. template<typename T, typename U>
  152. inline bool operator==(GCPtr<T> const& a, GCPtr<U> const& b)
  153. {
  154. return a.ptr() == b.ptr();
  155. }
  156. template<typename T, typename U>
  157. inline bool operator==(GCPtr<T> const& a, NonnullGCPtr<U> const& b)
  158. {
  159. return a.ptr() == b.ptr();
  160. }
  161. template<typename T, typename U>
  162. inline bool operator==(NonnullGCPtr<T> const& a, NonnullGCPtr<U> const& b)
  163. {
  164. return a.ptr() == b.ptr();
  165. }
  166. template<typename T, typename U>
  167. inline bool operator==(NonnullGCPtr<T> const& a, GCPtr<U> const& b)
  168. {
  169. return a.ptr() == b.ptr();
  170. }
  171. }
  172. namespace AK {
  173. template<typename T>
  174. struct Traits<JS::GCPtr<T>> : public DefaultTraits<JS::GCPtr<T>> {
  175. static unsigned hash(JS::GCPtr<T> const& value)
  176. {
  177. return Traits<T*>::hash(value.ptr());
  178. }
  179. };
  180. template<typename T>
  181. struct Traits<JS::NonnullGCPtr<T>> : public DefaultTraits<JS::NonnullGCPtr<T>> {
  182. static unsigned hash(JS::NonnullGCPtr<T> const& value)
  183. {
  184. return Traits<T*>::hash(value.ptr());
  185. }
  186. };
  187. }