-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathenum_type.h
More file actions
250 lines (193 loc) · 7.24 KB
/
Copy pathenum_type.h
File metadata and controls
250 lines (193 loc) · 7.24 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
// Copyright 2022 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_ENUM_TYPE_H_
#define THIRD_PARTY_CEL_CPP_BASE_TYPES_ENUM_TYPE_H_
#include <cstdint>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/log/absl_check.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "absl/types/variant.h"
#include "base/internal/data.h"
#include "base/kind.h"
#include "base/memory.h"
#include "base/type.h"
#include "internal/rtti.h"
namespace cel {
class MemoryManager;
class EnumValue;
class TypedEnumValueFactory;
class TypeManager;
// EnumType represents an enumeration type. An enumeration is a set of constants
// that can be looked up by name and/or number.
class EnumType : public Type, public base_internal::HeapData {
public:
struct Constant;
class ConstantId final {
public:
ConstantId() = delete;
ConstantId(const ConstantId&) = default;
ConstantId(ConstantId&&) = default;
ConstantId& operator=(const ConstantId&) = default;
ConstantId& operator=(ConstantId&&) = default;
std::string DebugString() const;
friend bool operator==(const ConstantId& lhs, const ConstantId& rhs) {
return lhs.data_ == rhs.data_;
}
friend bool operator<(const ConstantId& lhs, const ConstantId& rhs);
template <typename H>
friend H AbslHashValue(H state, const ConstantId& id) {
return H::combine(std::move(state), id.data_);
}
template <typename S>
friend void AbslStringify(S& sink, const ConstantId& id) {
sink.Append(id.DebugString());
}
private:
friend class EnumType;
friend class EnumValue;
explicit ConstantId(absl::string_view name)
: data_(absl::in_place_type<absl::string_view>, name) {}
explicit ConstantId(int64_t number)
: data_(absl::in_place_type<int64_t>, number) {}
absl::variant<absl::string_view, int64_t> data_;
};
static constexpr Kind kKind = Kind::kEnum;
using Type::Is;
static bool Is(const Type& type) { return type.kind() == kKind; }
static const EnumType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to enum";
return static_cast<const EnumType&>(type);
}
Kind kind() const { return kKind; }
virtual absl::string_view name() const ABSL_ATTRIBUTE_LIFETIME_BOUND = 0;
std::string DebugString() const { return std::string(name()); }
virtual size_t constant_count() const = 0;
// Find the constant definition for the given identifier. If the constant does
// not exist, an OK status and empty optional is returned. If the constant
// exists, an OK status and the constant is returned. Otherwise an error is
// returned.
absl::StatusOr<absl::optional<Constant>> FindConstant(ConstantId id) const
ABSL_ATTRIBUTE_LIFETIME_BOUND;
// Called by FindConstant.
virtual absl::StatusOr<absl::optional<Constant>> FindConstantByName(
absl::string_view name) const ABSL_ATTRIBUTE_LIFETIME_BOUND = 0;
// Called by FindConstant.
virtual absl::StatusOr<absl::optional<Constant>> FindConstantByNumber(
int64_t number) const ABSL_ATTRIBUTE_LIFETIME_BOUND = 0;
class ConstantIterator;
// Returns an iterator which can iterate over all the constants defined by
// this enumeration. The order with which iteration occurs is undefined.
virtual absl::StatusOr<UniqueRef<ConstantIterator>> NewConstantIterator(
MemoryManager& memory_manager) const ABSL_ATTRIBUTE_LIFETIME_BOUND = 0;
protected:
static ConstantId MakeConstantId(absl::string_view name) {
return ConstantId(name);
}
static ConstantId MakeConstantId(int64_t number) {
return ConstantId(number);
}
template <typename E>
static std::enable_if_t<
std::conjunction_v<
std::is_enum<E>,
std::is_convertible<std::underlying_type_t<E>, int64_t>>,
ConstantId>
MakeConstantId(E e) {
return MakeConstantId(
static_cast<int64_t>(static_cast<std::underlying_type_t<E>>(e)));
}
EnumType();
private:
friend internal::TypeInfo base_internal::GetEnumTypeTypeId(
const EnumType& enum_type);
struct NewInstanceVisitor;
struct FindConstantVisitor;
friend struct NewInstanceVisitor;
friend struct FindConstantVisitor;
friend class MemoryManager;
friend class EnumValue;
friend class TypeFactory;
friend class base_internal::TypeHandle;
EnumType(const EnumType&) = delete;
EnumType(EnumType&&) = delete;
// Called by CEL_IMPLEMENT_ENUM_TYPE() and Is() to perform type checking.
virtual internal::TypeInfo TypeId() const = 0;
};
// Constant describes a single value in an enumeration. All fields are valid so
// long as EnumType is valid.
struct EnumType::Constant final {
Constant(ConstantId id, absl::string_view name, int64_t number,
const void* hint = nullptr)
: id(id), name(name), number(number), hint(hint) {}
// Identifier which allows the most efficient form of lookup, compared to
// looking up by name or number.
ConstantId id;
// The unqualified enumeration value name.
absl::string_view name;
// The enumeration value number.
int64_t number;
// Some implementation-specific data that can be laundered to the value
// implementation for this type to perform optimizations.
const void* hint = nullptr;
};
class EnumType::ConstantIterator {
public:
using Constant = EnumType::Constant;
virtual ~ConstantIterator() = default;
ABSL_MUST_USE_RESULT virtual bool HasNext() = 0;
virtual absl::StatusOr<Constant> Next() = 0;
virtual absl::StatusOr<absl::string_view> NextName();
virtual absl::StatusOr<int64_t> NextNumber();
};
// CEL_DECLARE_ENUM_TYPE declares `enum_type` as an enumeration type. It must be
// part of the class definition of `enum_type`.
//
// class MyEnumType : public cel::EnumType {
// ...
// private:
// CEL_DECLARE_ENUM_TYPE(MyEnumType);
// };
#define CEL_DECLARE_ENUM_TYPE(enum_type) \
CEL_INTERNAL_DECLARE_TYPE(Enum, enum_type)
// CEL_IMPLEMENT_ENUM_TYPE implements `enum_type` as an enumeration type. It
// must be called after the class definition of `enum_type`.
//
// class MyEnumType : public cel::EnumType {
// ...
// private:
// CEL_DECLARE_ENUM_TYPE(MyEnumType);
// };
//
// CEL_IMPLEMENT_ENUM_TYPE(MyEnumType);
#define CEL_IMPLEMENT_ENUM_TYPE(enum_type) \
CEL_INTERNAL_IMPLEMENT_TYPE(Enum, enum_type)
CEL_INTERNAL_TYPE_DECL(EnumType);
namespace base_internal {
inline internal::TypeInfo GetEnumTypeTypeId(const EnumType& enum_type) {
return enum_type.TypeId();
}
} // namespace base_internal
namespace base_internal {
template <>
struct TypeTraits<EnumType> {
using value_type = EnumValue;
};
} // namespace base_internal
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_BASE_TYPES_ENUM_TYPE_H_