-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathoptional_value.h
More file actions
207 lines (171 loc) · 6.43 KB
/
Copy pathoptional_value.h
File metadata and controls
207 lines (171 loc) · 6.43 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2023 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"
// `OptionalValue` represents values of the `optional_type` type.
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_OPTIONAL_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_OPTIONAL_VALUE_H_
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/types/optional.h"
#include "common/optional_ref.h"
#include "common/type.h"
#include "common/values/opaque_value.h"
#include "google/protobuf/arena.h"
namespace cel {
class Value;
class OptionalValue;
namespace common_internal {
OptionalValue MakeOptionalValue(
const OpaqueValueDispatcher* absl_nonnull dispatcher,
OpaqueValueContent content);
}
class OptionalValue final : public OpaqueValue {
public:
static OptionalValue None();
static OptionalValue Of(cel::Value value, google::protobuf::Arena* absl_nonnull arena);
OptionalValue() : OptionalValue(None()) {}
OptionalValue(const OptionalValue&) = default;
OptionalValue(OptionalValue&&) = default;
OptionalValue& operator=(const OptionalValue&) = default;
OptionalValue& operator=(OptionalValue&&) = default;
OptionalType GetRuntimeType() const {
return OpaqueValue::GetRuntimeType().GetOptional();
}
bool HasValue() const;
void Value(cel::Value* absl_nonnull result) const;
cel::Value Value() const;
bool IsOptional() const = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>, bool> Is() const = delete;
optional_ref<const OptionalValue> AsOptional() & = delete;
optional_ref<const OptionalValue> AsOptional() const& = delete;
absl::optional<OptionalValue> AsOptional() && = delete;
absl::optional<OptionalValue> AsOptional() const&& = delete;
const OptionalValue& GetOptional() & = delete;
const OptionalValue& GetOptional() const& = delete;
OptionalValue GetOptional() && = delete;
OptionalValue GetOptional() const&& = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>,
optional_ref<const OptionalValue>>
As() & = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>,
optional_ref<const OptionalValue>>
As() const& = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>,
absl::optional<OptionalValue>>
As() && = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>,
absl::optional<OptionalValue>>
As() const&& = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>,
optional_ref<const OptionalValue>>
Get() & = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>,
optional_ref<const OptionalValue>>
Get() const& = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>,
absl::optional<OptionalValue>>
Get() && = delete;
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>,
absl::optional<OptionalValue>>
Get() const&& = delete;
private:
friend OptionalValue common_internal::MakeOptionalValue(
const OpaqueValueDispatcher* absl_nonnull dispatcher,
OpaqueValueContent content);
OptionalValue(const OpaqueValueDispatcher* absl_nonnull dispatcher,
OpaqueValueContent content)
: OpaqueValue(dispatcher, content) {}
using OpaqueValue::content;
using OpaqueValue::dispatcher;
using OpaqueValue::interface;
};
inline optional_ref<const OptionalValue> OpaqueValue::AsOptional() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsOptional();
}
inline absl::optional<OptionalValue> OpaqueValue::AsOptional() const&& {
return common_internal::AsOptional(AsOptional());
}
template <typename T>
inline std::enable_if_t<std::is_same_v<OptionalValue, T>,
optional_ref<const OptionalValue>>
OpaqueValue::As() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AsOptional();
}
template <typename T>
inline std::enable_if_t<std::is_same_v<OptionalValue, T>,
optional_ref<const OptionalValue>>
OpaqueValue::As() const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AsOptional();
}
template <typename T>
inline std::enable_if_t<std::is_same_v<OptionalValue, T>,
absl::optional<OptionalValue>>
OpaqueValue::As() && {
return std::move(*this).AsOptional();
}
template <typename T>
inline std::enable_if_t<std::is_same_v<OptionalValue, T>,
absl::optional<OptionalValue>>
OpaqueValue::As() const&& {
return std::move(*this).AsOptional();
}
inline const OptionalValue& OpaqueValue::GetOptional() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).GetOptional();
}
inline OptionalValue OpaqueValue::GetOptional() const&& {
return GetOptional();
}
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>, const OptionalValue&>
OpaqueValue::Get() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
return GetOptional();
}
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>, const OptionalValue&>
OpaqueValue::Get() const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
return GetOptional();
}
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>, OptionalValue>
OpaqueValue::Get() && {
return std::move(*this).GetOptional();
}
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>, OptionalValue>
OpaqueValue::Get() const&& {
return std::move(*this).GetOptional();
}
namespace common_internal {
inline OptionalValue MakeOptionalValue(
const OpaqueValueDispatcher* absl_nonnull dispatcher,
OpaqueValueContent content) {
return OptionalValue(dispatcher, content);
}
} // namespace common_internal
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_OPTIONAL_VALUE_H_