10USERVER_NAMESPACE_BEGIN
16 using std::exception::exception;
18 explicit bad_expected_access(
const std::string& message) : message_{message} {}
20 const char* what()
const noexcept override {
return message_.c_str(); }
29 unexpected(
const E& error);
30 unexpected(E&& error);
32 template <
class... Args>
33 unexpected(Args&&... args);
35 template <
class U,
class... Args>
36 unexpected(std::initializer_list<U> il, Args&&... args);
39 const E& error()
const noexcept;
51template <
class S,
class E>
54 constexpr expected()
noexcept(std::is_void_v<S>);
55 expected(
const S& success);
56 expected(S&& success);
60 template <
class G,
typename = std::enable_if_t<std::is_convertible_v<G, E>>>
63 template <
class G,
typename = std::enable_if_t<std::is_convertible_v<G, E>>>
78 const S&
value()
const&;
86 const E&
error()
const;
93unexpected<E>::unexpected(
const E& error) : value_{error} {}
96unexpected<E>::unexpected(E&& error) : value_{std::forward<E>(error)} {}
99template <
class... Args>
100unexpected<E>::unexpected(Args&&... args) : value_(std::forward<Args>(args)...) {}
103template <
class U,
class... Args>
104unexpected<E>::unexpected(std::initializer_list<U> il, Args&&... args) : value_(il, std::forward<Args>(args)...) {}
112const E&
unexpected<E>::error()
const noexcept {
116template <
class S,
class E>
117constexpr expected<S, E>::expected()
noexcept(std::is_void_v<S>) : data_(std::in_place_index<0>) {}
119template <
class S,
class E>
120expected<S, E>::expected(
const S& success) : data_(success) {}
122template <
class S,
class E>
123expected<S, E>::expected(S&& success) : data_(std::forward<S>(success)) {}
125template <
class S,
class E>
128template <
class S,
class E>
131template <
class S,
class E>
132template <
class G,
typename>
135template <
class S,
class E>
136template <
class G,
typename>
139template <
class S,
class E>
141 return std::holds_alternative<S>(data_);
144template <
class S,
class E>
146 S* result = std::get_if<S>(&data_);
147 if (result ==
nullptr) {
148 throw bad_expected_access(
"Trying to get undefined value from utils::expected");
153template <
class S,
class E>
155 return std::move(value());
158template <
class S,
class E>
160 const S* result = std::get_if<S>(&data_);
161 if (result ==
nullptr) {
162 throw bad_expected_access(
"Trying to get undefined value from utils::expected");
167template <
class S,
class E>
169 auto* result = std::get_if<
unexpected<E>>(&data_);
170 if (result ==
nullptr) {
171 throw bad_expected_access(
"Trying to get undefined error value from utils::expected");
173 return result->error();
176template <
class S,
class E>
178 const auto* result = std::get_if<
unexpected<E>>(&data_);
179 if (result ==
nullptr) {
180 throw bad_expected_access(
"Trying to get undefined error value from utils::expected");
182 return result->error();