10USERVER_NAMESPACE_BEGIN
16 using std::exception::exception;
18 explicit bad_expected_access(
const std::string& message)
19 : message_{message} {}
21 const char* what()
const noexcept override {
return message_.c_str(); }
30 unexpected(
const E& error);
31 unexpected(E&& error);
33 template <
class... Args>
34 unexpected(Args&&... args);
36 template <
class U,
class... Args>
37 unexpected(std::initializer_list<U> il, Args&&... args);
40 const E& error()
const noexcept;
52template <
class S,
class E>
55 constexpr expected()
noexcept(std::is_void_v<S>);
56 expected(
const S& success);
57 expected(S&& success);
61 template <
class G,
typename = std::enable_if_t<std::is_convertible_v<G, E>>>
64 template <
class G,
typename = std::enable_if_t<std::is_convertible_v<G, E>>>
79 const S&
value()
const&;
87 const E&
error()
const;
94unexpected<E>::unexpected(
const E& error) : value_{error} {}
97unexpected<E>::unexpected(E&& error) : value_{std::forward<E>(error)} {}
100template <
class... Args>
102 : value_(std::forward<Args>(args)...) {}
105template <
class U,
class... Args>
106unexpected<E>::unexpected(std::initializer_list<U> il, Args&&... args)
107 : value_(il, std::forward<Args>(args)...) {}
115const E&
unexpected<E>::error()
const noexcept {
119template <
class S,
class E>
120constexpr expected<S, E>::expected()
noexcept(std::is_void_v<S>)
121 : data_(std::in_place_index<0>) {}
123template <
class S,
class E>
124expected<S, E>::expected(
const S& success) : data_(success) {}
126template <
class S,
class E>
127expected<S, E>::expected(S&& success) : data_(std::forward<S>(success)) {}
129template <
class S,
class E>
132template <
class S,
class E>
134 : data_(std::forward<
unexpected<E>>(error.error())) {}
136template <
class S,
class E>
137template <
class G,
typename>
139 : data_(utils::
unexpected<E>(std::forward<G>(error.error()))) {}
141template <
class S,
class E>
142template <
class G,
typename>
144 : data_(utils::
unexpected<E>(std::forward<G>(error.error()))) {}
146template <
class S,
class E>
148 return std::holds_alternative<S>(data_);
151template <
class S,
class E>
153 S* result = std::get_if<S>(&data_);
154 if (result ==
nullptr) {
155 throw bad_expected_access(
156 "Trying to get undefined value from utils::expected");
161template <
class S,
class E>
163 return std::move(value());
166template <
class S,
class E>
168 const S* result = std::get_if<S>(&data_);
169 if (result ==
nullptr) {
170 throw bad_expected_access(
171 "Trying to get undefined value from utils::expected");
176template <
class S,
class E>
178 auto* result = std::get_if<
unexpected<E>>(&data_);
179 if (result ==
nullptr) {
180 throw bad_expected_access(
181 "Trying to get undefined error value from utils::expected");
183 return result->error();
186template <
class S,
class E>
188 const auto* result = std::get_if<
unexpected<E>>(&data_);
189 if (result ==
nullptr) {
190 throw bad_expected_access(
191 "Trying to get undefined error value from utils::expected");
193 return result->error();