19#include <QtCore/QString>
96[[noreturn]] SONDE_CORE_EXPORT
void badExpectedAccess(
const char *what,
const QString &problem);
104template <
typename T,
typename E = Error>
107 static_assert(!std::is_reference_v<T> && !std::is_void_v<T>,
108 "Expected<T&> doesn't exist; Expected<void> is specialised");
123 template <
typename G>
129 template <
typename G>
138 explicit operator bool() const noexcept {
return has_value(); }
144 return std::get<0>(
m_v);
150 return std::get<0>(
m_v);
156 return std::get<0>(std::move(
m_v));
162 Q_ASSERT_X(!
has_value(),
"Sonde::Expected::error()",
"it holds a value");
163 return *std::get_if<1>(&
m_v);
168 Q_ASSERT_X(!
has_value(),
"Sonde::Expected::error()",
"it holds a value");
169 return *std::get_if<1>(&
m_v);
174 Q_ASSERT_X(!
has_value(),
"Sonde::Expected::error()",
"it holds a value");
175 return std::move(*std::get_if<1>(&
m_v));
190 template <
typename U>
193 return has_value() ? std::get<0>(
m_v) :
static_cast<T
>(std::forward<U>(fallback));
196 template <
typename U>
200 :
static_cast<T
>(std::forward<U>(fallback));
209 QStringLiteral(
"called on an error: ")
216 Q_ASSERT_X(
has_value(),
"Sonde::Expected::operator*",
"it holds an error");
217 return std::get_if<0>(&
m_v);
222 Q_ASSERT_X(
has_value(),
"Sonde::Expected::operator*",
"it holds an error");
223 return std::get_if<0>(&
m_v);
226 template <
typename G>
229 if constexpr (std::is_same_v<G, Error>) {
230 return error.message;
251 template <
typename G>
257 template <
typename G>
266 explicit operator bool() const noexcept {
return has_value(); }
272 if constexpr (std::is_same_v<E, Error>) {
274 QStringLiteral(
"called on an error: ")
278 QStringLiteral(
"called on an error"));
285 Q_ASSERT_X(
has_value(),
"Sonde::Expected<void>::operator*",
"it holds an error");
293 QStringLiteral(
"called on success"));
302 QStringLiteral(
"called on success"));
311 QStringLiteral(
"called on success"));
327template <
typename T,
typename E>
351template <
typename T,
typename E,
typename T2,
typename E2>
356 if constexpr (std::is_void_v<T> && std::is_void_v<T2>) {
363template <
typename T,
typename E,
typename T2,
typename E2>
370template <
typename T,
typename E,
typename G>
376template <
typename T,
typename E,
typename G>
382template <
typename T,
typename E,
typename G>
388template <
typename T,
typename E,
typename G>
395template <
typename T,
typename E,
typename U,
396 std::enable_if_t<Detail::isPlainValue<U> && !std::is_void_v<T>,
int> = 0>
402template <
typename T,
typename E,
typename U,
403 std::enable_if_t<Detail::isPlainValue<U> && !std::is_void_v<T>,
int> = 0>
409template <
typename T,
typename E,
typename U,
410 std::enable_if_t<Detail::isPlainValue<U> && !std::is_void_v<T>,
int> = 0>
413 return !(e == value);
416template <
typename T,
typename E,
typename U,
417 std::enable_if_t<Detail::isPlainValue<U> && !std::is_void_v<T>,
int> = 0>
420 return !(e == value);
E & error() &
The error; it must hold one (stops the program if it succeeded).
Definition expected.h:298
void value_type
There is no value.
Definition expected.h:245
void operator*() const noexcept
Nothing; it must have succeeded.
Definition expected.h:283
Expected()=default
Success.
const E & error() const &
The error; it must hold one (stops the program if it succeeded).
Definition expected.h:289
E && error() &&
The error, moved out; it must hold one (stops the program if it succeeded).
Definition expected.h:307
void value() const
Nothing; stops the program if it holds an error.
Definition expected.h:269
Expected(const Unexpected< G > &error)
Holds error's error.
Definition expected.h:253
std::optional< E > m_error
The error, if it failed.
Definition expected.h:317
bool has_value() const noexcept
Whether it succeeded.
Definition expected.h:264
E error_type
The error's type.
Definition expected.h:246
Expected(Unexpected< G > &&error)
Holds error's error.
Definition expected.h:259
A value of type T, or an error of type E, as std::expected.
Definition expected.h:106
E error_type
The error's type.
Definition expected.h:112
T && operator*() &&
The value, moved out; it must hold one.
Definition expected.h:183
bool has_value() const noexcept
Whether it holds a value.
Definition expected.h:136
std::variant< T, E > m_v
The value (index 0) or the error (index 1).
Definition expected.h:237
const T & operator*() const &
The value; it must hold one.
Definition expected.h:179
Expected(T &&value)
Holds value.
Definition expected.h:121
T value_or(U &&fallback) const &
The value, or fallback if it holds an error.
Definition expected.h:191
Expected(const T &value)
Holds value.
Definition expected.h:118
E & error() &
The error; it must hold one.
Definition expected.h:166
T value_or(U &&fallback) &&
The value moved out, or fallback if it holds an error.
Definition expected.h:197
void check() const
Stops the program if it holds an error.
Definition expected.h:205
T * operator->()
The value's members; it must hold one.
Definition expected.h:187
T & operator*() &
The value; it must hold one.
Definition expected.h:181
Expected(Unexpected< G > &&error)
Holds error's error.
Definition expected.h:131
T * valuePointer()
The value, asserting that there is one.
Definition expected.h:220
Expected()
A default-constructed value.
Definition expected.h:115
E && error() &&
The error, moved out; it must hold one.
Definition expected.h:172
T & value() &
The value; stops the program if it holds an error.
Definition expected.h:147
const T * operator->() const
The value's members; it must hold one.
Definition expected.h:185
const T * valuePointer() const
The value, asserting that there is one.
Definition expected.h:214
static QString messageOf(const G &error)
The message of an Error, for badExpectedAccess(); other error types have none.
Definition expected.h:227
const T & value() const &
The value; stops the program if it holds an error.
Definition expected.h:141
Expected(const Unexpected< G > &error)
Holds error's error.
Definition expected.h:125
T && value() &&
The value, moved out; stops the program if it holds an error.
Definition expected.h:153
T value_type
The value's type.
Definition expected.h:111
const E & error() const &
The error; it must hold one.
Definition expected.h:160
An error, to construct an Expected from, as std::unexpected.
Definition expected.h:61
Unexpected(E error)
Wraps error.
Definition expected.h:64
E && error() &&noexcept
The error, moved out.
Definition expected.h:71
const E & error() const &noexcept
The error.
Definition expected.h:67
E & error() &noexcept
The error.
Definition expected.h:69
E m_error
The error.
Definition expected.h:74
constexpr bool isPlainValue
Whether U is a plain value to compare an Expected's value with: not an Expected or an Unexpected,...
Definition expected.h:344
void badExpectedAccess(const char *what, const QString &problem)
Stops the program: value() or error() was asked of the wrong alternative.
Definition global.cpp:16
What every Sonde header includes: the version, and the deprecation macros (design 0031).
Embeddable Model Context Protocol server for Qt applications.
Definition dispatch.cpp:10
Unexpected< Error > failure(QString message)
A failure with message, to return as an Expected<T>.
Definition expected.h:82
bool operator==(const Error &a, const Error &b)
Whether a and b are the same error: the same kind and message.
Definition expected.h:48
bool operator!=(const Error &a, const Error &b)
Whether a and b are different errors.
Definition expected.h:53
Unexpected< Error > notHandled()
"Not handled, ask the next integration", for GuiIntegration hooks to return.
Definition expected.h:88
ErrorKind
What kind of error an Error is.
Definition expected.h:29
@ NotHandled
A GuiIntegration hook doesn't handle this object: ask the next integration.
Definition expected.h:34
@ Failed
It failed; Error::message says why.
Definition expected.h:31
Whether X is an Expected.
Definition expected.h:324
Whether X is an Unexpected.
Definition expected.h:334
Why something failed.
Definition expected.h:42
ErrorKind kind
What kind of error it is.
Definition expected.h:44
QString message
What went wrong, in a sentence for a person (or a model) to read.
Definition expected.h:43