v8  9.0.257(node16.0.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 <type_traits>
11 
12 namespace cppgc {
13 
14 class Visitor;
15 
16 namespace internal {
17 template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
18  typename CheckingPolicy>
19 class BasicMember;
22 class StrongMemberTag;
23 class UntracedMemberTag;
24 class WeakMemberTag;
25 
26 // Pre-C++17 custom implementation of std::void_t.
27 template <typename... Ts>
28 struct make_void {
29  typedef void type;
30 };
31 template <typename... Ts>
32 using void_t = typename make_void<Ts...>::type;
33 
34 // Not supposed to be specialized by the user.
35 template <typename T>
36 struct IsWeak : std::false_type {};
37 
38 // IsTraceMethodConst is used to verify that all Trace methods are marked as
39 // const. It is equivalent to IsTraceable but for a non-const object.
40 template <typename T, typename = void>
41 struct IsTraceMethodConst : std::false_type {};
42 
43 template <typename T>
44 struct IsTraceMethodConst<T, void_t<decltype(std::declval<const T>().Trace(
45  std::declval<Visitor*>()))>> : std::true_type {
46 };
47 
48 template <typename T, typename = void>
49 struct IsTraceable : std::false_type {
50  static_assert(sizeof(T), "T must be fully defined");
51 };
52 
53 template <typename T>
54 struct IsTraceable<
55  T, void_t<decltype(std::declval<T>().Trace(std::declval<Visitor*>()))>>
56  : std::true_type {
57  // All Trace methods should be marked as const. If an object of type
58  // 'T' is traceable then any object of type 'const T' should also
59  // be traceable.
60  static_assert(IsTraceMethodConst<T>(),
61  "Trace methods should be marked as const.");
62 };
63 
64 template <typename T>
65 constexpr bool IsTraceableV = IsTraceable<T>::value;
66 
67 template <typename T, typename = void>
68 struct HasGarbageCollectedMixinTypeMarker : std::false_type {
69  static_assert(sizeof(T), "T must be fully defined");
70 };
71 
72 template <typename T>
74  T,
75  void_t<typename std::remove_const_t<T>::IsGarbageCollectedMixinTypeMarker>>
76  : std::true_type {
77  static_assert(sizeof(T), "T must be fully defined");
78 };
79 
80 template <typename T, typename = void>
81 struct HasGarbageCollectedTypeMarker : std::false_type {
82  static_assert(sizeof(T), "T must be fully defined");
83 };
84 
85 template <typename T>
87  T, void_t<typename std::remove_const_t<T>::IsGarbageCollectedTypeMarker>>
88  : std::true_type {
89  static_assert(sizeof(T), "T must be fully defined");
90 };
91 
92 template <typename T, bool = HasGarbageCollectedTypeMarker<T>::value,
93  bool = HasGarbageCollectedMixinTypeMarker<T>::value>
94 struct IsGarbageCollectedMixinType : std::false_type {
95  static_assert(sizeof(T), "T must be fully defined");
96 };
97 
98 template <typename T>
99 struct IsGarbageCollectedMixinType<T, false, true> : std::true_type {
100  static_assert(sizeof(T), "T must be fully defined");
101 };
102 
103 template <typename T, bool = HasGarbageCollectedTypeMarker<T>::value>
104 struct IsGarbageCollectedType : std::false_type {
105  static_assert(sizeof(T), "T must be fully defined");
106 };
107 
108 template <typename T>
109 struct IsGarbageCollectedType<T, true> : std::true_type {
110  static_assert(sizeof(T), "T must be fully defined");
111 };
112 
113 template <typename T>
115  : std::integral_constant<bool, IsGarbageCollectedType<T>::value ||
116  IsGarbageCollectedMixinType<T>::value> {
117  static_assert(sizeof(T), "T must be fully defined");
118 };
119 
120 template <typename T, bool = (HasGarbageCollectedTypeMarker<T>::value &&
122 struct IsGarbageCollectedWithMixinType : std::false_type {
123  static_assert(sizeof(T), "T must be fully defined");
124 };
125 
126 template <typename T>
127 struct IsGarbageCollectedWithMixinType<T, true> : std::true_type {
128  static_assert(sizeof(T), "T must be fully defined");
129 };
130 
131 template <typename BasicMemberCandidate, typename WeaknessTag,
132  typename WriteBarrierPolicy>
134  private:
135  template <typename T, typename CheckingPolicy>
136  static std::true_type SubclassCheck(
137  BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy>*);
138  static std::false_type SubclassCheck(...);
139 
140  public:
141  static constexpr bool value =
142  decltype(SubclassCheck(std::declval<BasicMemberCandidate*>()))::value;
143 };
144 
145 template <typename T,
147  T, StrongMemberTag, DijkstraWriteBarrierPolicy>::value>
148 struct IsMemberType : std::false_type {};
149 
150 template <typename T>
151 struct IsMemberType<T, true> : std::true_type {};
152 
153 template <typename T, bool = IsSubclassOfBasicMemberTemplate<
154  T, WeakMemberTag, DijkstraWriteBarrierPolicy>::value>
155 struct IsWeakMemberType : std::false_type {};
156 
157 template <typename T>
158 struct IsWeakMemberType<T, true> : std::true_type {};
159 
160 template <typename T, bool = IsSubclassOfBasicMemberTemplate<
161  T, UntracedMemberTag, NoWriteBarrierPolicy>::value>
162 struct IsUntracedMemberType : std::false_type {};
163 
164 template <typename T>
165 struct IsUntracedMemberType<T, true> : std::true_type {};
166 
167 } // namespace internal
168 
169 /**
170  * Value is true for types that inherit from `GarbageCollectedMixin` but not
171  * `GarbageCollected<T>` (i.e., they are free mixins), and false otherwise.
172  */
173 template <typename T>
176 
177 /**
178  * Value is true for types that inherit from `GarbageCollected<T>`, and false
179  * otherwise.
180  */
181 template <typename T>
182 constexpr bool IsGarbageCollectedTypeV =
184 
185 /**
186  * Value is true for types that inherit from either `GarbageCollected<T>` or
187  * `GarbageCollectedMixin`, and false otherwise.
188  */
189 template <typename T>
192 
193 /**
194  * Value is true for types that inherit from `GarbageCollected<T>` and
195  * `GarbageCollectedMixin`, and false otherwise.
196  */
197 template <typename T>
200 
201 /**
202  * Value is true for types of type `Member<T>`, and false otherwise.
203  */
204 template <typename T>
206 
207 /**
208  * Value is true for types of type `UntracedMember<T>`, and false otherwise.
209  */
210 template <typename T>
212 
213 /**
214  * Value is true for types of type `WeakMember<T>`, and false otherwise.
215  */
216 template <typename T>
218 
219 /**
220  * Value is true for types that are considered weak references, and false
221  * otherwise.
222  */
223 template <typename T>
224 constexpr bool IsWeakV = internal::IsWeak<T>::value;
225 
226 } // namespace cppgc
227 
228 #endif // INCLUDE_CPPGC_TYPE_TRAITS_H_
cppgc::internal::IsMemberType
Definition: type-traits.h:148
cppgc::internal::IsTraceable
Definition: type-traits.h:49
cppgc::IsGarbageCollectedTypeV
constexpr bool IsGarbageCollectedTypeV
Definition: type-traits.h:182
cppgc::internal::IsTraceMethodConst
Definition: type-traits.h:41
cppgc::internal::IsSubclassOfBasicMemberTemplate::value
static constexpr bool value
Definition: type-traits.h:141
cppgc::internal::IsWeak
Definition: type-traits.h:36
cppgc::internal::DijkstraWriteBarrierPolicy
Definition: pointer-policies.h:25
cppgc::internal::IsGarbageCollectedType
Definition: type-traits.h:104
cppgc
Definition: allocation.h:17
cppgc::IsWeakMemberTypeV
constexpr bool IsWeakMemberTypeV
Definition: type-traits.h:217
cppgc::internal::HasGarbageCollectedMixinTypeMarker
Definition: type-traits.h:68
cppgc::internal::IsUntracedMemberType
Definition: type-traits.h:162
cppgc::internal::make_void
Definition: type-traits.h:28
cppgc::Visitor
Definition: visitor.h:52
cppgc::internal::IsGarbageCollectedOrMixinType
Definition: type-traits.h:114
cppgc::internal::make_void::type
void type
Definition: type-traits.h:29
cppgc::internal::IsGarbageCollectedWithMixinType
Definition: type-traits.h:122
cppgc::IsGarbageCollectedWithMixinTypeV
constexpr bool IsGarbageCollectedWithMixinTypeV
Definition: type-traits.h:198
cppgc::IsGarbageCollectedOrMixinTypeV
constexpr bool IsGarbageCollectedOrMixinTypeV
Definition: type-traits.h:190
cppgc::internal::IsGarbageCollectedMixinType
Definition: type-traits.h:94
cppgc::internal::IsTraceableV
constexpr bool IsTraceableV
Definition: type-traits.h:65
cppgc::internal::IsWeakMemberType
Definition: type-traits.h:155
cppgc::internal::NoWriteBarrierPolicy
Definition: pointer-policies.h:45
cppgc::internal
Definition: allocation.h:22
cppgc::internal::IsSubclassOfBasicMemberTemplate
Definition: type-traits.h:133
cppgc::IsMemberTypeV
constexpr bool IsMemberTypeV
Definition: type-traits.h:205
cppgc::IsGarbageCollectedMixinTypeV
constexpr bool IsGarbageCollectedMixinTypeV
Definition: type-traits.h:174
cppgc::IsUntracedMemberTypeV
constexpr bool IsUntracedMemberTypeV
Definition: type-traits.h:211
cppgc::IsWeakV
constexpr bool IsWeakV
Definition: type-traits.h:224
cppgc::internal::HasGarbageCollectedTypeMarker
Definition: type-traits.h:81