TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_result.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <utility>
5#include <variant>
6
7namespace tachyon
8{
11
23
25 struct Error
26 {
28 std::string message;
29 };
30
34 template<class T>
35 class [[nodiscard]] Result
36 {
37 public:
40 Result(T value) : m_value(std::move(value)) {}
41 Result(Error error) : m_value(std::move(error)) {}
43
45 bool HasValue() const { return m_value.index() == 0; }
46 explicit operator bool() const { return HasValue(); }
47
50 T& Value() { return std::get<0>(m_value); }
51 const T& Value() const { return std::get<0>(m_value); }
52 T& operator*() { return Value(); }
53 const T& operator*() const { return Value(); }
54 T* operator->() { return &Value(); }
55 const T* operator->() const { return &Value(); }
58 T ValueOr(T fallback) const { return HasValue() ? Value() : std::move(fallback); }
59
61 const Error& GetError() const { return std::get<1>(m_value); }
62
63 private:
64 std::variant<T, Error> m_value;
65 };
66
68 template<>
69 class [[nodiscard]] Result<void>
70 {
71 public:
74 Result() = default;
75 Result(Error error) : m_error(std::move(error)), m_failed(true) {}
77
79 bool HasValue() const { return !m_failed; }
80 explicit operator bool() const { return HasValue(); }
81
83 const Error& GetError() const { return m_error; }
84
85 private:
86 Error m_error {};
87 bool m_failed = false;
88 };
89
90}
bool HasValue() const
True on success; same as the bool conversion.
Definition gui_result.h:79
Result()=default
Default-constructed is success; implicit from an Error for failure.
Result(Error error)
Default-constructed is success; implicit from an Error for failure.
Definition gui_result.h:75
const Error & GetError() const
The error; a default Error on success.
Definition gui_result.h:83
T ValueOr(T fallback) const
A copy of the value, or fallback when there is none.
Definition gui_result.h:58
bool HasValue() const
True when it holds the value; same as the bool conversion.
Definition gui_result.h:45
T & Value()
The value; only valid when HasValue() (std::get throws otherwise).
Definition gui_result.h:50
const T * operator->() const
The value; only valid when HasValue() (std::get throws otherwise).
Definition gui_result.h:55
const T & operator*() const
The value; only valid when HasValue() (std::get throws otherwise).
Definition gui_result.h:53
const T & Value() const
The value; only valid when HasValue() (std::get throws otherwise).
Definition gui_result.h:51
T * operator->()
The value; only valid when HasValue() (std::get throws otherwise).
Definition gui_result.h:54
T & operator*()
The value; only valid when HasValue() (std::get throws otherwise).
Definition gui_result.h:52
const Error & GetError() const
The error; only valid when !HasValue().
Definition gui_result.h:61
ErrorCode
Why a call failed.
Definition gui_result.h:14
Result(T value)
Implicit from a value or an Error, so functions can return either.
Definition gui_result.h:40
Result(Error error)
Definition gui_result.h:41
@ ParseFailed
a theme, a layout
Definition gui_result.h:18
@ InvalidArgument
Definition gui_result.h:16
@ NotFound
a file, a device, an element
Definition gui_result.h:17
@ Unsupported
the device or backend lacks a feature
Definition gui_result.h:19
@ OutOfMemory
Definition gui_result.h:20
@ Failed
anything else; see the message
Definition gui_result.h:15
@ DeviceLost
the GPU device was lost (the library recreates it)
Definition gui_result.h:21
Definition gui_id.h:91
Definition gui_logging.h:24
A failure: its category and a human-readable message.
Definition gui_result.h:26
ErrorCode code
Definition gui_result.h:27
std::string message
what went wrong, for logs and users
Definition gui_result.h:28