-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathlist_value.h
More file actions
389 lines (293 loc) · 10.9 KB
/
Copy pathlist_value.h
File metadata and controls
389 lines (293 loc) · 10.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// 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_LIST_VALUE_H_
#define THIRD_PARTY_CEL_CPP_BASE_VALUES_LIST_VALUE_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include <utility>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/hash/hash.h"
#include "absl/log/absl_check.h"
#include "absl/status/statusor.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/list_type.h"
#include "base/value.h"
#include "internal/rtti.h"
namespace cel {
class ValueFactory;
class ListValueBuilderInterface;
template <typename T>
class ListValueBuilder;
// ListValue represents an instance of cel::ListType.
class ListValue : public Value {
public:
using BuilderInterface = ListValueBuilderInterface;
template <typename T>
using Builder = ListValueBuilder<T>;
static constexpr Kind kKind = ListType::kKind;
static bool Is(const Value& value) { return value.kind() == kKind; }
using Value::Is;
static const ListValue& Cast(const Value& value) {
ABSL_DCHECK(Is(value)) << "cannot cast " << value.type()->name()
<< " to list";
return static_cast<const ListValue&>(value);
}
// TODO(issues/5): implement iterators so we can have cheap concated lists
Handle<ListType> type() const;
constexpr Kind kind() const { return kKind; }
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_;
};
absl::StatusOr<Handle<Value>> Get(const GetContext& context,
size_t index) const;
struct Element final {
Element(size_t index, Handle<Value> value)
: index(index), value(std::move(value)) {}
size_t index;
Handle<Value> value;
};
class Iterator;
absl::StatusOr<UniqueRef<Iterator>> NewIterator(
MemoryManager& memory_manager) const ABSL_ATTRIBUTE_LIFETIME_BOUND;
bool Equals(const Value& other) const;
void HashValue(absl::HashState state) const;
private:
friend class base_internal::LegacyListValue;
friend class base_internal::AbstractListValue;
friend internal::TypeInfo base_internal::GetListValueTypeId(
const ListValue& list_value);
friend class base_internal::ValueHandle;
ListValue() = default;
// Called by CEL_IMPLEMENT_LIST_VALUE() and Is() to perform type checking.
internal::TypeInfo TypeId() const;
};
// Abstract class describes an iterator which can iterate over the elements in a
// list. A default implementation is provided by `ListValue::NewIterator`,
// however it is likely not as efficient as providing your own implementation.
class ListValue::Iterator {
public:
using Element = ListValue::Element;
virtual ~Iterator() = default;
ABSL_MUST_USE_RESULT virtual bool HasNext() = 0;
virtual absl::StatusOr<Element> Next(
const ListValue::GetContext& context) = 0;
virtual absl::StatusOr<size_t> NextIndex(
const ListValue::GetContext& context);
virtual absl::StatusOr<Handle<Value>> NextValue(
const ListValue::GetContext& context);
};
namespace base_internal {
ABSL_ATTRIBUTE_WEAK absl::StatusOr<Handle<Value>> LegacyListValueGet(
uintptr_t impl, ValueFactory& value_factory, size_t index);
ABSL_ATTRIBUTE_WEAK size_t LegacyListValueSize(uintptr_t impl);
ABSL_ATTRIBUTE_WEAK bool LegacyListValueEmpty(uintptr_t impl);
class LegacyListValue final : public ListValue, public InlineData {
public:
static bool Is(const Value& value) {
return value.kind() == kKind &&
static_cast<const ListValue&>(value).TypeId() ==
internal::TypeId<LegacyListValue>();
}
using ListValue::Is;
static const LegacyListValue& Cast(const Value& value) {
ABSL_ASSERT(Is(value));
return static_cast<const LegacyListValue&>(value);
}
Handle<ListType> type() const;
std::string DebugString() const;
size_t size() const;
bool empty() const;
absl::StatusOr<Handle<Value>> Get(const GetContext& context,
size_t index) const;
constexpr uintptr_t value() const { return impl_; }
absl::StatusOr<UniqueRef<Iterator>> NewIterator(
MemoryManager& memory_manager) const ABSL_ATTRIBUTE_LIFETIME_BOUND;
private:
friend class base_internal::ValueHandle;
friend class cel::ListValue;
template <size_t Size, size_t Align>
friend struct AnyData;
static constexpr uintptr_t kMetadata =
kStoredInline | kTrivial | (static_cast<uintptr_t>(kKind) << kKindShift);
explicit LegacyListValue(uintptr_t impl)
: ListValue(), InlineData(kMetadata), impl_(impl) {}
// Called by CEL_IMPLEMENT_STRUCT_VALUE() and Is() to perform type checking.
internal::TypeInfo TypeId() const {
return internal::TypeId<LegacyListValue>();
}
uintptr_t impl_;
};
class AbstractListValue : public ListValue,
public HeapData,
public EnableOwnerFromThis<AbstractListValue> {
public:
static bool Is(const Value& value) {
return value.kind() == kKind &&
static_cast<const ListValue&>(value).TypeId() !=
internal::TypeId<LegacyListValue>();
}
using ListValue::Is;
static const AbstractListValue& Cast(const Value& value) {
ABSL_ASSERT(Is(value));
return static_cast<const AbstractListValue&>(value);
}
const Handle<ListType>& 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<Handle<Value>> Get(const GetContext& context,
size_t index) const = 0;
virtual absl::StatusOr<UniqueRef<Iterator>> NewIterator(
MemoryManager& memory_manager) const ABSL_ATTRIBUTE_LIFETIME_BOUND;
protected:
explicit AbstractListValue(Handle<ListType> type);
private:
friend class cel::ListValue;
friend class base_internal::LegacyListValue;
friend class base_internal::AbstractListValue;
friend internal::TypeInfo base_internal::GetListValueTypeId(
const ListValue& list_value);
friend class base_internal::ValueHandle;
// Called by CEL_IMPLEMENT_LIST_VALUE() and Is() to perform type checking.
virtual internal::TypeInfo TypeId() const = 0;
const Handle<ListType> type_;
};
inline internal::TypeInfo GetListValueTypeId(const ListValue& list_value) {
return list_value.TypeId();
}
class DynamicListValue final : public AbstractListValue {
public:
DynamicListValue(Handle<ListType> type,
std::vector<Handle<Value>, Allocator<Handle<Value>>> storage)
: AbstractListValue(std::move(type)), storage_(std::move(storage)) {}
std::string DebugString() const override {
size_t count = size();
std::string out;
out.push_back('[');
if (count != 0) {
out.append(storage_[0]->DebugString());
for (size_t index = 1; index < count; index++) {
out.append(", ");
out.append(storage_[index]->DebugString());
}
}
out.push_back(']');
return out;
}
size_t size() const override { return storage_.size(); }
bool empty() const override { return storage_.empty(); }
absl::StatusOr<Handle<Value>> Get(const GetContext& context,
size_t index) const override {
static_cast<void>(context);
return storage_[index];
}
internal::TypeInfo TypeId() const override {
return internal::TypeId<DynamicListValue>();
}
private:
std::vector<Handle<Value>, Allocator<Handle<Value>>> storage_;
};
template <typename T>
class StaticListValue final : public AbstractListValue {
public:
using value_traits = ValueTraits<T>;
using underlying_type = typename value_traits::underlying_type;
StaticListValue(
Handle<ListType> type,
std::vector<underlying_type, Allocator<underlying_type>> storage)
: AbstractListValue(std::move(type)), storage_(std::move(storage)) {}
std::string DebugString() const override {
size_t count = size();
std::string out;
out.push_back('[');
if (count != 0) {
out.append(value_traits::DebugString(storage_[0]));
for (size_t index = 1; index < count; index++) {
out.append(", ");
out.append(value_traits::DebugString(storage_[index]));
}
}
out.push_back(']');
return out;
}
size_t size() const override { return storage_.size(); }
bool empty() const override { return storage_.empty(); }
absl::StatusOr<Handle<Value>> Get(const GetContext& context,
size_t index) const override {
return value_traits::Wrap(context.value_factory(), storage_[index]);
}
internal::TypeInfo TypeId() const override {
return internal::TypeId<StaticListValue<T>>();
}
private:
std::vector<underlying_type, Allocator<underlying_type>> storage_;
};
} // namespace base_internal
#define CEL_LIST_VALUE_CLASS ::cel::base_internal::AbstractListValue
CEL_INTERNAL_VALUE_DECL(ListValue);
// CEL_DECLARE_LIST_VALUE declares `list_value` as an list value. It must
// be part of the class definition of `list_value`.
//
// class MyListValue : public CEL_LIST_VALUE_CLASS {
// ...
// private:
// CEL_DECLARE_LIST_VALUE(MyListValue);
// };
#define CEL_DECLARE_LIST_VALUE(list_value) \
CEL_INTERNAL_DECLARE_VALUE(List, list_value)
// CEL_IMPLEMENT_LIST_VALUE implements `list_value` as an list
// value. It must be called after the class definition of `list_value`.
//
// class MyListValue : public CEL_LIST_VALUE_CLASS {
// ...
// private:
// CEL_DECLARE_LIST_VALUE(MyListValue);
// };
//
// CEL_IMPLEMENT_LIST_VALUE(MyListValue);
#define CEL_IMPLEMENT_LIST_VALUE(list_value) \
CEL_INTERNAL_IMPLEMENT_VALUE(List, list_value)
namespace base_internal {
template <>
struct ValueTraits<ListValue> {
using type = ListValue;
using type_type = ListType;
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_LIST_VALUE_H_