-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathlist_value_builder.h
More file actions
316 lines (257 loc) · 10.4 KB
/
Copy pathlist_value_builder.h
File metadata and controls
316 lines (257 loc) · 10.4 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
// 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_VALUES_LIST_VALUE_BUILDER_H_
#define THIRD_PARTY_CEL_CPP_BASE_VALUES_LIST_VALUE_BUILDER_H_
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/variant.h"
#include "absl/utility/utility.h"
#include "base/memory.h"
#include "base/value_factory.h"
#include "base/values/list_value.h"
#include "internal/overloaded.h"
namespace cel {
// Abstract interface for building ListValue.
//
// ListValueBuilderInterface is not reusable, once Build() is called the state
// of ListValueBuilderInterface is undefined.
class ListValueBuilderInterface {
public:
virtual ~ListValueBuilderInterface() = default;
virtual std::string DebugString() const = 0;
virtual absl::Status Add(Handle<Value> value) = 0;
virtual size_t size() const = 0;
virtual bool empty() const { return size() == 0; }
virtual void reserve(size_t size) = 0;
virtual absl::StatusOr<Handle<ListValue>> Build() && = 0;
protected:
explicit ListValueBuilderInterface(
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory)
: value_factory_(value_factory) {}
ValueFactory& value_factory() const { return value_factory_; }
private:
ValueFactory& value_factory_;
};
// ListValueBuilder implements ListValueBuilderInterface, but is specialized for
// some types which have underlying C++ representations. When T is Value,
// ListValueBuilder has exactly the same methods as ListValueBuilderInterface.
// When T is not Value itself, each function that accepts Handle<Value> above
// also accepts Handle<T> variants. When T has some underlying C++
// representation, each function that accepts Handle<Value> above also accepts
// the underlying C++ representation.
//
// For example, ListValueBuilder<IntValue>::Add accepts Handle<Value>,
// Handle<IntValue> and int64_t.
template <typename T>
class ListValueBuilder;
namespace base_internal {
// ComposableListType is a variant which represents either the ListType or the
// element Type for creating a ListType.
template <typename T>
using ComposableListType = absl::variant<Handle<T>, Handle<ListType>>;
// Create a ListType from ComposableListType.
template <typename T>
absl::StatusOr<Handle<ListType>> ComposeListType(
ValueFactory& value_factory, ComposableListType<T>&& composable) {
return absl::visit(
internal::Overloaded{
[&value_factory](
Handle<T>&& element) -> absl::StatusOr<Handle<ListType>> {
return value_factory.type_factory().CreateListType(
std::move(element));
},
[](Handle<ListType>&& list) -> absl::StatusOr<Handle<ListType>> {
return std::move(list);
},
},
std::move(composable));
}
template <typename List, typename DebugStringer>
std::string ComposeListValueDebugString(const List& list,
const DebugStringer& debug_stringer) {
std::string out;
out.push_back('[');
auto current = list.begin();
if (current != list.end()) {
out.append(debug_stringer(*current));
++current;
for (; current != list.end(); ++current) {
out.append(", ");
out.append(debug_stringer(*current));
}
}
out.push_back(']');
return out;
}
struct ComposedListType {
explicit ComposedListType() = default;
};
inline constexpr ComposedListType kComposedListType{};
// Implementation of ListValueBuilder. Specialized to store some value types as
// C++ primitives, avoiding Handle overhead. Anything that does not have a C++
// primitive is stored as Handle<Value>.
template <typename T, typename U = void>
class ListValueBuilderImpl;
// Specialization for when the element type is not Value itself and has no C++
// primitive types.
template <typename T>
class ListValueBuilderImpl<T, void> : public ListValueBuilderInterface {
public:
static_assert(std::is_base_of_v<Value, T>);
ListValueBuilderImpl(
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<typename ValueTraits<T>::type_type> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<typename ValueTraits<T>::type_type>>,
std::move(type)),
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}
ListValueBuilderImpl(
ComposedListType,
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<ListType> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}
std::string DebugString() const override {
return ComposeListValueDebugString(
storage_,
[](const Handle<Value>& value) { return value->DebugString(); });
}
absl::Status Add(Handle<Value> value) override {
return Add(std::move(value).As<T>());
}
absl::Status Add(Handle<T> value) {
storage_.push_back(std::move(value));
return absl::OkStatus();
}
size_t size() const override { return storage_.size(); }
bool empty() const override { return storage_.empty(); }
void reserve(size_t size) override { storage_.reserve(size); }
absl::StatusOr<Handle<ListValue>> Build() && override {
CEL_ASSIGN_OR_RETURN(auto type,
ComposeListType(value_factory(), std::move(type_)));
return value_factory()
.template CreateListValue<base_internal::DynamicListValue>(
std::move(type), std::move(storage_));
}
private:
ComposableListType<typename ValueTraits<T>::type_type> type_;
std::vector<Handle<Value>, Allocator<Handle<Value>>> storage_;
};
// Specialization for when the element type is Value itself and has no C++
// primitive types.
template <>
class ListValueBuilderImpl<Value, void> : public ListValueBuilderInterface {
public:
ListValueBuilderImpl(
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<Type> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<Type>>, std::move(type)),
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}
ListValueBuilderImpl(
ComposedListType,
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<ListType> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}
std::string DebugString() const override {
return ComposeListValueDebugString(
storage_,
[](const Handle<Value>& value) { return value->DebugString(); });
}
absl::Status Add(Handle<Value> value) override {
storage_.push_back(std::move(value));
return absl::OkStatus();
}
size_t size() const override { return storage_.size(); }
bool empty() const override { return storage_.empty(); }
void reserve(size_t size) override { storage_.reserve(size); }
absl::StatusOr<Handle<ListValue>> Build() && override {
CEL_ASSIGN_OR_RETURN(auto type,
ComposeListType(value_factory(), std::move(type_)));
return value_factory()
.template CreateListValue<base_internal::DynamicListValue>(
std::move(type), std::move(storage_));
}
private:
ComposableListType<Type> type_;
std::vector<Handle<Value>, Allocator<Handle<Value>>> storage_;
};
// Specialization used when the element type has some C++ primitive
// representation.
template <typename T, typename U>
class ListValueBuilderImpl : public ListValueBuilderInterface {
public:
ListValueBuilderImpl(
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<typename ValueTraits<T>::type_type> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<typename ValueTraits<T>::type_type>>,
std::move(type)),
storage_(Allocator<U>{value_factory.memory_manager()}) {}
ListValueBuilderImpl(
ComposedListType,
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<ListType> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
storage_(Allocator<U>{value_factory.memory_manager()}) {}
std::string DebugString() const override {
return ComposeListValueDebugString(storage_, [](const U& value) {
return ValueTraits<T>::DebugString(value);
});
}
absl::Status Add(Handle<Value> value) override {
return Add(std::move(value).As<T>());
}
absl::Status Add(const Handle<T>& value) { return Add(value->value()); }
absl::Status Add(U value) {
storage_.push_back(std::move(value));
return absl::OkStatus();
}
size_t size() const override { return storage_.size(); }
bool empty() const override { return storage_.empty(); }
void reserve(size_t size) override { storage_.reserve(size); }
absl::StatusOr<Handle<ListValue>> Build() && override {
CEL_ASSIGN_OR_RETURN(auto type,
ComposeListType(value_factory(), std::move(type_)));
return value_factory()
.template CreateListValue<base_internal::StaticListValue<T>>(
std::move(type), std::move(storage_));
}
private:
ComposableListType<typename ValueTraits<T>::type_type> type_;
std::vector<U, Allocator<U>> storage_;
};
} // namespace base_internal
template <typename T>
class ListValueBuilder final
: public base_internal::ListValueBuilderImpl<
T, typename base_internal::ValueTraits<T>::underlying_type> {
private:
using Impl = base_internal::ListValueBuilderImpl<
T, typename base_internal::ValueTraits<T>::underlying_type>;
static_assert(!std::is_same_v<T, ListValue>);
public:
using Impl::Impl;
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_BASE_VALUES_LIST_VALUE_BUILDER_H_