-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathenum_value.h
More file actions
157 lines (125 loc) · 5.05 KB
/
enum_value.h
File metadata and controls
157 lines (125 loc) · 5.05 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
// Copyright 2024 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_ENUM_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_ENUM_VALUE_H_
#include <cstdint>
#include <ostream>
#include <string>
#include <type_traits>
#include "google/protobuf/struct.pb.h"
#include "absl/base/nullability.h"
#include "absl/meta/type_traits.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "common/type.h"
#include "common/value_kind.h"
#include "common/values/int_value.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/generated_enum_util.h"
#include "google/protobuf/message.h"
namespace cel {
namespace common_internal {
template <typename T, typename U = absl::remove_cv_t<T>>
inline constexpr bool kIsWellKnownEnumType =
std::is_same<google::protobuf::NullValue, U>::value;
template <typename T, typename U = absl::remove_cv_t<T>>
inline constexpr bool kIsGeneratedEnum = google::protobuf::is_proto_enum<U>::value;
template <typename T, typename U, typename R = void>
using EnableIfWellKnownEnum = std::enable_if_t<
kIsWellKnownEnumType<T> && std::is_same<absl::remove_cv_t<T>, U>::value, R>;
template <typename T, typename R = void>
using EnableIfGeneratedEnum = std::enable_if_t<
absl::conjunction<
std::bool_constant<kIsGeneratedEnum<T>>,
absl::negation<std::bool_constant<kIsWellKnownEnumType<T>>>>::value,
R>;
} // namespace common_internal
class Value;
class ValueManager;
class IntValue;
class TypeManager;
class EnumValue;
// `EnumValue` represents protobuf enum values which behave like values of the
// primitive `int` type, except that they return the enum name in
// `DebugString()`.
class EnumValue final {
public:
static constexpr ValueKind kKind = ValueKind::kInt;
explicit EnumValue(
absl::Nonnull<const google::protobuf::EnumValueDescriptor*> value) noexcept
: value_(value->number()), name_(value->name()) {}
explicit EnumValue(absl::string_view name, int64_t value) noexcept
: value_(value), name_(name) {}
EnumValue(const EnumValue&) = default;
EnumValue(EnumValue&&) = default;
EnumValue& operator=(const EnumValue&) = default;
EnumValue& operator=(EnumValue&&) = default;
ValueKind kind() const { return kKind; }
absl::string_view GetTypeName() const { return IntType::kName; }
absl::string_view GetEnumName() const { return name_; }
std::string DebugString() const { return std::string(GetEnumName()); }
// See Value::SerializeTo().
absl::Status SerializeTo(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Cord& value) const {
return IntValue(NativeValue())
.SerializeTo(descriptor_pool, message_factory, value);
}
// 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 {
return IntValue(NativeValue())
.ConvertToJson(descriptor_pool, message_factory, json);
}
absl::Status Equal(ValueManager& value_manager, const Value& other,
Value& result) const {
return IntValue(NativeValue()).Equal(value_manager, other, result);
}
absl::StatusOr<Value> Equal(ValueManager& value_manager,
const Value& other) const;
bool IsZeroValue() const { return NativeValue() == 0; }
int64_t NativeValue() const { return static_cast<int64_t>(*this); }
// NOLINTNEXTLINE(google-explicit-constructor)
operator int64_t() const noexcept { return value_; }
friend void swap(EnumValue& lhs, EnumValue& rhs) noexcept {
using std::swap;
swap(lhs.value_, rhs.value_);
swap(lhs.name_, rhs.name_);
}
private:
int64_t value_;
absl::string_view name_;
};
template <typename H>
H AbslHashValue(H state, EnumValue value) {
return H::combine(std::move(state), value.NativeValue());
}
inline bool operator==(EnumValue lhs, EnumValue rhs) {
return lhs.NativeValue() == rhs.NativeValue();
}
inline bool operator!=(EnumValue lhs, EnumValue rhs) {
return !operator==(lhs, rhs);
}
inline std::ostream& operator<<(std::ostream& out, EnumValue value) {
return out << value.DebugString();
}
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_ENUM_VALUE_H_