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>>>
74 const S& value()
const;
81 const E& error()
const;
88unexpected<E>::unexpected(
const E& error) : value_{error} {}
91unexpected<E>::unexpected(E&& error) : value_{std::forward<E>(error)} {}
94template <
class... Args>
96 : value_(std::forward<Args>(args)...) {}
99template <
class U,
class... Args>
100unexpected<E>::unexpected(std::initializer_list<U> il, Args&&... args)
101 : value_(il, std::forward<Args>(args)...) {}
109const E&
unexpected<E>::error()
const noexcept {
113template <
class S,
class E>
114expected<S, E>::expected(
const S& success) : data_(success) {}
116template <
class S,
class E>
117expected<S, E>::expected(S&& success) : data_(std::forward<S>(success)) {}
119template <
class S,
class E>
122template <
class S,
class E>
124 : data_(std::forward<
unexpected<E>>(error.error())) {}
126template <
class S,
class E>
127template <
class G,
typename>
131template <
class S,
class E>
132template <
class G,
typename>
136template <
class S,
class E>
138 return std::holds_alternative<S>(data_);
141template <
class S,
class E>
143 S* result = std::get_if<S>(&data_);
144 if (result ==
nullptr) {
145 throw bad_expected_access(
146 "Trying to get undefined value from utils::expected");
151template <
class S,
class E>
152const S&
expected<S, E>::value()
const {
153 const 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>
164 if (result ==
nullptr) {
165 throw bad_expected_access(
166 "Trying to get undefined error value from utils::expected");
168 return result->error();
171template <
class S,
class E>
172const E&
expected<S, E>::error()
const {
174 if (result ==
nullptr) {
175 throw bad_expected_access(
176 "Trying to get undefined error value from utils::expected");
178 return result->error();