|
19 | 19 | #define THIRD_PARTY_CEL_CPP_COMMON_VALUES_ERROR_VALUE_H_ |
20 | 20 |
|
21 | 21 | #include <cstddef> |
| 22 | +#include <new> |
22 | 23 | #include <ostream> |
23 | 24 | #include <string> |
24 | 25 | #include <type_traits> |
|
30 | 31 | #include "absl/status/status.h" |
31 | 32 | #include "absl/strings/cord.h" |
32 | 33 | #include "absl/strings/string_view.h" |
33 | | -#include "absl/types/variant.h" |
34 | | -#include "absl/utility/utility.h" |
35 | 34 | #include "common/allocator.h" |
36 | 35 | #include "common/type.h" |
37 | 36 | #include "common/value_kind.h" |
|
43 | 42 | namespace cel { |
44 | 43 |
|
45 | 44 | class Value; |
46 | | -class ErrorValue; |
47 | | -class TypeManager; |
48 | 45 |
|
49 | 46 | // `ErrorValue` represents values of the `ErrorType`. |
50 | | -class ErrorValue final : private common_internal::ValueMixin<ErrorValue> { |
| 47 | +class ABSL_ATTRIBUTE_TRIVIAL_ABI ErrorValue final |
| 48 | + : private common_internal::ValueMixin<ErrorValue> { |
51 | 49 | public: |
52 | 50 | static constexpr ValueKind kKind = ValueKind::kError; |
53 | 51 |
|
54 | | - explicit ErrorValue(absl::Status value) |
55 | | - : variant_(absl::in_place_type<absl::Status>, std::move(value)) { |
| 52 | + explicit ErrorValue(absl::Status value) : arena_(nullptr) { |
| 53 | + ::new (static_cast<void*>(&status_.val[0])) absl::Status(std::move(value)); |
56 | 54 | ABSL_DCHECK(*this) << "ErrorValue requires a non-OK absl::Status"; |
57 | 55 | } |
58 | 56 |
|
59 | | - ErrorValue& operator=(absl::Status status) { |
60 | | - variant_.emplace<absl::Status>(std::move(status)); |
61 | | - ABSL_DCHECK(*this) << "ErrorValue requires a non-OK absl::Status"; |
62 | | - return *this; |
63 | | - } |
64 | | - |
65 | 57 | // By default, this creates an UNKNOWN error. You should always create a more |
66 | 58 | // specific error value. |
67 | 59 | ErrorValue(); |
68 | | - ErrorValue(const ErrorValue&) = default; |
69 | | - ErrorValue(ErrorValue&&) = default; |
70 | | - ErrorValue& operator=(const ErrorValue&) = default; |
71 | | - ErrorValue& operator=(ErrorValue&&) = default; |
72 | 60 |
|
73 | | - constexpr ValueKind kind() const { return kKind; } |
| 61 | + ErrorValue(const ErrorValue& other) { CopyConstruct(other); } |
| 62 | + |
| 63 | + ErrorValue(ErrorValue&& other) noexcept { MoveConstruct(other); } |
| 64 | + |
| 65 | + ~ErrorValue() { Destruct(); } |
| 66 | + |
| 67 | + ErrorValue& operator=(const ErrorValue& other) { |
| 68 | + if (this != &other) { |
| 69 | + Destruct(); |
| 70 | + CopyConstruct(other); |
| 71 | + } |
| 72 | + return *this; |
| 73 | + } |
| 74 | + |
| 75 | + ErrorValue& operator=(ErrorValue&& other) noexcept { |
| 76 | + if (this != &other) { |
| 77 | + Destruct(); |
| 78 | + MoveConstruct(other); |
| 79 | + } |
| 80 | + return *this; |
| 81 | + } |
74 | 82 |
|
75 | | - absl::string_view GetTypeName() const { return ErrorType::kName; } |
| 83 | + static constexpr ValueKind kind() { return kKind; } |
| 84 | + |
| 85 | + static absl::string_view GetTypeName() { return ErrorType::kName; } |
76 | 86 |
|
77 | 87 | std::string DebugString() const; |
78 | 88 |
|
@@ -116,18 +126,42 @@ class ErrorValue final : private common_internal::ValueMixin<ErrorValue> { |
116 | 126 | private: |
117 | 127 | friend class common_internal::ValueMixin<ErrorValue>; |
118 | 128 |
|
119 | | - using ArenaStatus = std::pair<absl::Nullable<google::protobuf::Arena*>, |
120 | | - absl::Nonnull<const absl::Status*>>; |
121 | | - using Variant = absl::variant<absl::Status, ArenaStatus>; |
122 | | - |
123 | | - ErrorValue(absl::Nullable<google::protobuf::Arena*> arena, |
| 129 | + ErrorValue(absl::Nonnull<google::protobuf::Arena*> arena, |
124 | 130 | absl::Nonnull<const absl::Status*> status) |
125 | | - : variant_(absl::in_place_type<ArenaStatus>, arena, status) {} |
| 131 | + : arena_(arena), status_{.ptr = status} {} |
| 132 | + |
| 133 | + void CopyConstruct(const ErrorValue& other) { |
| 134 | + arena_ = other.arena_; |
| 135 | + if (arena_ == nullptr) { |
| 136 | + ::new (static_cast<void*>(&status_.val[0])) absl::Status(*std::launder( |
| 137 | + reinterpret_cast<const absl::Status*>(&other.status_.val[0]))); |
| 138 | + } else { |
| 139 | + status_.ptr = other.status_.ptr; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + void MoveConstruct(ErrorValue& other) { |
| 144 | + arena_ = other.arena_; |
| 145 | + if (arena_ == nullptr) { |
| 146 | + ::new (static_cast<void*>(&status_.val[0])) |
| 147 | + absl::Status(std::move(*std::launder( |
| 148 | + reinterpret_cast<absl::Status*>(&other.status_.val[0])))); |
| 149 | + } else { |
| 150 | + status_.ptr = other.status_.ptr; |
| 151 | + } |
| 152 | + } |
126 | 153 |
|
127 | | - explicit ErrorValue(const ArenaStatus& status) |
128 | | - : ErrorValue(status.first, status.second) {} |
| 154 | + void Destruct() { |
| 155 | + if (arena_ == nullptr) { |
| 156 | + std::launder(reinterpret_cast<absl::Status*>(&status_.val[0]))->~Status(); |
| 157 | + } |
| 158 | + } |
129 | 159 |
|
130 | | - Variant variant_; |
| 160 | + absl::Nullable<google::protobuf::Arena*> arena_; |
| 161 | + union { |
| 162 | + alignas(absl::Status) char val[sizeof(absl::Status)]; |
| 163 | + absl::Nonnull<const absl::Status*> ptr; |
| 164 | + } status_; |
131 | 165 | }; |
132 | 166 |
|
133 | 167 | ErrorValue NoSuchFieldError(absl::string_view field); |
|
0 commit comments