/proc/self/cwd/common/values/bool_value.h
Line | Count | Source |
1 | | // Copyright 2023 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // IWYU pragma: private, include "common/value.h" |
16 | | // IWYU pragma: friend "common/value.h" |
17 | | |
18 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_BOOL_VALUE_H_ |
19 | | #define THIRD_PARTY_CEL_CPP_COMMON_VALUES_BOOL_VALUE_H_ |
20 | | |
21 | | #include <ostream> |
22 | | #include <string> |
23 | | |
24 | | #include "absl/base/nullability.h" |
25 | | #include "absl/status/status.h" |
26 | | #include "absl/strings/string_view.h" |
27 | | #include "common/type.h" |
28 | | #include "common/value_kind.h" |
29 | | #include "common/values/values.h" |
30 | | #include "google/protobuf/arena.h" |
31 | | #include "google/protobuf/descriptor.h" |
32 | | #include "google/protobuf/io/zero_copy_stream.h" |
33 | | #include "google/protobuf/message.h" |
34 | | |
35 | | namespace cel { |
36 | | |
37 | | class Value; |
38 | | class BoolValue; |
39 | | |
40 | | // `BoolValue` represents values of the primitive `bool` type. |
41 | | class BoolValue final : private common_internal::ValueMixin<BoolValue> { |
42 | | public: |
43 | | static constexpr ValueKind kKind = ValueKind::kBool; |
44 | | |
45 | | BoolValue() = default; |
46 | | BoolValue(const BoolValue&) = default; |
47 | | BoolValue(BoolValue&&) = default; |
48 | | BoolValue& operator=(const BoolValue&) = default; |
49 | | BoolValue& operator=(BoolValue&&) = default; |
50 | | |
51 | 32.8k | explicit BoolValue(bool value) noexcept : value_(value) {} |
52 | | |
53 | | // NOLINTNEXTLINE(google-explicit-constructor) |
54 | 24.8k | operator bool() const noexcept { return value_; } |
55 | | |
56 | 0 | ValueKind kind() const { return kKind; } |
57 | | |
58 | 0 | absl::string_view GetTypeName() const { return BoolType::kName; } |
59 | | |
60 | | std::string DebugString() const; |
61 | | |
62 | | // See Value::SerializeTo(). |
63 | | absl::Status SerializeTo( |
64 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
65 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
66 | | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const; |
67 | | |
68 | | // See Value::ConvertToJson(). |
69 | | absl::Status ConvertToJson( |
70 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
71 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
72 | | google::protobuf::Message* absl_nonnull json) const; |
73 | | |
74 | | absl::Status Equal(const Value& other, |
75 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
76 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
77 | | google::protobuf::Arena* absl_nonnull arena, |
78 | | Value* absl_nonnull result) const; |
79 | | using ValueMixin::Equal; |
80 | | |
81 | 0 | bool IsZeroValue() const { return NativeValue() == false; } |
82 | | |
83 | 24.8k | bool NativeValue() const { return static_cast<bool>(*this); } |
84 | | |
85 | 0 | friend void swap(BoolValue& lhs, BoolValue& rhs) noexcept { |
86 | 0 | using std::swap; |
87 | 0 | swap(lhs.value_, rhs.value_); |
88 | 0 | } |
89 | | |
90 | | private: |
91 | | friend class common_internal::ValueMixin<BoolValue>; |
92 | | |
93 | | bool value_ = false; |
94 | | }; |
95 | | |
96 | | template <typename H> |
97 | 0 | H AbslHashValue(H state, BoolValue value) { |
98 | 0 | return H::combine(std::move(state), value.NativeValue()); |
99 | 0 | } |
100 | | |
101 | 0 | inline std::ostream& operator<<(std::ostream& out, BoolValue value) { |
102 | 0 | return out << value.DebugString(); |
103 | 0 | } |
104 | | |
105 | 3.18k | inline BoolValue FalseValue() noexcept { return BoolValue(false); } |
106 | | |
107 | 84 | inline BoolValue TrueValue() noexcept { return BoolValue(true); } |
108 | | |
109 | | } // namespace cel |
110 | | |
111 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_BOOL_VALUE_H_ |