userver: userver/utils/optional_ref.hpp Source File
Loading...
Searching...
No Matches
optional_ref.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/optional_ref.hpp
4/// @brief @copybrief utils::OptionalRef
5
6#include <functional>
7#include <memory>
8#include <optional>
9#include <type_traits>
10#include <utility>
11
12#include <userver/utils/assert.hpp>
13#include <userver/utils/meta_light.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace utils {
18
19/// @ingroup userver_universal userver_containers
20///
21/// @brief Class that behaves as a nullable reference. Main difference from the
22/// pointer - value comparison of pointed values.
23///
24/// Initializes from reference to a T or from optional<T>.
25///
26/// Once the reference is constructed it can not be changed to point to
27/// different address.
28template <class T>
30public:
31 using value_type = T;
32
33 static_assert(!std::is_reference<T>::value, "Do not use a reference for T");
34
35 constexpr OptionalRef() noexcept = default;
36 constexpr OptionalRef(std::nullopt_t) noexcept {}
37 constexpr OptionalRef(const OptionalRef&) noexcept = default;
38 constexpr OptionalRef& operator=(const OptionalRef&) noexcept = delete;
39
40 constexpr OptionalRef(T& other) noexcept : data_(std::addressof(other)) {}
41
42 // Forming a reference to a temporary is forbidden
43 constexpr explicit OptionalRef(const T&&) = delete;
44
45 template <typename U>
46 constexpr explicit OptionalRef(const std::optional<U>& other) noexcept : data_(GetPointer(other)) {}
47
48 template <typename U>
49 constexpr explicit OptionalRef(std::optional<U>& other) noexcept : data_(GetPointer(other)) {}
50
51 template <typename U>
52 constexpr explicit OptionalRef(const std::optional<U>&&) noexcept {
53 static_assert(!sizeof(U), "Forming a reference to a temporary");
54 }
55
56 constexpr bool has_value() const noexcept { return !!data_; }
57 constexpr explicit operator bool() const noexcept { return has_value(); }
58
59 constexpr T* operator->() const {
60 UASSERT(data_);
61 return data_;
62 }
63
64 constexpr T& operator*() const {
65 UASSERT(data_);
66 return *data_;
67 }
68
69 constexpr T& value() const {
70 if (!has_value()) {
71 throw std::bad_optional_access();
72 }
73
74 return *data_;
75 }
76
77 template <typename U>
78 constexpr T value_or(U&& default_value) const {
79 if (!has_value()) {
80 return std::forward<U>(default_value);
81 }
82
83 return *data_;
84 }
85
86 template <typename F>
87 constexpr auto and_then(F&& function) const -> std::remove_cvref_t<std::invoke_result_t<F, T&>> {
88 using Result = std::remove_cvref_t<std::invoke_result_t<F, T&>>;
89 static_assert(
90 meta::IsInstantiationOf<Result, std::optional> || meta::IsInstantiationOf<Result, OptionalRef>,
91 "The function passed to and_then must return std::optional or utils::OptionalRef"
92 );
93
94 if (has_value()) {
95 return std::invoke(std::forward<F>(function), *data_);
96 }
97 return Result{};
98 }
99
100 template <typename F>
101 constexpr auto transform(F&& function) const {
102 using Result = std::remove_cv_t<std::invoke_result_t<F, T&>>;
103
104 if (has_value()) {
105 return std::optional<Result>{std::invoke(std::forward<F>(function), *data_)};
106 }
107 return std::optional<Result>{};
108 }
109
110 template <typename F>
111 constexpr OptionalRef or_else(F&& function) const {
112 using Result = std::remove_cvref_t<std::invoke_result_t<F>>;
113 static_assert(
114 std::is_same_v<Result, OptionalRef>,
115 "The function passed to or_else must return utils::OptionalRef<T>"
116 );
117
118 if (has_value()) {
119 return *this;
120 }
121 return std::invoke(std::forward<F>(function));
122 }
123
124private:
125 template <class Optional>
126 static T* GetPointer(Optional& other) noexcept {
127 using ValueType = decltype(*other);
128 static_assert(
129 std::is_const<T>::value || !std::is_const<ValueType>::value,
130 "Attempt to initialize non-const T from a const optional value"
131 );
132
133 return other.has_value() ? std::addressof(*other) : nullptr;
134 }
135
136 T* const data_ = nullptr;
137};
138
139template <class T, class U>
140constexpr bool operator==(OptionalRef<T> lhs, OptionalRef<U> rhs) noexcept {
141 if (!lhs || !rhs) {
142 return !lhs && !rhs;
143 }
144 return *lhs == *rhs;
145}
146
147} // namespace utils
148
149USERVER_NAMESPACE_END