1818#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_ENUM_VALUE_H_
1919#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_ENUM_VALUE_H_
2020
21+ #include < cstdint>
22+ #include < ostream>
23+ #include < string>
2124#include < type_traits>
2225
2326#include " google/protobuf/struct.pb.h"
27+ #include " absl/base/nullability.h"
2428#include " absl/meta/type_traits.h"
29+ #include " absl/status/status.h"
30+ #include " absl/status/statusor.h"
31+ #include " absl/strings/cord.h"
32+ #include " absl/strings/string_view.h"
33+ #include " common/type.h"
34+ #include " common/value_kind.h"
35+ #include " common/values/int_value.h"
36+ #include " google/protobuf/descriptor.h"
2537#include " google/protobuf/generated_enum_util.h"
38+ #include " google/protobuf/message.h"
2639
27- namespace cel ::common_internal {
40+ namespace cel {
41+ namespace common_internal {
2842
2943template <typename T, typename U = absl::remove_cv_t <T>>
3044inline constexpr bool kIsWellKnownEnumType =
@@ -44,6 +58,100 @@ using EnableIfGeneratedEnum = std::enable_if_t<
4458 absl::negation<std::bool_constant<kIsWellKnownEnumType <T>>>>::value,
4559 R>;
4660
47- } // namespace cel::common_internal
61+ } // namespace common_internal
62+
63+ class Value ;
64+ class ValueManager ;
65+ class IntValue ;
66+ class TypeManager ;
67+ class EnumValue ;
68+
69+ // `EnumValue` represents protobuf enum values which behave like values of the
70+ // primitive `int` type, except that they return the enum name in
71+ // `DebugString()`.
72+ class EnumValue final {
73+ public:
74+ static constexpr ValueKind kKind = ValueKind::kInt ;
75+
76+ explicit EnumValue (
77+ absl::Nonnull<const google::protobuf::EnumValueDescriptor*> value) noexcept
78+ : value_(value->number ()), name_(value->name ()) {}
79+ explicit EnumValue (absl::string_view name, int64_t value) noexcept
80+ : value_(value), name_(name) {}
81+
82+ EnumValue (const EnumValue&) = default;
83+ EnumValue (EnumValue&&) = default;
84+ EnumValue& operator =(const EnumValue&) = default;
85+ EnumValue& operator =(EnumValue&&) = default;
86+
87+ ValueKind kind () const { return kKind ; }
88+
89+ absl::string_view GetTypeName () const { return IntType::kName ; }
90+
91+ absl::string_view GetEnumName () const { return name_; }
92+
93+ std::string DebugString () const { return std::string (GetEnumName ()); }
94+
95+ // See Value::SerializeTo().
96+ absl::Status SerializeTo (
97+ absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
98+ absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
99+ absl::Cord& value) const {
100+ return IntValue (NativeValue ())
101+ .SerializeTo (descriptor_pool, message_factory, value);
102+ }
103+
104+ // See Value::ConvertToJson().
105+ absl::Status ConvertToJson (
106+ absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
107+ absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
108+ absl::Nonnull<google::protobuf::Message*> json) const {
109+ return IntValue (NativeValue ())
110+ .ConvertToJson (descriptor_pool, message_factory, json);
111+ }
112+
113+ absl::Status Equal (ValueManager& value_manager, const Value& other,
114+ Value& result) const {
115+ return IntValue (NativeValue ()).Equal (value_manager, other, result);
116+ }
117+ absl::StatusOr<Value> Equal (ValueManager& value_manager,
118+ const Value& other) const ;
119+
120+ bool IsZeroValue () const { return NativeValue () == 0 ; }
121+
122+ int64_t NativeValue () const { return static_cast <int64_t >(*this ); }
123+
124+ // NOLINTNEXTLINE(google-explicit-constructor)
125+ operator int64_t () const noexcept { return value_; }
126+
127+ friend void swap (EnumValue& lhs, EnumValue& rhs) noexcept {
128+ using std::swap;
129+ swap (lhs.value_ , rhs.value_ );
130+ swap (lhs.name_ , rhs.name_ );
131+ }
132+
133+ private:
134+ int64_t value_;
135+ absl::string_view name_;
136+ };
137+
138+ template <typename H>
139+ H AbslHashValue (H state, EnumValue value) {
140+ return H::combine (std::move (state), value.NativeValue ());
141+ }
142+
143+ inline bool operator ==(EnumValue lhs, EnumValue rhs) {
144+ return lhs.NativeValue () == rhs.NativeValue ();
145+ }
146+
147+ inline bool operator !=(EnumValue lhs, EnumValue rhs) {
148+ return !operator ==(lhs, rhs);
149+ }
150+
151+ inline std::ostream& operator <<(std::ostream& out, EnumValue value) {
152+ return out << value.DebugString ();
153+ }
154+
155+ } // namespace cel
48156
49157#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_ENUM_VALUE_H_
0 commit comments