HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
parse_expected.hpp
1
2
3#pragma once
4
5#include "../macros.hpp"
6#include <expected>
7#include <optional>
8#include <variant>
9
10hi_export_module(hikogui.parser : parse_expected);
11
12hi_export namespace hi {
13inline namespace v1 {
14
15template<typename T, typename E>
17public:
18 using value_type = T;
19 using error_type = E;
20
21 constexpr parse_expected(parse_expected const&) noexcept = default;
22 constexpr parse_expected(parse_expected&&) noexcept = default;
23 constexpr parse_expected& operator=(parse_expected const&) noexcept = default;
24 constexpr parse_expected& operator=(parse_expected&&) noexcept = default;
25
26 constexpr parse_expected() noexcept = default;
27
28 constexpr parse_expected(std::nullopt_t) noexcept _v{std::monostate{}} {}
29
30 template<typename Arg = T>
31 constexpr explicit(not std::is_convertible_v<Arg, T>) parse_expected(Arg&& arg) noexcept : _v{std::forward<Arg>(arg)}
32 {
33 }
34
35 template<typename Arg>
36 constexpr explicit(not std::is_convertible_v<const Arg&, E>) parse_expected(std::unexpected<Arg> const& arg) noexcept :
37 _v{arg}
38 {
39 }
40
41 constexpr parse_expected &operator=(std::nullopt_t) noexcept
42 {
43 _v.template emplace<0>();
44 return *this;
45 }
46
47 template<typename Arg = T>
48 constexpr parse_expected &operator=(Arg &&arg) noexcept
49 {
50 _v.template emplace<1>(std::forward<Arg>(arg));
51 return *this;
52 }
53
54 template<typename Arg = E>
55 constexpr parse_expected &operator=(std::unexpected<Arg> const &arg) noexcept
56 {
57 _v.template emplace<2>(arg);
58 return *this;
59 }
60
61 template<typename Arg = E>
62 constexpr parse_expected &operator=(std::unexpected<Arg> &&arg) noexcept
63 {
64 _v.template emplace<2>(std::move(arg));
65 return *this;
66 }
67
68 template<typename... Args>
69 constexpr value_type &emplace(Args &&... args) noexcept
70 {
71 return _v.template emplace<1>(std::forward<Args>(args)...);
72 }
73
74 [[nodiscard]] constexpr bool has_value() const noexcept
75 {
76 return _v.index() == 1;
77 }
78
79 constexpr explicit operator bool() const noexcept
80 {
81 return has_value();
82 }
83
84 [[nodiscard]] constexpr bool has_error() const noexcept
85 {
86 return _v.index() == 2;
87 }
88
89 [[nodiscard]] constexpr value_type const& operator*() const noexcept
90 {
91 hi_axiom(_v.index() == 1);
92 return std::get<1>(_v);
93 }
94
95 [[nodiscard]] constexpr value_type& operator*() noexcept
96 {
97 hi_axiom(_v.index() == 1);
98 return std::get<1>(_v);
99 }
100
101 [[nodiscard]] constexpr value_type const* operator->() const noexcept
102 {
103 hi_axiom(_v.index() == 1);
104 return std::addressof(::get<1>(_v));
105 }
106
107 [[nodiscard]] constexpr value_type* operator->() noexcept
108 {
109 hi_axiom(_v.index() == 1);
110 return std::addressof(std::get<1>(_v));
111 }
112
113 [[nodiscard]] constexpr error_type const& error() const noexcept
114 {
115 hi_axiom(_v.index() == 2);
116 return std::get<2>(_v);
117 }
118
119 [[nodiscard]] constexpr error_type& error() noexcept
120 {
121 hi_axiom(_v.index() == 2);
122 return std::get<2>(_v);
123 }
124
125private:
126 std::variant<std::monostate, T, E> _v;
127};
128
129} // namespace v1
130}
The HikoGUI namespace.
Definition array_generic.hpp:20
constexpr matrix2 operator*(matrix2 const &lhs, matrix2 const &rhs) noexcept
Matrix/Matrix multiplication.
Definition transform.hpp:69
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition parse_expected.hpp:16
T addressof(T... args)
T move(T... args)