-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathlist_value.cc
More file actions
193 lines (154 loc) · 6.11 KB
/
Copy pathlist_value.cc
File metadata and controls
193 lines (154 loc) · 6.11 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
// 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.
#include "base/values/list_value.h"
#include <cstddef>
#include <string>
#include <utility>
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
#include "absl/status/statusor.h"
#include "base/handle.h"
#include "base/internal/data.h"
#include "base/type.h"
#include "base/types/list_type.h"
#include "internal/rtti.h"
#include "internal/status_macros.h"
namespace cel {
CEL_INTERNAL_VALUE_IMPL(ListValue);
#define CEL_INTERNAL_LIST_VALUE_DISPATCH(method, ...) \
base_internal::Metadata::IsStoredInline(*this) \
? static_cast<const base_internal::LegacyListValue&>(*this).method( \
__VA_ARGS__) \
: static_cast<const base_internal::AbstractListValue&>(*this).method( \
__VA_ARGS__)
Handle<ListType> ListValue::type() const {
return CEL_INTERNAL_LIST_VALUE_DISPATCH(type);
}
std::string ListValue::DebugString() const {
return CEL_INTERNAL_LIST_VALUE_DISPATCH(DebugString);
}
size_t ListValue::size() const {
return CEL_INTERNAL_LIST_VALUE_DISPATCH(size);
}
bool ListValue::empty() const {
return CEL_INTERNAL_LIST_VALUE_DISPATCH(empty);
}
absl::StatusOr<Handle<Value>> ListValue::Get(const GetContext& context,
size_t index) const {
return CEL_INTERNAL_LIST_VALUE_DISPATCH(Get, context, index);
}
absl::StatusOr<UniqueRef<ListValue::Iterator>> ListValue::NewIterator(
MemoryManager& memory_manager) const {
return CEL_INTERNAL_LIST_VALUE_DISPATCH(NewIterator, memory_manager);
}
internal::TypeInfo ListValue::TypeId() const {
return CEL_INTERNAL_LIST_VALUE_DISPATCH(TypeId);
}
#undef CEL_INTERNAL_LIST_VALUE_DISPATCH
absl::StatusOr<size_t> ListValue::Iterator::NextIndex(
const ListValue::GetContext& context) {
CEL_ASSIGN_OR_RETURN(auto element, Next(context));
return element.index;
}
absl::StatusOr<Handle<Value>> ListValue::Iterator::NextValue(
const ListValue::GetContext& context) {
CEL_ASSIGN_OR_RETURN(auto element, Next(context));
return std::move(element.value);
}
namespace base_internal {
namespace {
class LegacyListValueIterator final : public ListValue::Iterator {
public:
explicit LegacyListValueIterator(uintptr_t impl)
: impl_(impl), size_(LegacyListValueSize(impl_)) {}
bool HasNext() override { return index_ < size_; }
absl::StatusOr<Element> Next(const ListValue::GetContext& context) override {
if (ABSL_PREDICT_FALSE(index_ >= size_)) {
return absl::FailedPreconditionError(
"ListValue::Iterator::Next() called when "
"ListValue::Iterator::HasNext() returns false");
}
CEL_ASSIGN_OR_RETURN(
auto value, LegacyListValueGet(impl_, context.value_factory(), index_));
return Element(index_++, std::move(value));
}
absl::StatusOr<size_t> NextIndex(
const ListValue::GetContext& context) override {
if (ABSL_PREDICT_FALSE(index_ >= size_)) {
return absl::FailedPreconditionError(
"ListValue::Iterator::Next() called when "
"ListValue::Iterator::HasNext() returns false");
}
return index_++;
}
private:
const uintptr_t impl_;
const size_t size_;
size_t index_ = 0;
};
class AbstractListValueIterator final : public ListValue::Iterator {
public:
explicit AbstractListValueIterator(const AbstractListValue* value)
: value_(value), size_(value_->size()) {}
bool HasNext() override { return index_ < size_; }
absl::StatusOr<Element> Next(const ListValue::GetContext& context) override {
if (ABSL_PREDICT_FALSE(index_ >= size_)) {
return absl::FailedPreconditionError(
"ListValue::Iterator::Next() called when "
"ListValue::Iterator::HasNext() returns false");
}
CEL_ASSIGN_OR_RETURN(auto value, value_->Get(context, index_));
return Element(index_++, std::move(value));
}
absl::StatusOr<size_t> NextIndex(
const ListValue::GetContext& context) override {
if (ABSL_PREDICT_FALSE(index_ >= size_)) {
return absl::FailedPreconditionError(
"ListValue::Iterator::Next() called when "
"ListValue::Iterator::HasNext() returns false");
}
return index_++;
}
private:
const AbstractListValue* const value_;
const size_t size_;
size_t index_ = 0;
};
} // namespace
Handle<ListType> LegacyListValue::type() const {
return HandleFactory<ListType>::Make<LegacyListType>();
}
std::string LegacyListValue::DebugString() const { return "list"; }
size_t LegacyListValue::size() const { return LegacyListValueSize(impl_); }
bool LegacyListValue::empty() const { return LegacyListValueEmpty(impl_); }
absl::StatusOr<UniqueRef<ListValue::Iterator>> LegacyListValue::NewIterator(
MemoryManager& memory_manager) const {
return MakeUnique<LegacyListValueIterator>(memory_manager, impl_);
}
absl::StatusOr<Handle<Value>> LegacyListValue::Get(const GetContext& context,
size_t index) const {
return LegacyListValueGet(impl_, context.value_factory(), index);
}
AbstractListValue::AbstractListValue(Handle<ListType> type)
: HeapData(kKind), type_(std::move(type)) {
// Ensure `Value*` and `HeapData*` are not thunked.
ABSL_ASSERT(reinterpret_cast<uintptr_t>(static_cast<Value*>(this)) ==
reinterpret_cast<uintptr_t>(static_cast<HeapData*>(this)));
}
absl::StatusOr<UniqueRef<ListValue::Iterator>> AbstractListValue::NewIterator(
MemoryManager& memory_manager) const {
return MakeUnique<AbstractListValueIterator>(memory_manager, this);
}
} // namespace base_internal
} // namespace cel