v8  9.4.146 (node 16.13.0)
V8 is Google's open source JavaScript engine
type-traits.h
Go to the documentation of this file.
1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef INCLUDE_CPPGC_TYPE_TRAITS_H_
6 #define INCLUDE_CPPGC_TYPE_TRAITS_H_
7 
8 // This file should stay with minimal dependencies to allow embedder to check
9 // against Oilpan types without including any other parts.
10 #include <cstddef>
11 #include <type_traits>
12 
13 namespace cppgc {
14 
15 class Visitor;
16 
17 namespace internal {
18 template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
19  typename CheckingPolicy>
20 class BasicMember;
23 class StrongMemberTag;
24 class UntracedMemberTag;
25 class WeakMemberTag;
26 
27 // Pre-C++17 custom implementation of std::void_t.
28 template <typename... Ts>
29 struct make_void {
30  typedef void type;
31 };
32 template <typename... Ts>
33 using void_t = typename make_void<Ts...>::type;
34 
35 // Not supposed to be specialized by the user.
36 template <typename T>
37 struct IsWeak : std::false_type {};
38 
39 // IsTraceMethodConst is used to verify that all Trace methods are marked as
40 // const. It is equivalent to IsTraceable but for a non-const object.
41 template <typename T, typename = void>
42 struct IsTraceMethodConst : std::false_type {};
43 
44 template <typename T>
45 struct IsTraceMethodConst<T, void_t<decltype(std::declval<const T>().Trace(
46  std::declval<Visitor*>()))>> : std::true_type {
47 };
48 
49 template <typename T, typename = void>
50 struct IsTraceable : std::false_type {
51  static_assert(sizeof(T), "T must be fully defined");
52 };
53 
54 template <typename T>
55 struct IsTraceable<
56  T, void_t<decltype(std::declval<T>().Trace(std::declval<Visitor*>()))>>
57  : std::true_type {
58  // All Trace methods should be marked as const. If an object of type
59  // 'T' is traceable then any object of type 'const T' should also
60  // be traceable.
61  static_assert(IsTraceMethodConst<T>(),
62  "Trace methods should be marked as const.");
63 };
64 
65 template <typename T>
66 constexpr bool IsTraceableV = IsTraceable<T>::value;
67 
68 template <typename T, typename = void>
69 struct HasGarbageCollectedMixinTypeMarker : std::false_type {
70  static_assert(sizeof(T), "T must be fully defined");
71 };
72 
73 template <typename T>
75  T,
76  void_t<typename std::remove_const_t<T>::IsGarbageCollectedMixinTypeMarker>>
77  : std::true_type {
78  static_assert(sizeof(T), "T must be fully defined");
79 };
80 
81 template <typename T, typename = void>
82 struct HasGarbageCollectedTypeMarker : std::false_type {
83  static_assert(sizeof(T), "T must be fully defined");
84 };
85 
86 template <typename T>
88  T, void_t<typename std::remove_const_t<T>::IsGarbageCollectedTypeMarker>>
89  : std::true_type {
90  static_assert(sizeof(T), "T must be fully defined");
91 };
92 
93 template <typename T, bool = HasGarbageCollectedTypeMarker<T>::value,
94  bool = HasGarbageCollectedMixinTypeMarker<T>::value>
95 struct IsGarbageCollectedMixinType : std::false_type {
96  static_assert(sizeof(T), "T must be fully defined");
97 };
98 
99 template <typename T>
100 struct IsGarbageCollectedMixinType<T, false, true> : std::true_type {
101  static_assert(sizeof(T), "T must be fully defined");
102 };
103 
104 template <typename T, bool = HasGarbageCollectedTypeMarker<T>::value>
105 struct IsGarbageCollectedType : std::false_type {
106  static_assert(sizeof(T), "T must be fully defined");
107 };
108 
109 template <typename T>
110 struct IsGarbageCollectedType<T, true> : std::true_type {
111  static_assert(sizeof(T), "T must be fully defined");
112 };
113 
114 template <typename T>
116  : std::integral_constant<bool, IsGarbageCollectedType<T>::value ||
117  IsGarbageCollectedMixinType<T>::value> {
118  static_assert(sizeof(T), "T must be fully defined");
119 };
120 
121 template <typename T, bool = (HasGarbageCollectedTypeMarker<T>::value &&
123 struct IsGarbageCollectedWithMixinType : std::false_type {
124  static_assert(sizeof(T), "T must be fully defined");
125 };
126 
127 template <typename T>
128 struct IsGarbageCollectedWithMixinType<T, true> : std::true_type {
129  static_assert(sizeof(T), "T must be fully defined");
130 };
131 
132 template <typename BasicMemberCandidate, typename WeaknessTag,
133  typename WriteBarrierPolicy>
135  private:
136  template <typename T, typename CheckingPolicy>
137  static std::true_type SubclassCheck(
138  BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy>*);
139  static std::false_type SubclassCheck(...);
140 
141  public:
142  static constexpr bool value =
143  decltype(SubclassCheck(std::declval<BasicMemberCandidate*>()))::value;
144 };
145 
146 template <typename T,
148  T, StrongMemberTag, DijkstraWriteBarrierPolicy>::value>
149 struct IsMemberType : std::false_type {};
150 
151 template <typename T>
152 struct IsMemberType<T, true> : std::true_type {};
153 
154 template <typename T, bool = IsSubclassOfBasicMemberTemplate<
155  T, WeakMemberTag, DijkstraWriteBarrierPolicy>::value>
156 struct IsWeakMemberType : std::false_type {};
157 
158 template <typename T>
159 struct IsWeakMemberType<T, true> : std::true_type {};
160 
161 template <typename T, bool = IsSubclassOfBasicMemberTemplate<
162  T, UntracedMemberTag, NoWriteBarrierPolicy>::value>
163 struct IsUntracedMemberType : std::false_type {};
164 
165 template <typename T>
166 struct IsUntracedMemberType<T, true> : std::true_type {};
167 
168 template <typename T>
169 struct IsComplete {
170  private:
171  template <typename U, size_t = sizeof(U)>
172  static std::true_type IsSizeOfKnown(U*);
173  static std::false_type IsSizeOfKnown(...);
174 
175  public:
176  static constexpr bool value =
177  decltype(IsSizeOfKnown(std::declval<T*>()))::value;
178 };
179 
180 } // namespace internal
181 
182 /**
183  * Value is true for types that inherit from `GarbageCollectedMixin` but not
184  * `GarbageCollected<T>` (i.e., they are free mixins), and false otherwise.
185  */
186 template <typename T>
189 
190 /**
191  * Value is true for types that inherit from `GarbageCollected<T>`, and false
192  * otherwise.
193  */
194 template <typename T>
195 constexpr bool IsGarbageCollectedTypeV =
197 
198 /**
199  * Value is true for types that inherit from either `GarbageCollected<T>` or
200  * `GarbageCollectedMixin`, and false otherwise.
201  */
202 template <typename T>
205 
206 /**
207  * Value is true for types that inherit from `GarbageCollected<T>` and
208  * `GarbageCollectedMixin`, and false otherwise.
209  */
210 template <typename T>
213 
214 /**
215  * Value is true for types of type `Member<T>`, and false otherwise.
216  */
217 template <typename T>
219 
220 /**
221  * Value is true for types of type `UntracedMember<T>`, and false otherwise.
222  */
223 template <typename T>
225 
226 /**
227  * Value is true for types of type `WeakMember<T>`, and false otherwise.
228  */
229 template <typename T>
231 
232 /**
233  * Value is true for types that are considered weak references, and false
234  * otherwise.
235  */
236 template <typename T>
237 constexpr bool IsWeakV = internal::IsWeak<T>::value;
238 
239 /**
240  * Value is true for types that are complete, and false otherwise.
241  */
242 template <typename T>
243 constexpr bool IsCompleteV = internal::IsComplete<T>::value;
244 
245 } // namespace cppgc
246 
247 #endif // INCLUDE_CPPGC_TYPE_TRAITS_H_