Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
expected.h
Go to the documentation of this file.
1#pragma once
2
16
17#include <sonde/global.h>
18
19#include <QtCore/QString>
20
21#include <optional>
22#include <type_traits>
23#include <utility>
24#include <variant>
25
26namespace Sonde {
27
29enum class ErrorKind {
35};
36
46
48inline bool operator==(const Error &a, const Error &b)
49{
50 return a.kind == b.kind && a.message == b.message;
51}
52
53inline bool operator!=(const Error &a, const Error &b)
54{
55 return !(a == b);
56}
57
59template <typename E>
61{
62public:
64 explicit Unexpected(E error) : m_error(std::move(error)) { }
65
67 const E &error() const & noexcept { return m_error; }
69 E &error() & noexcept { return m_error; }
71 E &&error() && noexcept { return std::move(m_error); }
72
73private:
75};
76
78template <typename E>
80
82inline Unexpected<Error> failure(QString message)
83{
84 return Unexpected(Error{ std::move(message), ErrorKind::Failed });
85}
86
92
93namespace Detail {
96[[noreturn]] SONDE_CORE_EXPORT void badExpectedAccess(const char *what, const QString &problem);
97} // namespace Detail
98
104template <typename T, typename E = Error>
106{
107 static_assert(!std::is_reference_v<T> && !std::is_void_v<T>,
108 "Expected<T&> doesn't exist; Expected<void> is specialised");
109
110public:
111 using value_type = T;
112 using error_type = E;
113
115 Expected() : m_v(std::in_place_index<0>) { }
117 // NOLINTNEXTLINE(cppcoreguidelines-explicit-constructor,misc-explicit-constructor): implicit, as std::expected's, so a function can return a value or failure()
118 Expected(const T &value) : m_v(std::in_place_index<0>, value) { }
120 // NOLINTNEXTLINE(cppcoreguidelines-explicit-constructor,misc-explicit-constructor): implicit, as std::expected's, so a function can return a value or failure()
121 Expected(T &&value) : m_v(std::in_place_index<0>, std::move(value)) { }
123 template <typename G>
124 // NOLINTNEXTLINE(cppcoreguidelines-explicit-constructor,misc-explicit-constructor): implicit, as std::expected's, so a function can return a value or failure()
125 Expected(const Unexpected<G> &error) : m_v(std::in_place_index<1>, error.error())
126 {
127 }
128
129 template <typename G>
130 // NOLINTNEXTLINE(cppcoreguidelines-explicit-constructor,misc-explicit-constructor): implicit, as std::expected's, so a function can return a value or failure()
131 Expected(Unexpected<G> &&error) : m_v(std::in_place_index<1>, std::move(error).error())
132 {
133 }
134
136 bool has_value() const noexcept { return m_v.index() == 0; }
138 explicit operator bool() const noexcept { return has_value(); }
139
141 const T &value() const &
142 {
143 check();
144 return std::get<0>(m_v);
145 }
146
147 T &value() &
148 {
149 check();
150 return std::get<0>(m_v);
151 }
152
153 T &&value() &&
154 {
155 check();
156 return std::get<0>(std::move(m_v));
157 }
158
160 const E &error() const &
161 {
162 Q_ASSERT_X(!has_value(), "Sonde::Expected::error()", "it holds a value");
163 return *std::get_if<1>(&m_v);
164 }
165
166 E &error() &
167 {
168 Q_ASSERT_X(!has_value(), "Sonde::Expected::error()", "it holds a value");
169 return *std::get_if<1>(&m_v);
170 }
171
172 E &&error() &&
173 {
174 Q_ASSERT_X(!has_value(), "Sonde::Expected::error()", "it holds a value");
175 return std::move(*std::get_if<1>(&m_v));
176 }
177
179 const T &operator*() const & { return *valuePointer(); }
181 T &operator*() & { return *valuePointer(); }
183 T &&operator*() && { return std::move(*valuePointer()); }
185 const T *operator->() const { return valuePointer(); }
187 T *operator->() { return valuePointer(); }
188
190 template <typename U>
191 T value_or(U &&fallback) const &
192 {
193 return has_value() ? std::get<0>(m_v) : static_cast<T>(std::forward<U>(fallback));
194 }
195
196 template <typename U>
197 T value_or(U &&fallback) &&
198 {
199 return has_value() ? std::get<0>(std::move(m_v))
200 : static_cast<T>(std::forward<U>(fallback));
201 }
202
203private:
205 void check() const
206 {
207 if (!has_value()) {
208 Detail::badExpectedAccess("Sonde::Expected::value()",
209 QStringLiteral("called on an error: ")
210 + messageOf(*std::get_if<1>(&m_v)));
211 }
212 }
213
214 const T *valuePointer() const
215 {
216 Q_ASSERT_X(has_value(), "Sonde::Expected::operator*", "it holds an error");
217 return std::get_if<0>(&m_v);
218 }
219
221 {
222 Q_ASSERT_X(has_value(), "Sonde::Expected::operator*", "it holds an error");
223 return std::get_if<0>(&m_v);
224 }
225
226 template <typename G>
227 static QString messageOf(const G &error)
228 {
229 if constexpr (std::is_same_v<G, Error>) {
230 return error.message;
231 } else {
232 Q_UNUSED(error);
233 return { };
234 }
235 }
236
237 std::variant<T, E> m_v;
238};
239
241template <typename E>
242class Expected<void, E>
243{
244public:
245 using value_type = void;
246 using error_type = E;
247
249 Expected() = default;
251 template <typename G>
252 // NOLINTNEXTLINE(cppcoreguidelines-explicit-constructor,misc-explicit-constructor): implicit, as std::expected's, so a function can return a value or failure()
254 {
255 }
256
257 template <typename G>
258 // NOLINTNEXTLINE(cppcoreguidelines-explicit-constructor,misc-explicit-constructor): implicit, as std::expected's, so a function can return a value or failure()
260 {
261 }
262
264 bool has_value() const noexcept { return !m_error.has_value(); }
266 explicit operator bool() const noexcept { return has_value(); }
267
269 void value() const
270 {
271 if (m_error) {
272 if constexpr (std::is_same_v<E, Error>) {
273 Detail::badExpectedAccess("Sonde::Expected<void>::value()",
274 QStringLiteral("called on an error: ")
275 + m_error->message);
276 } else {
277 Detail::badExpectedAccess("Sonde::Expected<void>::value()",
278 QStringLiteral("called on an error"));
279 }
280 }
281 }
282
283 void operator*() const noexcept
284 {
285 Q_ASSERT_X(has_value(), "Sonde::Expected<void>::operator*", "it holds an error");
286 }
287
289 const E &error() const &
290 {
291 if (!m_error) { // asking success for its error is a bug
292 Detail::badExpectedAccess("Sonde::Expected<void>::error()",
293 QStringLiteral("called on success"));
294 }
295 return *m_error;
296 }
297
298 E &error() &
299 {
300 if (!m_error) { // asking success for its error is a bug
301 Detail::badExpectedAccess("Sonde::Expected<void>::error()",
302 QStringLiteral("called on success"));
303 }
304 return *m_error;
305 }
306
307 E &&error() &&
308 {
309 if (!m_error) { // asking success for its error is a bug
310 Detail::badExpectedAccess("Sonde::Expected<void>::error()",
311 QStringLiteral("called on success"));
312 }
313 return std::move(*m_error);
314 }
315
316private:
317 std::optional<E> m_error;
318};
319
320namespace Detail {
322template <typename X>
323struct IsExpected : std::false_type
324{
325};
326
327template <typename T, typename E>
328struct IsExpected<Expected<T, E>> : std::true_type
329{
330};
331
332template <typename X>
333struct IsUnexpected : std::false_type
334{
335};
336
337template <typename E>
338struct IsUnexpected<Unexpected<E>> : std::true_type
339{
340};
341
343template <typename U>
345} // namespace Detail
346
347// The comparisons std::expected has: with another Expected, with an Unexpected, and with
348// a value.
349
351template <typename T, typename E, typename T2, typename E2>
353{
354 if (a.has_value() != b.has_value())
355 return false;
356 if constexpr (std::is_void_v<T> && std::is_void_v<T2>) {
357 return a.has_value() || a.error() == b.error();
358 } else {
359 return a.has_value() ? *a == *b : a.error() == b.error();
360 }
361}
362
363template <typename T, typename E, typename T2, typename E2>
365{
366 return !(a == b);
367}
368
370template <typename T, typename E, typename G>
372{
373 return !e.has_value() && e.error() == u.error();
374}
375
376template <typename T, typename E, typename G>
378{
379 return e == u;
380}
381
382template <typename T, typename E, typename G>
384{
385 return !(e == u);
386}
387
388template <typename T, typename E, typename G>
390{
391 return !(e == u);
392}
393
395template <typename T, typename E, typename U,
396 std::enable_if_t<Detail::isPlainValue<U> && !std::is_void_v<T>, int> = 0>
397bool operator==(const Expected<T, E> &e, const U &value)
398{
399 return e.has_value() && *e == value;
400}
401
402template <typename T, typename E, typename U,
403 std::enable_if_t<Detail::isPlainValue<U> && !std::is_void_v<T>, int> = 0>
404bool operator==(const U &value, const Expected<T, E> &e)
405{
406 return e == value;
407}
408
409template <typename T, typename E, typename U,
410 std::enable_if_t<Detail::isPlainValue<U> && !std::is_void_v<T>, int> = 0>
411bool operator!=(const Expected<T, E> &e, const U &value)
412{
413 return !(e == value);
414}
415
416template <typename T, typename E, typename U,
417 std::enable_if_t<Detail::isPlainValue<U> && !std::is_void_v<T>, int> = 0>
418bool operator!=(const U &value, const Expected<T, E> &e)
419{
420 return !(e == value);
421}
422
423} // namespace Sonde
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