Loading...
Searching...
No Matches
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#include <utility>
13
14namespace cppgc {
15
16class Visitor;
17
18namespace internal {
19template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
20 typename CheckingPolicy, typename StorageType>
21class BasicMember;
22struct DijkstraWriteBarrierPolicy;
23struct NoWriteBarrierPolicy;
24class StrongMemberTag;
25class UntracedMemberTag;
26class WeakMemberTag;
27
28// Not supposed to be specialized by the user.
29template <typename T>
30struct IsWeak : std::false_type {};
31
32// IsTraceMethodConst is used to verify that all Trace methods are marked as
33// const. It is equivalent to IsTraceable but for a non-const object.
34template <typename T, typename = void>
35struct IsTraceMethodConst : std::false_type {};
36
37template <typename T>
38struct IsTraceMethodConst<T, std::void_t<decltype(std::declval<const T>().Trace(
39 std::declval<Visitor*>()))>> : std::true_type {
40};
41
42template <typename T, typename = void>
43struct IsTraceable : std::false_type {
44 static_assert(sizeof(T), "T must be fully defined");
45};
46
47template <typename T>
49 T, std::void_t<decltype(std::declval<T>().Trace(std::declval<Visitor*>()))>>
50 : std::true_type {
51 // All Trace methods should be marked as const. If an object of type
52 // 'T' is traceable then any object of type 'const T' should also
53 // be traceable.
54 static_assert(IsTraceMethodConst<T>(),
55 "Trace methods should be marked as const.");
56};
57
58template <typename T>
60
61template <typename T, typename = void>
62struct HasGarbageCollectedMixinTypeMarker : std::false_type {
63 static_assert(sizeof(T), "T must be fully defined");
64};
65
66template <typename T>
68 T, std::void_t<
69 typename std::remove_const_t<T>::IsGarbageCollectedMixinTypeMarker>>
70 : std::true_type {
71 static_assert(sizeof(T), "T must be fully defined");
72};
73
74template <typename T, typename = void>
75struct HasGarbageCollectedTypeMarker : std::false_type {
76 static_assert(sizeof(T), "T must be fully defined");
77};
78
79template <typename T>
81 T,
82 std::void_t<typename std::remove_const_t<T>::IsGarbageCollectedTypeMarker>>
83 : std::true_type {
84 static_assert(sizeof(T), "T must be fully defined");
85};
86
87template <typename T, bool = HasGarbageCollectedTypeMarker<T>::value,
89struct IsGarbageCollectedMixinType : std::false_type {
90 static_assert(sizeof(T), "T must be fully defined");
91};
92
93template <typename T>
94struct IsGarbageCollectedMixinType<T, false, true> : std::true_type {
95 static_assert(sizeof(T), "T must be fully defined");
96};
97
98template <typename T, bool = HasGarbageCollectedTypeMarker<T>::value>
99struct IsGarbageCollectedType : std::false_type {
100 static_assert(sizeof(T), "T must be fully defined");
101};
102
103template <typename T>
104struct IsGarbageCollectedType<T, true> : std::true_type {
105 static_assert(sizeof(T), "T must be fully defined");
106};
107
108template <typename T>
110 : std::integral_constant<bool, IsGarbageCollectedType<T>::value ||
111 IsGarbageCollectedMixinType<T>::value> {
112 static_assert(sizeof(T), "T must be fully defined");
113};
114
115template <typename T, bool = (HasGarbageCollectedTypeMarker<T>::value &&
116 HasGarbageCollectedMixinTypeMarker<T>::value)>
117struct IsGarbageCollectedWithMixinType : std::false_type {
118 static_assert(sizeof(T), "T must be fully defined");
119};
120
121template <typename T>
122struct IsGarbageCollectedWithMixinType<T, true> : std::true_type {
123 static_assert(sizeof(T), "T must be fully defined");
124};
125
126template <typename BasicMemberCandidate, typename WeaknessTag,
127 typename WriteBarrierPolicy>
129 private:
130 template <typename T, typename CheckingPolicy, typename StorageType>
131 static std::true_type SubclassCheck(
132 const BasicMember<T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy,
133 StorageType>*);
134 static std::false_type SubclassCheck(...);
135
136 public:
137 static constexpr bool value = decltype(SubclassCheck(
138 std::declval<std::decay_t<BasicMemberCandidate>*>()))::value;
139};
140
141template <typename T,
143 T, StrongMemberTag, DijkstraWriteBarrierPolicy>::value>
144struct IsMemberType : std::false_type {};
145
146template <typename T>
147struct IsMemberType<T, true> : std::true_type {};
148
149template <typename T, bool = IsSubclassOfBasicMemberTemplate<
150 T, WeakMemberTag, DijkstraWriteBarrierPolicy>::value>
151struct IsWeakMemberType : std::false_type {};
152
153template <typename T>
154struct IsWeakMemberType<T, true> : std::true_type {};
155
156template <typename T, bool = IsSubclassOfBasicMemberTemplate<
157 T, UntracedMemberTag, NoWriteBarrierPolicy>::value>
158struct IsUntracedMemberType : std::false_type {};
159
160template <typename T>
161struct IsUntracedMemberType<T, true> : std::true_type {};
162
163template <typename T>
165 private:
166 template <typename U, size_t = sizeof(U)>
167 static std::true_type IsSizeOfKnown(U*);
168 static std::false_type IsSizeOfKnown(...);
169
170 public:
171 static constexpr bool value =
172 decltype(IsSizeOfKnown(std::declval<T*>()))::value;
173};
174
175template <typename T, typename U>
176constexpr bool IsDecayedSameV =
177 std::is_same_v<std::decay_t<T>, std::decay_t<U>>;
178
179template <typename B, typename D>
180constexpr bool IsStrictlyBaseOfV =
181 std::is_base_of_v<std::decay_t<B>, std::decay_t<D>> &&
182 !IsDecayedSameV<B, D>;
183
184template <typename T>
185constexpr bool IsAnyMemberTypeV = false;
186
187template <typename T, typename WeaknessTag, typename WriteBarrierPolicy,
188 typename CheckingPolicy, typename StorageType>
190 T, WeaknessTag, WriteBarrierPolicy, CheckingPolicy, StorageType>> = true;
191
192} // namespace internal
193
198template <typename T>
201
206template <typename T>
209
214template <typename T>
217
222template <typename T>
225
229template <typename T>
231
235template <typename T>
237
241template <typename T>
243
248template <typename T>
250
254template <typename T>
256
260template <typename T>
262 IsMemberTypeV<T> || IsWeakMemberTypeV<T>;
263
267template <typename T>
268constexpr bool IsAnyMemberTypeV = internal::IsAnyMemberTypeV<std::decay_t<T>>;
269
270} // namespace cppgc
271
272#endif // INCLUDE_CPPGC_TYPE_TRAITS_H_
Definition: member.h:77
constexpr bool IsStrictlyBaseOfV
Definition: type-traits.h:180
constexpr bool IsDecayedSameV
Definition: type-traits.h:176
constexpr bool IsAnyMemberTypeV
Definition: type-traits.h:185
constexpr bool IsTraceableV
Definition: type-traits.h:59
Definition: allocation.h:38
constexpr bool IsGarbageCollectedOrMixinTypeV
Definition: type-traits.h:215
constexpr bool IsMemberTypeV
Definition: type-traits.h:230
constexpr bool IsCompleteV
Definition: type-traits.h:255
constexpr bool IsWeakMemberTypeV
Definition: type-traits.h:242
constexpr bool IsGarbageCollectedTypeV
Definition: type-traits.h:207
constexpr bool IsWeakV
Definition: type-traits.h:249
constexpr bool IsMemberOrWeakMemberTypeV
Definition: type-traits.h:261
constexpr bool IsGarbageCollectedWithMixinTypeV
Definition: type-traits.h:223
constexpr bool IsUntracedMemberTypeV
Definition: type-traits.h:236
constexpr bool IsAnyMemberTypeV
Definition: type-traits.h:268
constexpr bool IsGarbageCollectedMixinTypeV
Definition: type-traits.h:199
Definition: pointer-policies.h:30
Definition: type-traits.h:164
static constexpr bool value
Definition: type-traits.h:171
Definition: type-traits.h:99
Definition: type-traits.h:144
static constexpr bool value
Definition: type-traits.h:137
Definition: type-traits.h:35
Definition: type-traits.h:43
Definition: type-traits.h:158
Definition: type-traits.h:151
Definition: type-traits.h:30
Definition: pointer-policies.h:108