Skip to content

Commit b28386a

Browse files
jckingcopybara-github
authored andcommitted
Migrate cel::optional_ref to absl::optional_ref
PiperOrigin-RevId: 991014396
1 parent 96346c7 commit b28386a

4 files changed

Lines changed: 98 additions & 199 deletions

File tree

‎common/BUILD‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ cc_library(
830830
"@com_google_absl//absl/strings:string_view",
831831
"@com_google_absl//absl/time",
832832
"@com_google_absl//absl/types:optional",
833+
"@com_google_absl//absl/types:optional_ref",
833834
"@com_google_absl//absl/types:source_location",
834835
"@com_google_absl//absl/types:span",
835836
"@com_google_absl//absl/types:variant",
@@ -976,9 +977,7 @@ cc_library(
976977
hdrs = ["optional_ref.h"],
977978
deps = [
978979
"@com_google_absl//absl/base:core_headers",
979-
"@com_google_absl//absl/base:nullability",
980-
"@com_google_absl//absl/types:optional",
981-
"@com_google_absl//absl/utility",
980+
"@com_google_absl//absl/types:optional_ref",
982981
],
983982
)
984983

‎common/optional_ref.h‎

Lines changed: 9 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -15,144 +15,34 @@
1515
#ifndef THIRD_PARTY_CEL_CPP_OPTIONAL_REF_H_
1616
#define THIRD_PARTY_CEL_CPP_OPTIONAL_REF_H_
1717

18-
#include <memory>
18+
#include <optional>
1919
#include <type_traits>
2020

21-
#include "absl/base/attributes.h"
2221
#include "absl/base/macros.h"
23-
#include "absl/base/nullability.h"
24-
#include "absl/base/optimization.h"
25-
#include "absl/types/optional.h"
26-
#include "absl/utility/utility.h"
22+
#include "absl/types/optional_ref.h"
2723

2824
namespace cel {
2925

3026
// `optional_ref<T>` looks and feels like `absl::optional<T>`, but instead of
3127
// owning the underlying value, it retains a reference to the value it accepts
3228
// in its constructor.
3329
template <typename T>
34-
class optional_ref final {
35-
public:
36-
static_assert(!std::is_reference_v<T>, "T must not be a reference.");
37-
static_assert(!std::is_same_v<absl::nullopt_t, std::remove_cv_t<T>>,
38-
"optional_ref<absl::nullopt_t> is not allowed.");
39-
static_assert(!std::is_same_v<absl::in_place_t, std::remove_cv_t<T>>,
40-
"optional_ref<absl::in_place_t> is not allowed.");
41-
42-
using value_type = T;
43-
44-
optional_ref() = default;
45-
46-
// NOLINTNEXTLINE(google-explicit-constructor)
47-
constexpr optional_ref(absl::nullopt_t) : optional_ref() {}
48-
49-
// NOLINTNEXTLINE(google-explicit-constructor)
50-
constexpr optional_ref(T& value ABSL_ATTRIBUTE_LIFETIME_BOUND)
51-
: value_(std::addressof(value)) {}
52-
53-
template <
54-
typename U,
55-
typename = std::enable_if_t<std::conjunction_v<
56-
std::is_const<T>, std::is_same<std::decay_t<U>, std::decay_t<T>>>>>
57-
// NOLINTNEXTLINE(google-explicit-constructor)
58-
constexpr optional_ref(
59-
const absl::optional<U>& value ABSL_ATTRIBUTE_LIFETIME_BOUND)
60-
: value_(value.has_value() ? std::addressof(*value) : nullptr) {}
61-
62-
template <typename U, typename = std::enable_if_t<
63-
std::is_same_v<std::decay_t<U>, std::decay_t<T>>>>
64-
// NOLINTNEXTLINE(google-explicit-constructor)
65-
constexpr optional_ref(absl::optional<U>& value ABSL_ATTRIBUTE_LIFETIME_BOUND)
66-
: value_(value.has_value() ? std::addressof(*value) : nullptr) {}
67-
68-
template <
69-
typename U,
70-
typename = std::enable_if_t<std::conjunction_v<
71-
std::negation<std::is_same<U, T>>,
72-
std::is_convertible<std::add_pointer_t<U>, std::add_pointer_t<T>>>>>
73-
// NOLINTNEXTLINE(google-explicit-constructor)
74-
constexpr optional_ref(const optional_ref<U>& other) : value_(other.value_) {}
75-
76-
optional_ref(const optional_ref<T>&) = default;
77-
78-
optional_ref<T>& operator=(const optional_ref<T>&) = delete;
79-
80-
constexpr bool has_value() const { return value_ != nullptr; }
81-
82-
constexpr explicit operator bool() const { return has_value(); }
83-
84-
constexpr T& value() const {
85-
return ABSL_PREDICT_TRUE(has_value())
86-
? *value_
87-
// Replicate the same error logic as in `absl::optional`'s
88-
// `value()`. It either throws an exception or aborts the
89-
// program. We intentionally ignore the return value of
90-
// the constructed optional's value as we only need to run
91-
// the code for error checking.
92-
: ((void)absl::optional<T>().value(), *value_);
93-
}
94-
95-
constexpr T& operator*() const {
96-
ABSL_ASSERT(has_value());
97-
return *value_;
98-
}
99-
100-
constexpr T* absl_nonnull operator->() const {
101-
ABSL_ASSERT(has_value());
102-
return value_;
103-
}
104-
105-
private:
106-
template <typename U>
107-
friend class optional_ref;
108-
109-
T* const value_ = nullptr;
110-
};
111-
112-
template <typename T>
113-
optional_ref(const T&) -> optional_ref<const T>;
114-
115-
template <typename T>
116-
optional_ref(T&) -> optional_ref<T>;
117-
118-
template <typename T>
119-
optional_ref(const absl::optional<T>&) -> optional_ref<const T>;
120-
121-
template <typename T>
122-
optional_ref(absl::optional<T>&) -> optional_ref<T>;
123-
124-
template <typename T>
125-
constexpr bool operator==(const optional_ref<T>& lhs, absl::nullopt_t) {
126-
return !lhs.has_value();
127-
}
128-
129-
template <typename T>
130-
constexpr bool operator==(absl::nullopt_t, const optional_ref<T>& rhs) {
131-
return !rhs.has_value();
132-
}
133-
134-
template <typename T>
135-
constexpr bool operator!=(const optional_ref<T>& lhs, absl::nullopt_t) {
136-
return !operator==(lhs, absl::nullopt);
137-
}
138-
139-
template <typename T>
140-
constexpr bool operator!=(absl::nullopt_t, const optional_ref<T>& rhs) {
141-
return !operator==(absl::nullopt, rhs);
142-
}
30+
using optional_ref ABSL_DEPRECATE_AND_INLINE() = absl::optional_ref<T>;
14331

14432
namespace common_internal {
14533

14634
template <typename T>
147-
absl::optional<std::decay_t<T>> AsOptional(optional_ref<T> ref) {
148-
if (ref) {
35+
[[nodiscard]]
36+
std::optional<std::decay_t<T>> AsOptional(absl::optional_ref<T> ref) {
37+
if (ref.has_value()) {
14938
return *ref;
15039
}
151-
return absl::nullopt;
40+
return std::nullopt;
15241
}
15342

15443
template <typename T>
155-
absl::optional<T> AsOptional(absl::optional<T> opt) {
44+
[[nodiscard]]
45+
std::optional<T> AsOptional(std::optional<T> opt) {
15646
return opt;
15747
}
15848

‎common/value.cc‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
#include "absl/strings/string_view.h"
4141
#include "absl/time/time.h"
4242
#include "absl/types/optional.h"
43+
#include "absl/types/optional_ref.h"
4344
#include "absl/types/source_location.h"
4445
#include "absl/types/variant.h"
4546
#include "common/allocator.h"
4647
#include "common/memory.h"
47-
#include "common/optional_ref.h"
4848
#include "common/type.h"
4949
#include "common/value_kind.h"
5050
#include "common/values/list_value_builder.h"
@@ -2008,7 +2008,7 @@ Value Value::WrapMapFieldValueUnsafe(
20082008
message_factory, arena);
20092009
}
20102010

2011-
optional_ref<const BytesValue> Value::AsBytes() const& {
2011+
absl::optional_ref<const BytesValue> Value::AsBytes() const& {
20122012
if (const auto* alternative = variant_.As<BytesValue>();
20132013
alternative != nullptr) {
20142014
return *alternative;
@@ -2039,7 +2039,7 @@ absl::optional<DurationValue> Value::AsDuration() const {
20392039
return std::nullopt;
20402040
}
20412041

2042-
optional_ref<const ErrorValue> Value::AsError() const& {
2042+
absl::optional_ref<const ErrorValue> Value::AsError() const& {
20432043
if (const auto* alternative = variant_.As<ErrorValue>();
20442044
alternative != nullptr) {
20452045
return *alternative;
@@ -2166,7 +2166,7 @@ absl::optional<NullValue> Value::AsNull() const {
21662166
return std::nullopt;
21672167
}
21682168

2169-
optional_ref<const OpaqueValue> Value::AsOpaque() const& {
2169+
absl::optional_ref<const OpaqueValue> Value::AsOpaque() const& {
21702170
if (const auto* alternative = variant_.As<OpaqueValue>();
21712171
alternative != nullptr) {
21722172
return *alternative;
@@ -2181,7 +2181,7 @@ absl::optional<OpaqueValue> Value::AsOpaque() && {
21812181
return std::nullopt;
21822182
}
21832183

2184-
optional_ref<const OptionalValue> Value::AsOptional() const& {
2184+
absl::optional_ref<const OptionalValue> Value::AsOptional() const& {
21852185
if (const auto* alternative = variant_.As<OpaqueValue>();
21862186
alternative != nullptr && alternative->IsOptional()) {
21872187
return static_cast<const OptionalValue&>(*alternative);
@@ -2197,7 +2197,7 @@ absl::optional<OptionalValue> Value::AsOptional() && {
21972197
return std::nullopt;
21982198
}
21992199

2200-
optional_ref<const ParsedJsonListValue> Value::AsParsedJsonList() const& {
2200+
absl::optional_ref<const ParsedJsonListValue> Value::AsParsedJsonList() const& {
22012201
if (const auto* alternative = variant_.As<ParsedJsonListValue>();
22022202
alternative != nullptr) {
22032203
return *alternative;
@@ -2213,7 +2213,7 @@ absl::optional<ParsedJsonListValue> Value::AsParsedJsonList() && {
22132213
return std::nullopt;
22142214
}
22152215

2216-
optional_ref<const ParsedJsonMapValue> Value::AsParsedJsonMap() const& {
2216+
absl::optional_ref<const ParsedJsonMapValue> Value::AsParsedJsonMap() const& {
22172217
if (const auto* alternative = variant_.As<ParsedJsonMapValue>();
22182218
alternative != nullptr) {
22192219
return *alternative;
@@ -2229,7 +2229,7 @@ absl::optional<ParsedJsonMapValue> Value::AsParsedJsonMap() && {
22292229
return std::nullopt;
22302230
}
22312231

2232-
optional_ref<const CustomListValue> Value::AsCustomList() const& {
2232+
absl::optional_ref<const CustomListValue> Value::AsCustomList() const& {
22332233
if (const auto* alternative = variant_.As<CustomListValue>();
22342234
alternative != nullptr) {
22352235
return *alternative;
@@ -2245,7 +2245,7 @@ absl::optional<CustomListValue> Value::AsCustomList() && {
22452245
return std::nullopt;
22462246
}
22472247

2248-
optional_ref<const CustomMapValue> Value::AsCustomMap() const& {
2248+
absl::optional_ref<const CustomMapValue> Value::AsCustomMap() const& {
22492249
if (const auto* alternative = variant_.As<CustomMapValue>();
22502250
alternative != nullptr) {
22512251
return *alternative;
@@ -2261,7 +2261,7 @@ absl::optional<CustomMapValue> Value::AsCustomMap() && {
22612261
return std::nullopt;
22622262
}
22632263

2264-
optional_ref<const ParsedMapFieldValue> Value::AsParsedMapField() const& {
2264+
absl::optional_ref<const ParsedMapFieldValue> Value::AsParsedMapField() const& {
22652265
if (const auto* alternative = variant_.As<ParsedMapFieldValue>();
22662266
alternative != nullptr) {
22672267
return *alternative;
@@ -2277,7 +2277,7 @@ absl::optional<ParsedMapFieldValue> Value::AsParsedMapField() && {
22772277
return std::nullopt;
22782278
}
22792279

2280-
optional_ref<const ParsedMessageValue> Value::AsParsedMessage() const& {
2280+
absl::optional_ref<const ParsedMessageValue> Value::AsParsedMessage() const& {
22812281
if (const auto* alternative = variant_.As<ParsedMessageValue>();
22822282
alternative != nullptr) {
22832283
return *alternative;
@@ -2293,8 +2293,8 @@ absl::optional<ParsedMessageValue> Value::AsParsedMessage() && {
22932293
return std::nullopt;
22942294
}
22952295

2296-
optional_ref<const ParsedRepeatedFieldValue> Value::AsParsedRepeatedField()
2297-
const& {
2296+
absl::optional_ref<const ParsedRepeatedFieldValue>
2297+
Value::AsParsedRepeatedField() const& {
22982298
if (const auto* alternative = variant_.As<ParsedRepeatedFieldValue>();
22992299
alternative != nullptr) {
23002300
return *alternative;
@@ -2310,7 +2310,7 @@ absl::optional<ParsedRepeatedFieldValue> Value::AsParsedRepeatedField() && {
23102310
return std::nullopt;
23112311
}
23122312

2313-
optional_ref<const CustomStructValue> Value::AsCustomStruct() const& {
2313+
absl::optional_ref<const CustomStructValue> Value::AsCustomStruct() const& {
23142314
if (const auto* alternative = variant_.As<CustomStructValue>();
23152315
alternative != nullptr) {
23162316
return *alternative;
@@ -2326,7 +2326,7 @@ absl::optional<CustomStructValue> Value::AsCustomStruct() && {
23262326
return std::nullopt;
23272327
}
23282328

2329-
optional_ref<const StringValue> Value::AsString() const& {
2329+
absl::optional_ref<const StringValue> Value::AsString() const& {
23302330
if (const auto* alternative = variant_.As<StringValue>();
23312331
alternative != nullptr) {
23322332
return *alternative;
@@ -2382,7 +2382,7 @@ absl::optional<TimestampValue> Value::AsTimestamp() const {
23822382
return std::nullopt;
23832383
}
23842384

2385-
optional_ref<const TypeValue> Value::AsType() const& {
2385+
absl::optional_ref<const TypeValue> Value::AsType() const& {
23862386
if (const auto* alternative = variant_.As<TypeValue>();
23872387
alternative != nullptr) {
23882388
return *alternative;
@@ -2405,7 +2405,7 @@ absl::optional<UintValue> Value::AsUint() const {
24052405
return std::nullopt;
24062406
}
24072407

2408-
optional_ref<const UnknownValue> Value::AsUnknown() const& {
2408+
absl::optional_ref<const UnknownValue> Value::AsUnknown() const& {
24092409
if (const auto* alternative = variant_.As<UnknownValue>();
24102410
alternative != nullptr) {
24112411
return *alternative;

0 commit comments

Comments
 (0)