1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
** Copyright (C) 2019-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#ifndef MU_RESULT_HH__
#define MU_RESULT_HH__
#include <tl/expected.hpp>
#include "utils/mu-error.hh"
namespace Mu {
/**
* A little Rust-envy...a Result is _either_ some value of type T, _or_ a Mu::Error
*/
template <typename T> using Result = tl::expected<T, Error>;
/**
* Ok() is not typically strictly needed (unlike Err), but imitates Rust's Ok
* and it helps the reader.
*
* @param t the value to return
*
* @return a success Result<T>
*/
template <typename T> Result<T>
Ok(T&& t)
{
return std::move(t);
}
/**
* Implementation of Ok() for void results.
*
* @return a success Result<void>
*/
static inline Result<void>
Ok()
{
return {};
}
/**
* Return an error
*
* @param err the error
*
* @return error
*/
template<typename T> Result<T>
Err(Error&& err)
{
return tl::unexpected(std::move(err));
}
template<typename T> Result<T>
Err(const Error& err)
{
return tl::unexpected(err);
}
static inline tl::unexpected<Error>
Err(Error&& err)
{
return tl::unexpected(std::move(err));
}
static inline tl::unexpected<Error>
Err(const Error& err)
{
return tl::unexpected(err);
}
template<typename T>
static inline tl::unexpected<Error>
Err(const Result<T>& res)
{
return res.error();
}
template<typename T>
static inline tl::unexpected<Error>
Err(Result<T>&& res)
{
return std::move(res.error());
}
/*
* convenience
*/
template <typename ...T>
tl::unexpected<Error>
Err(Error::Code code, fmt::format_string<T...> frm, T&&... args)
{
return Err(Error{code, frm, std::forward<T>(args)...});
}
template <typename ...T>
tl::unexpected<Error>
Err(Error::Code code, GError **err, fmt::format_string<T...> frm, T&&... args)
{
return Err(Error{code, err, frm, std::forward<T>(args)...});
}
template<typename T> T
unwrap(Result<T>&& res)
{
if (!!res)
return std::move(res.value());
else
throw res.error();
}
/**
* Assert that some result has a value (for unit tests)
*
* @param R some result
*/
#define assert_valid_result(R) do { \
auto&& res__ = R; \
if(!res__) { \
mu_printerrln("{}:{}: error-result: {}", \
__FILE__, __LINE__, \
(res__).error().what()); \
g_assert_true(!!res__); \
} \
} while(0)
}// namespace Mu
#endif /* MU_RESULT_HH__ */
|