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 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>>>
79 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>
101 : value_(std::forward<Args>(args)...) {}
104template <
class U,
class... Args>
105unexpected<E>::unexpected(std::initializer_list<U> il, Args&&... args)
106 : value_(il, std::forward<Args>(args)...) {}
114const E&
unexpected<E>::error()
const noexcept {
118template <
class S,
class E>
119expected<S, E>::expected(
const S& success) : data_(success) {}
121template <
class S,
class E>
122expected<S, E>::expected(S&& success) : data_(std::forward<S>(success)) {}
124template <
class S,
class E>
127template <
class S,
class E>
129 : data_(std::forward<
unexpected<E>>(error.error())) {}
131template <
class S,
class E>
132template <
class G,
typename>
134 : data_(utils::
unexpected<E>(std::forward<G>(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>
143 return std::holds_alternative<S>(data_);
146template <
class S,
class E>
148 S* result = std::get_if<S>(&data_);
149 if (result ==
nullptr) {
150 throw bad_expected_access(
151 "Trying to get undefined value from utils::expected");
156template <
class S,
class E>
158 return std::move(value());
161template <
class S,
class E>
162const S&
expected<S, E>::value()
const& {
163 const S* result = std::get_if<S>(&data_);
164 if (result ==
nullptr) {
165 throw bad_expected_access(
166 "Trying to get undefined value from utils::expected");
171template <
class S,
class E>
173 auto* result = std::get_if<
unexpected<E>>(&data_);
174 if (result ==
nullptr) {
175 throw bad_expected_access(
176 "Trying to get undefined error value from utils::expected");
178 return result->error();
181template <
class S,
class E>
182const E&
expected<S, E>::error()
const {
183 const auto* result = std::get_if<
unexpected<E>>(&data_);
184 if (result ==
nullptr) {
185 throw bad_expected_access(
186 "Trying to get undefined error value from utils::expected");
188 return result->error();