-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathwrapper_type.h
More file actions
345 lines (259 loc) · 10.7 KB
/
Copy pathwrapper_type.h
File metadata and controls
345 lines (259 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_CEL_CPP_BASE_TYPES_WRAPPER_TYPE_H_
#define THIRD_PARTY_CEL_CPP_BASE_TYPES_WRAPPER_TYPE_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include "absl/base/attributes.h"
#include "absl/log/absl_check.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/internal/data.h"
#include "base/kind.h"
#include "base/type.h"
#include "base/types/bool_type.h"
#include "base/types/bytes_type.h"
#include "base/types/double_type.h"
#include "base/types/int_type.h"
#include "base/types/string_type.h"
#include "base/types/uint_type.h"
namespace cel {
class TypeFactory;
class BoolWrapperType;
class BytesWrapperType;
class DoubleWrapperType;
class IntWrapperType;
class StringWrapperType;
class UintWrapperType;
// WrapperType is a special type that is effectively a union of NullType with
// one of BoolType, BytesType, DoubleType, IntType, StringType, or UintType.
class WrapperType : public Type, base_internal::InlineData {
private:
using Base = base_internal::InlineData;
public:
static constexpr Kind kKind = Kind::kWrapper;
static bool Is(const Type& type) { return type.kind() == Kind::kWrapper; }
using Type::Is;
static const WrapperType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to wrapper";
return static_cast<const WrapperType&>(type);
}
constexpr Kind kind() const { return kKind; }
absl::string_view name() const;
std::string DebugString() const { return std::string(name()); }
const Handle<Type>& wrapped() const;
private:
friend class Type;
friend class BoolWrapperType;
friend class BytesWrapperType;
friend class DoubleWrapperType;
friend class IntWrapperType;
friend class StringWrapperType;
friend class UintWrapperType;
// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;
using Base::Base;
};
inline const Handle<Type>& UnwrapType(const Handle<Type>& handle) {
return handle->Is<WrapperType>() ? handle.As<WrapperType>()->wrapped()
: handle;
}
inline Handle<Type> UnwrapType(Handle<Type>&& handle) {
return handle->Is<WrapperType>() ? handle.As<WrapperType>()->wrapped()
: handle;
}
inline const Type& UnwrapType(const Type& type) {
return WrapperType::Is(type) ? *WrapperType::Cast(type).wrapped() : type;
}
class BoolWrapperType final : public WrapperType {
public:
static constexpr absl::string_view kName = "google.protobuf.BoolValue";
static bool Is(const Type& type) {
return WrapperType::Is(type) &&
static_cast<const WrapperType&>(type).wrapped()->kind() ==
Kind::kBool;
}
using WrapperType::Is;
static const BoolWrapperType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to " << kName;
return static_cast<const BoolWrapperType&>(type);
}
constexpr absl::string_view name() const { return kName; }
const Handle<BoolType>& wrapped() const { return BoolType::Get(); }
private:
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
ABSL_ATTRIBUTE_PURE_FUNCTION static const Handle<BoolWrapperType>& Get();
static constexpr uintptr_t kMetadata =
base_internal::kStoredInline | base_internal::kTrivial |
(static_cast<uintptr_t>(kKind) << base_internal::kKindShift) |
(static_cast<uintptr_t>(BoolType::kKind)
<< base_internal::kInlineVariantShift);
constexpr BoolWrapperType() : WrapperType(kMetadata) {}
};
class BytesWrapperType final : public WrapperType {
public:
static constexpr absl::string_view kName = "google.protobuf.BytesValue";
static bool Is(const Type& type) {
return WrapperType::Is(type) &&
static_cast<const WrapperType&>(type).wrapped()->kind() ==
Kind::kBytes;
}
using WrapperType::Is;
static const BytesWrapperType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to " << kName;
return static_cast<const BytesWrapperType&>(type);
}
constexpr absl::string_view name() const { return kName; }
const Handle<BytesType>& wrapped() const { return BytesType::Get(); }
private:
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
ABSL_ATTRIBUTE_PURE_FUNCTION static const Handle<BytesWrapperType>& Get();
static constexpr uintptr_t kMetadata =
base_internal::kStoredInline | base_internal::kTrivial |
(static_cast<uintptr_t>(kKind) << base_internal::kKindShift) |
(static_cast<uintptr_t>(BytesType::kKind)
<< base_internal::kInlineVariantShift);
constexpr BytesWrapperType() : WrapperType(kMetadata) {}
};
class DoubleWrapperType final : public WrapperType {
public:
static constexpr absl::string_view kName = "google.protobuf.DoubleValue";
static bool Is(const Type& type) {
return WrapperType::Is(type) &&
static_cast<const WrapperType&>(type).wrapped()->kind() ==
Kind::kDouble;
}
using WrapperType::Is;
static const DoubleWrapperType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to " << kName;
return static_cast<const DoubleWrapperType&>(type);
}
constexpr absl::string_view name() const { return kName; }
const Handle<DoubleType>& wrapped() const { return DoubleType::Get(); }
private:
friend class WrapperType;
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
ABSL_ATTRIBUTE_PURE_FUNCTION static const Handle<DoubleWrapperType>& Get();
static constexpr uintptr_t kMetadata =
base_internal::kStoredInline | base_internal::kTrivial |
(static_cast<uintptr_t>(kKind) << base_internal::kKindShift) |
(static_cast<uintptr_t>(DoubleType::kKind)
<< base_internal::kInlineVariantShift);
constexpr DoubleWrapperType() : WrapperType(kMetadata) {}
// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;
};
class IntWrapperType final : public WrapperType {
public:
static constexpr absl::string_view kName = "google.protobuf.Int64Value";
static bool Is(const Type& type) {
return WrapperType::Is(type) &&
static_cast<const WrapperType&>(type).wrapped()->kind() ==
Kind::kInt;
}
using WrapperType::Is;
static const IntWrapperType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to " << kName;
return static_cast<const IntWrapperType&>(type);
}
constexpr absl::string_view name() const { return kName; }
const Handle<IntType>& wrapped() const { return IntType::Get(); }
private:
friend class WrapperType;
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
ABSL_ATTRIBUTE_PURE_FUNCTION static const Handle<IntWrapperType>& Get();
static constexpr uintptr_t kMetadata =
base_internal::kStoredInline | base_internal::kTrivial |
(static_cast<uintptr_t>(kKind) << base_internal::kKindShift) |
(static_cast<uintptr_t>(IntType::kKind)
<< base_internal::kInlineVariantShift);
constexpr IntWrapperType() : WrapperType(kMetadata) {}
// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;
};
class StringWrapperType final : public WrapperType {
public:
static constexpr absl::string_view kName = "google.protobuf.StringValue";
static bool Is(const Type& type) {
return WrapperType::Is(type) &&
static_cast<const WrapperType&>(type).wrapped()->kind() ==
Kind::kString;
}
using WrapperType::Is;
static const StringWrapperType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to " << kName;
return static_cast<const StringWrapperType&>(type);
}
constexpr absl::string_view name() const { return kName; }
const Handle<StringType>& wrapped() const { return StringType::Get(); }
private:
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
ABSL_ATTRIBUTE_PURE_FUNCTION static const Handle<StringWrapperType>& Get();
static constexpr uintptr_t kMetadata =
base_internal::kStoredInline | base_internal::kTrivial |
(static_cast<uintptr_t>(kKind) << base_internal::kKindShift) |
(static_cast<uintptr_t>(StringType::kKind)
<< base_internal::kInlineVariantShift);
constexpr StringWrapperType() : WrapperType(kMetadata) {}
};
class UintWrapperType final : public WrapperType {
public:
static constexpr absl::string_view kName = "google.protobuf.UInt64Value";
static bool Is(const Type& type) {
return WrapperType::Is(type) &&
static_cast<const WrapperType&>(type).wrapped()->kind() ==
Kind::kUint;
}
using WrapperType::Is;
static const UintWrapperType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to " << kName;
return static_cast<const UintWrapperType&>(type);
}
constexpr absl::string_view name() const { return kName; }
const Handle<UintType>& wrapped() const { return UintType::Get(); }
private:
friend class WrapperType;
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
ABSL_ATTRIBUTE_PURE_FUNCTION static const Handle<UintWrapperType>& Get();
static constexpr uintptr_t kMetadata =
base_internal::kStoredInline | base_internal::kTrivial |
(static_cast<uintptr_t>(kKind) << base_internal::kKindShift) |
(static_cast<uintptr_t>(UintType::kKind)
<< base_internal::kInlineVariantShift);
constexpr UintWrapperType() : WrapperType(kMetadata) {}
// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;
};
extern template class Handle<WrapperType>;
extern template class Handle<BoolWrapperType>;
extern template class Handle<BytesWrapperType>;
extern template class Handle<DoubleWrapperType>;
extern template class Handle<IntWrapperType>;
extern template class Handle<StringWrapperType>;
extern template class Handle<UintWrapperType>;
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_BASE_TYPES_WRAPPER_TYPE_H_