/proc/self/cwd/common/values/optional_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 | | // `OptionalValue` represents values of the `optional_type` type. |
19 | | |
20 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_OPTIONAL_VALUE_H_ |
21 | | #define THIRD_PARTY_CEL_CPP_COMMON_VALUES_OPTIONAL_VALUE_H_ |
22 | | |
23 | | #include <type_traits> |
24 | | #include <utility> |
25 | | |
26 | | #include "absl/base/attributes.h" |
27 | | #include "absl/base/nullability.h" |
28 | | #include "absl/types/optional.h" |
29 | | #include "common/optional_ref.h" |
30 | | #include "common/type.h" |
31 | | #include "common/values/opaque_value.h" |
32 | | #include "google/protobuf/arena.h" |
33 | | |
34 | | namespace cel { |
35 | | |
36 | | class Value; |
37 | | class OptionalValue; |
38 | | |
39 | | namespace common_internal { |
40 | | OptionalValue MakeOptionalValue( |
41 | | const OpaqueValueDispatcher* absl_nonnull dispatcher, |
42 | | OpaqueValueContent content); |
43 | | } |
44 | | |
45 | | class OptionalValue final : public OpaqueValue { |
46 | | public: |
47 | | static OptionalValue None(); |
48 | | |
49 | | static OptionalValue Of(cel::Value value, google::protobuf::Arena* absl_nonnull arena); |
50 | | |
51 | 0 | OptionalValue() : OptionalValue(None()) {} |
52 | | OptionalValue(const OptionalValue&) = default; |
53 | | OptionalValue(OptionalValue&&) = default; |
54 | | OptionalValue& operator=(const OptionalValue&) = default; |
55 | | OptionalValue& operator=(OptionalValue&&) = default; |
56 | | |
57 | 0 | OptionalType GetRuntimeType() const { |
58 | 0 | return OpaqueValue::GetRuntimeType().GetOptional(); |
59 | 0 | } |
60 | | |
61 | | bool HasValue() const; |
62 | | |
63 | | void Value(cel::Value* absl_nonnull result) const; |
64 | | |
65 | | cel::Value Value() const; |
66 | | |
67 | | bool IsOptional() const = delete; |
68 | | template <typename T> |
69 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, bool> Is() const = delete; |
70 | | optional_ref<const OptionalValue> AsOptional() & = delete; |
71 | | optional_ref<const OptionalValue> AsOptional() const& = delete; |
72 | | absl::optional<OptionalValue> AsOptional() && = delete; |
73 | | absl::optional<OptionalValue> AsOptional() const&& = delete; |
74 | | const OptionalValue& GetOptional() & = delete; |
75 | | const OptionalValue& GetOptional() const& = delete; |
76 | | OptionalValue GetOptional() && = delete; |
77 | | OptionalValue GetOptional() const&& = delete; |
78 | | template <typename T> |
79 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, |
80 | | optional_ref<const OptionalValue>> |
81 | | As() & = delete; |
82 | | template <typename T> |
83 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, |
84 | | optional_ref<const OptionalValue>> |
85 | | As() const& = delete; |
86 | | template <typename T> |
87 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, |
88 | | absl::optional<OptionalValue>> |
89 | | As() && = delete; |
90 | | template <typename T> |
91 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, |
92 | | absl::optional<OptionalValue>> |
93 | | As() const&& = delete; |
94 | | template <typename T> |
95 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, |
96 | | optional_ref<const OptionalValue>> |
97 | | Get() & = delete; |
98 | | template <typename T> |
99 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, |
100 | | optional_ref<const OptionalValue>> |
101 | | Get() const& = delete; |
102 | | template <typename T> |
103 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, |
104 | | absl::optional<OptionalValue>> |
105 | | Get() && = delete; |
106 | | template <typename T> |
107 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, |
108 | | absl::optional<OptionalValue>> |
109 | | Get() const&& = delete; |
110 | | |
111 | | private: |
112 | | friend OptionalValue common_internal::MakeOptionalValue( |
113 | | const OpaqueValueDispatcher* absl_nonnull dispatcher, |
114 | | OpaqueValueContent content); |
115 | | |
116 | | OptionalValue(const OpaqueValueDispatcher* absl_nonnull dispatcher, |
117 | | OpaqueValueContent content) |
118 | 0 | : OpaqueValue(dispatcher, content) {} |
119 | | |
120 | | using OpaqueValue::content; |
121 | | using OpaqueValue::dispatcher; |
122 | | using OpaqueValue::interface; |
123 | | }; |
124 | | |
125 | | inline optional_ref<const OptionalValue> OpaqueValue::AsOptional() & |
126 | 0 | ABSL_ATTRIBUTE_LIFETIME_BOUND { |
127 | 0 | return std::as_const(*this).AsOptional(); |
128 | 0 | } |
129 | | |
130 | 0 | inline absl::optional<OptionalValue> OpaqueValue::AsOptional() const&& { |
131 | 0 | return common_internal::AsOptional(AsOptional()); |
132 | 0 | } |
133 | | |
134 | | template <typename T> |
135 | | inline std::enable_if_t<std::is_same_v<OptionalValue, T>, |
136 | | optional_ref<const OptionalValue>> |
137 | | OpaqueValue::As() & ABSL_ATTRIBUTE_LIFETIME_BOUND { |
138 | | return AsOptional(); |
139 | | } |
140 | | |
141 | | template <typename T> |
142 | | inline std::enable_if_t<std::is_same_v<OptionalValue, T>, |
143 | | optional_ref<const OptionalValue>> |
144 | | OpaqueValue::As() const& ABSL_ATTRIBUTE_LIFETIME_BOUND { |
145 | | return AsOptional(); |
146 | | } |
147 | | |
148 | | template <typename T> |
149 | | inline std::enable_if_t<std::is_same_v<OptionalValue, T>, |
150 | | absl::optional<OptionalValue>> |
151 | | OpaqueValue::As() && { |
152 | | return std::move(*this).AsOptional(); |
153 | | } |
154 | | |
155 | | template <typename T> |
156 | | inline std::enable_if_t<std::is_same_v<OptionalValue, T>, |
157 | | absl::optional<OptionalValue>> |
158 | | OpaqueValue::As() const&& { |
159 | | return std::move(*this).AsOptional(); |
160 | | } |
161 | | |
162 | | inline const OptionalValue& OpaqueValue::GetOptional() & |
163 | 0 | ABSL_ATTRIBUTE_LIFETIME_BOUND { |
164 | 0 | return std::as_const(*this).GetOptional(); |
165 | 0 | } |
166 | | |
167 | 0 | inline OptionalValue OpaqueValue::GetOptional() const&& { |
168 | 0 | return GetOptional(); |
169 | 0 | } |
170 | | |
171 | | template <typename T> |
172 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, const OptionalValue&> |
173 | | OpaqueValue::Get() & ABSL_ATTRIBUTE_LIFETIME_BOUND { |
174 | | return GetOptional(); |
175 | | } |
176 | | |
177 | | template <typename T> |
178 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, const OptionalValue&> |
179 | | OpaqueValue::Get() const& ABSL_ATTRIBUTE_LIFETIME_BOUND { |
180 | | return GetOptional(); |
181 | | } |
182 | | |
183 | | template <typename T> |
184 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, OptionalValue> |
185 | | OpaqueValue::Get() && { |
186 | | return std::move(*this).GetOptional(); |
187 | | } |
188 | | |
189 | | template <typename T> |
190 | | std::enable_if_t<std::is_same_v<OptionalValue, T>, OptionalValue> |
191 | | OpaqueValue::Get() const&& { |
192 | | return std::move(*this).GetOptional(); |
193 | | } |
194 | | |
195 | | namespace common_internal { |
196 | | |
197 | | inline OptionalValue MakeOptionalValue( |
198 | | const OpaqueValueDispatcher* absl_nonnull dispatcher, |
199 | 0 | OpaqueValueContent content) { |
200 | 0 | return OptionalValue(dispatcher, content); |
201 | 0 | } |
202 | | |
203 | | } // namespace common_internal |
204 | | |
205 | | } // namespace cel |
206 | | |
207 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_OPTIONAL_VALUE_H_ |