-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy patherror_value.h
More file actions
143 lines (107 loc) · 3.97 KB
/
Copy patherror_value.h
File metadata and controls
143 lines (107 loc) · 3.97 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
// 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_ERROR_VALUE_H_
#define THIRD_PARTY_CEL_CPP_BASE_VALUES_ERROR_VALUE_H_
#include <cstdint>
#include <string>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "base/kind.h"
#include "base/type.h"
#include "base/types/error_type.h"
#include "base/value.h"
namespace cel {
class ErrorValue final : public Value, public base_internal::InlineData {
public:
ABSL_ATTRIBUTE_PURE_FUNCTION static std::string DebugString(
const absl::Status& value);
static constexpr Kind kKind = ErrorType::kKind;
static bool Is(const Value& value) { return value.kind() == kKind; }
using Value::Is;
static const ErrorValue& Cast(const Value& value) {
ABSL_DCHECK(Is(value)) << "cannot cast " << value.type()->name()
<< " to error";
return static_cast<const ErrorValue&>(value);
}
constexpr Kind kind() const { return kKind; }
Handle<ErrorType> type() const { return ErrorType::Get(); }
std::string DebugString() const;
const absl::Status& value() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
private:
friend class ValueHandle;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
friend struct interop_internal::ErrorValueAccess;
static constexpr uintptr_t kMetadata =
base_internal::kStoredInline |
(static_cast<uintptr_t>(kKind) << base_internal::kKindShift);
explicit ErrorValue(absl::Status value)
: base_internal::InlineData(kMetadata), value_(std::move(value)) {}
explicit ErrorValue(const absl::Status* value_ptr)
: base_internal::InlineData(kMetadata | base_internal::kTrivial),
value_ptr_(value_ptr) {}
ErrorValue(const ErrorValue& other) : ErrorValue(other.value_) {
// Only called when `other.value_` is the active member.
}
ErrorValue(ErrorValue&& other) : ErrorValue(std::move(other.value_)) {
// Only called when `other.value_` is the active member.
}
~ErrorValue() {
// Only called when `value_` is the active member.
value_.~Status();
}
ErrorValue& operator=(const ErrorValue& other) {
// Only called when `value_` and `other.value_` are the active members.
if (ABSL_PREDICT_TRUE(this != &other)) {
value_ = other.value_;
}
return *this;
}
ErrorValue& operator=(ErrorValue&& other) {
// Only called when `value_` and `other.value_` are the active members.
if (ABSL_PREDICT_TRUE(this != &other)) {
value_ = std::move(other.value_);
}
return *this;
}
union {
absl::Status value_;
const absl::Status* value_ptr_;
};
};
CEL_INTERNAL_VALUE_DECL(ErrorValue);
namespace base_internal {
template <>
struct ValueTraits<ErrorValue> {
using type = ErrorValue;
using type_type = ErrorType;
using underlying_type = absl::Status;
static std::string DebugString(const underlying_type& value) {
return type::DebugString(value);
}
static std::string DebugString(const type& value) {
return value.DebugString();
}
static Handle<type> Wrap(ValueFactory& value_factory, underlying_type value);
static underlying_type Unwrap(underlying_type value) { return value; }
static underlying_type Unwrap(const Handle<type>& value) {
return Unwrap(value->value());
}
};
} // namespace base_internal
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_BASE_VALUES_ERROR_VALUE_H_