-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathmap_value.h
More file actions
335 lines (248 loc) · 9.5 KB
/
Copy pathmap_value.h
File metadata and controls
335 lines (248 loc) · 9.5 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
// 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_VALUES_MAP_VALUE_H_
#define THIRD_PARTY_CEL_CPP_BASE_VALUES_MAP_VALUE_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/log/absl_check.h"
#include "absl/status/statusor.h"
#include "absl/types/optional.h"
#include "base/handle.h"
#include "base/internal/data.h"
#include "base/kind.h"
#include "base/memory.h"
#include "base/owner.h"
#include "base/type.h"
#include "base/types/map_type.h"
#include "base/value.h"
#include "base/values/list_value.h"
#include "internal/rtti.h"
namespace cel {
class ListValue;
class ValueFactory;
class MemoryManager;
class MapValueBuilderInterface;
template <typename K, typename V>
class MapValueBuilder;
// MapValue represents an instance of cel::MapType.
class MapValue : public Value {
public:
using BuilderInterface = MapValueBuilderInterface;
template <typename K, typename V>
using Builder = MapValueBuilder<K, V>;
static constexpr Kind kKind = MapType::kKind;
static bool Is(const Value& value) { return value.kind() == kKind; }
using Value::Is;
static const MapValue& Cast(const Value& value) {
ABSL_DCHECK(Is(value)) << "cannot cast " << value.type()->name()
<< " to map";
return static_cast<const MapValue&>(value);
}
constexpr Kind kind() const { return kKind; }
Handle<MapType> type() const;
std::string DebugString() const;
size_t size() const;
bool empty() const;
class GetContext final {
public:
explicit GetContext(ValueFactory& value_factory)
: value_factory_(value_factory) {}
ValueFactory& value_factory() const { return value_factory_; }
private:
ValueFactory& value_factory_;
};
// Retrieves the value corresponding to the given key. If the key does not
// exist, an empty optional is returned. If the given key type is not
// compatible with the expected key type, an error is returned.
absl::StatusOr<absl::optional<Handle<Value>>> Get(
const GetContext& context, const Handle<Value>& key) const;
class HasContext final {};
absl::StatusOr<bool> Has(const HasContext& context,
const Handle<Value>& key) const;
class ListKeysContext final {
public:
explicit ListKeysContext(ValueFactory& value_factory)
: value_factory_(value_factory) {}
ValueFactory& value_factory() const { return value_factory_; }
private:
ValueFactory& value_factory_;
};
absl::StatusOr<Handle<ListValue>> ListKeys(
const ListKeysContext& context) const;
struct Entry final {
Entry(Handle<Value> key, Handle<Value> value)
: key(std::move(key)), value(std::move(value)) {}
Handle<Value> key;
Handle<Value> value;
};
class Iterator;
absl::StatusOr<UniqueRef<Iterator>> NewIterator(
MemoryManager& memory_manager) const ABSL_ATTRIBUTE_LIFETIME_BOUND;
private:
friend internal::TypeInfo base_internal::GetMapValueTypeId(
const MapValue& map_value);
friend class base_internal::ValueHandle;
friend class base_internal::LegacyMapValue;
friend class base_internal::AbstractMapValue;
MapValue() = default;
// Called by CEL_IMPLEMENT_MAP_VALUE() and Is() to perform type checking.
internal::TypeInfo TypeId() const;
};
// Abstract class describes an iterator which can iterate over the entries in a
// map. A default implementation is provided by `MapValue::NewIterator`, however
// it is likely not as efficient as providing your own implementation.
class MapValue::Iterator {
public:
using Entry = MapValue::Entry;
virtual ~Iterator() = default;
ABSL_MUST_USE_RESULT virtual bool HasNext() = 0;
virtual absl::StatusOr<Entry> Next(const MapValue::GetContext& context) = 0;
virtual absl::StatusOr<Handle<Value>> NextKey(
const MapValue::GetContext& context);
virtual absl::StatusOr<Handle<Value>> NextValue(
const MapValue::GetContext& context);
};
CEL_INTERNAL_VALUE_DECL(MapValue);
namespace base_internal {
ABSL_ATTRIBUTE_WEAK size_t LegacyMapValueSize(uintptr_t impl);
ABSL_ATTRIBUTE_WEAK bool LegacyMapValueEmpty(uintptr_t impl);
ABSL_ATTRIBUTE_WEAK absl::StatusOr<absl::optional<Handle<Value>>>
LegacyMapValueGet(uintptr_t impl, ValueFactory& value_factory,
const Handle<Value>& key);
ABSL_ATTRIBUTE_WEAK absl::StatusOr<bool> LegacyMapValueHas(
uintptr_t impl, const Handle<Value>& key);
ABSL_ATTRIBUTE_WEAK absl::StatusOr<Handle<ListValue>> LegacyMapValueListKeys(
uintptr_t impl, ValueFactory& value_factory);
class LegacyMapValue final : public MapValue, public InlineData {
public:
static bool Is(const Value& value) {
return value.kind() == kKind &&
static_cast<const MapValue&>(value).TypeId() ==
internal::TypeId<LegacyMapValue>();
}
using MapValue::Is;
static const LegacyMapValue& Cast(const Value& value) {
ABSL_ASSERT(Is(value));
return static_cast<const LegacyMapValue&>(value);
}
Handle<MapType> type() const;
std::string DebugString() const;
size_t size() const;
bool empty() const;
absl::StatusOr<absl::optional<Handle<Value>>> Get(
const GetContext& context, const Handle<Value>& key) const;
absl::StatusOr<bool> Has(const HasContext& context,
const Handle<Value>& key) const;
absl::StatusOr<Handle<ListValue>> ListKeys(
const ListKeysContext& context) const;
absl::StatusOr<UniqueRef<Iterator>> NewIterator(
MemoryManager& memory_manager) const ABSL_ATTRIBUTE_LIFETIME_BOUND;
constexpr uintptr_t value() const { return impl_; }
private:
friend class base_internal::ValueHandle;
friend class cel::MapValue;
template <size_t Size, size_t Align>
friend struct AnyData;
static constexpr uintptr_t kMetadata =
kStoredInline | kTrivial | (static_cast<uintptr_t>(kKind) << kKindShift);
explicit LegacyMapValue(uintptr_t impl)
: MapValue(), InlineData(kMetadata), impl_(impl) {}
internal::TypeInfo TypeId() const {
return internal::TypeId<LegacyMapValue>();
}
uintptr_t impl_;
};
class AbstractMapValue : public MapValue,
public HeapData,
public EnableOwnerFromThis<AbstractMapValue> {
public:
static bool Is(const Value& value) {
return value.kind() == kKind &&
static_cast<const MapValue&>(value).TypeId() !=
internal::TypeId<LegacyMapValue>();
}
using MapValue::Is;
static const AbstractMapValue& Cast(const Value& value) {
ABSL_ASSERT(Is(value));
return static_cast<const AbstractMapValue&>(value);
}
const Handle<MapType>& type() const { return type_; }
virtual std::string DebugString() const = 0;
virtual size_t size() const = 0;
virtual bool empty() const { return size() == 0; }
virtual absl::StatusOr<absl::optional<Handle<Value>>> Get(
const GetContext& context, const Handle<Value>& key) const = 0;
virtual absl::StatusOr<bool> Has(const HasContext& context,
const Handle<Value>& key) const = 0;
virtual absl::StatusOr<Handle<ListValue>> ListKeys(
const ListKeysContext& context) const = 0;
virtual absl::StatusOr<UniqueRef<Iterator>> NewIterator(
MemoryManager& memory_manager) const ABSL_ATTRIBUTE_LIFETIME_BOUND;
protected:
explicit AbstractMapValue(Handle<MapType> type);
private:
friend class cel::MapValue;
friend class base_internal::ValueHandle;
// Called by CEL_IMPLEMENT_MAP_VALUE() and Is() to perform type checking.
virtual internal::TypeInfo TypeId() const = 0;
const Handle<MapType> type_;
};
inline internal::TypeInfo GetMapValueTypeId(const MapValue& map_value) {
return map_value.TypeId();
}
} // namespace base_internal
#define CEL_MAP_VALUE_CLASS ::cel::base_internal::AbstractMapValue
// CEL_DECLARE_MAP_VALUE declares `map_value` as an map value. It must
// be part of the class definition of `map_value`.
//
// class MyMapValue : public CEL_MAP_VALUE_CLASS {
// ...
// private:
// CEL_DECLARE_MAP_VALUE(MyMapValue);
// };
#define CEL_DECLARE_MAP_VALUE(map_value) \
CEL_INTERNAL_DECLARE_VALUE(Map, map_value)
// CEL_IMPLEMENT_MAP_VALUE implements `map_value` as an map
// value. It must be called after the class definition of `map_value`.
//
// class MyMapValue : public CEL_MAP_VALUE_CLASS {
// ...
// private:
// CEL_DECLARE_MAP_VALUE(MyMapValue);
// };
//
// CEL_IMPLEMENT_MAP_VALUE(MyMapValue);
#define CEL_IMPLEMENT_MAP_VALUE(map_value) \
CEL_INTERNAL_IMPLEMENT_VALUE(Map, map_value)
namespace base_internal {
template <>
struct ValueTraits<MapValue> {
using type = MapValue;
using type_type = MapType;
using underlying_type = void;
static std::string DebugString(const type& value) {
return value.DebugString();
}
static Handle<type> Wrap(ValueFactory& value_factory, Handle<type> value) {
static_cast<void>(value_factory);
return value;
}
static Handle<type> Unwrap(Handle<type> value) { return value; }
};
} // namespace base_internal
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_BASE_VALUES_MAP_VALUE_H_