-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy patherror_value.h
More file actions
301 lines (227 loc) · 8.63 KB
/
error_value.h
File metadata and controls
301 lines (227 loc) · 8.63 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
// 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.
// IWYU pragma: private, include "common/value.h"
// IWYU pragma: friend "common/value.h"
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_ERROR_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_ERROR_VALUE_H_
#include <cstddef>
#include <memory>
#include <new>
#include <ostream>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "common/arena.h"
#include "common/type.h"
#include "common/value_kind.h"
#include "common/values/values.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel {
class Value;
// `ErrorValue` represents values of the `ErrorType`.
class ABSL_ATTRIBUTE_TRIVIAL_ABI ErrorValue final
: private common_internal::ValueMixin<ErrorValue> {
public:
static constexpr ValueKind kKind = ValueKind::kError;
explicit ErrorValue(absl::Status value) : arena_(nullptr) {
::new (static_cast<void*>(&status_.val[0])) absl::Status(std::move(value));
ABSL_DCHECK(*this) << "ErrorValue requires a non-OK absl::Status";
}
// By default, this creates an UNKNOWN error. You should always create a more
// specific error value.
ErrorValue();
ErrorValue(const ErrorValue& other) { CopyConstruct(other); }
ErrorValue(ErrorValue&& other) noexcept { MoveConstruct(other); }
~ErrorValue() { Destruct(); }
ErrorValue& operator=(const ErrorValue& other) {
if (this != &other) {
Destruct();
CopyConstruct(other);
}
return *this;
}
ErrorValue& operator=(ErrorValue&& other) noexcept {
if (this != &other) {
Destruct();
MoveConstruct(other);
}
return *this;
}
static constexpr ValueKind kind() { return kKind; }
static absl::string_view GetTypeName() { return ErrorType::kName; }
std::string DebugString() const;
// See Value::SerializeTo().
absl::Status SerializeTo(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<absl::Cord*> value) const;
// See Value::ConvertToJson().
absl::Status ConvertToJson(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Message*> json) const;
absl::Status Equal(
const Value& other,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const;
using ValueMixin::Equal;
bool IsZeroValue() const { return false; }
ErrorValue Clone(absl::Nonnull<google::protobuf::Arena*> arena) const;
absl::Status ToStatus() const&;
absl::Status ToStatus() &&;
ABSL_DEPRECATED("Use ToStatus()")
absl::Status NativeValue() const& { return ToStatus(); }
ABSL_DEPRECATED("Use ToStatus()")
absl::Status NativeValue() && { return std::move(*this).ToStatus(); }
friend void swap(ErrorValue& lhs, ErrorValue& rhs) noexcept;
explicit operator bool() const;
private:
friend class common_internal::ValueMixin<ErrorValue>;
friend struct ArenaTraits<ErrorValue>;
ErrorValue(absl::Nonnull<google::protobuf::Arena*> arena,
absl::Nonnull<const absl::Status*> status)
: arena_(arena), status_{.ptr = status} {}
void CopyConstruct(const ErrorValue& other) {
arena_ = other.arena_;
if (arena_ == nullptr) {
::new (static_cast<void*>(&status_.val[0])) absl::Status(*std::launder(
reinterpret_cast<const absl::Status*>(&other.status_.val[0])));
} else {
status_.ptr = other.status_.ptr;
}
}
void MoveConstruct(ErrorValue& other) {
arena_ = other.arena_;
if (arena_ == nullptr) {
::new (static_cast<void*>(&status_.val[0]))
absl::Status(std::move(*std::launder(
reinterpret_cast<absl::Status*>(&other.status_.val[0]))));
} else {
status_.ptr = other.status_.ptr;
}
}
void Destruct() {
if (arena_ == nullptr) {
std::launder(reinterpret_cast<absl::Status*>(&status_.val[0]))->~Status();
}
}
absl::Nullable<google::protobuf::Arena*> arena_;
union {
alignas(absl::Status) char val[sizeof(absl::Status)];
absl::Nonnull<const absl::Status*> ptr;
} status_;
};
ErrorValue NoSuchFieldError(absl::string_view field);
ErrorValue NoSuchKeyError(absl::string_view key);
ErrorValue NoSuchTypeError(absl::string_view type);
ErrorValue DuplicateKeyError();
ErrorValue TypeConversionError(absl::string_view from, absl::string_view to);
ErrorValue TypeConversionError(const Type& from, const Type& to);
ErrorValue IndexOutOfBoundsError(size_t index);
ErrorValue IndexOutOfBoundsError(ptrdiff_t index);
ErrorValue IntDivisionByZeroError();
ErrorValue UintDivisionByZeroError();
ErrorValue IntModuloByZeroError();
ErrorValue UintModuloByZeroError();
ErrorValue IntOverflowError();
ErrorValue UintOverflowError();
ErrorValue DurationOverflowError();
ErrorValue TimestampOverflowError();
ErrorValue DoubleToIntOutOfRangeError();
ErrorValue DoubleToUintOutOfRangeError();
ErrorValue IntToUintOutOfRangeError();
ErrorValue UintToIntOutOfRangeError();
ErrorValue Int64ToInt32OutOfRangeError();
ErrorValue Uint64ToUint32OutOfRangeError();
// Catch other integrals and forward them to the above ones. This is needed to
// avoid ambiguous overload issues for smaller integral types like `int`.
template <typename T>
std::enable_if_t<std::conjunction_v<std::is_integral<T>, std::is_unsigned<T>,
std::negation<std::is_same<T, size_t>>>,
ErrorValue>
IndexOutOfBoundsError(T index) {
static_assert(sizeof(T) <= sizeof(size_t));
return IndexOutOfBoundsError(static_cast<size_t>(index));
}
template <typename T>
std::enable_if_t<std::conjunction_v<std::is_integral<T>, std::is_signed<T>,
std::negation<std::is_same<T, ptrdiff_t>>>,
ErrorValue>
IndexOutOfBoundsError(T index) {
static_assert(sizeof(T) <= sizeof(ptrdiff_t));
return IndexOutOfBoundsError(static_cast<ptrdiff_t>(index));
}
inline std::ostream& operator<<(std::ostream& out, const ErrorValue& value) {
return out << value.DebugString();
}
bool IsNoSuchField(const ErrorValue& value);
bool IsNoSuchKey(const ErrorValue& value);
class ErrorValueReturn final {
public:
ErrorValueReturn() = default;
ErrorValue operator()(absl::Status status) const {
return ErrorValue(std::move(status));
}
};
namespace common_internal {
struct ImplicitlyConvertibleStatus {
// NOLINTNEXTLINE(google-explicit-constructor)
operator absl::Status() const { return absl::OkStatus(); }
template <typename T>
// NOLINTNEXTLINE(google-explicit-constructor)
operator absl::StatusOr<T>() const {
return T();
}
};
} // namespace common_internal
// For use with `RETURN_IF_ERROR(...).With(cel::ErrorValueAssign(&result))` and
// `ASSIGN_OR_RETURN(..., ..., _.With(cel::ErrorValueAssign(&result)))`.
//
// IMPORTANT:
// If the returning type is `absl::Status` the result will be
// `absl::OkStatus()`. If the returning type is `absl::StatusOr<T>` the result
// will be `T()`.
class ErrorValueAssign final {
public:
ErrorValueAssign() = delete;
explicit ErrorValueAssign(Value& value ABSL_ATTRIBUTE_LIFETIME_BOUND)
: ErrorValueAssign(std::addressof(value)) {}
explicit ErrorValueAssign(
absl::Nonnull<Value*> value ABSL_ATTRIBUTE_LIFETIME_BOUND)
: value_(value) {
ABSL_DCHECK(value != nullptr);
}
common_internal::ImplicitlyConvertibleStatus operator()(
absl::Status status) const;
private:
absl::Nonnull<Value*> value_;
};
template <>
struct ArenaTraits<ErrorValue> {
static bool trivially_destructible(const ErrorValue& value) {
return value.arena_ != nullptr;
}
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_ERROR_VALUE_H_