2020-11-09 08:20:54 +01:00
|
|
|
/*
|
2023-07-01 17:20:51 +02:00
|
|
|
** Copyright (C) 2019-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2020-11-09 08:20:54 +01:00
|
|
|
**
|
|
|
|
** 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__
|
|
|
|
|
2022-04-29 23:56:02 +02:00
|
|
|
#include "thirdparty/expected.hpp"
|
2020-11-09 08:20:54 +01:00
|
|
|
#include "utils/mu-error.hh"
|
|
|
|
|
|
|
|
namespace Mu {
|
|
|
|
/**
|
2022-02-15 21:24:28 +01:00
|
|
|
* A little Rust-envy...a Result is _either_ some value of type T, _or_ a Mu::Error
|
2020-11-09 08:20:54 +01:00
|
|
|
*/
|
|
|
|
template <typename T> using Result = tl::expected<T, Error>;
|
|
|
|
|
2022-02-15 21:24:28 +01:00
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2021-10-20 11:18:15 +02:00
|
|
|
template <typename T>
|
2022-02-18 09:49:56 +01:00
|
|
|
class Result<T>::expected
|
2021-10-20 11:18:15 +02:00
|
|
|
Ok(T&& t)
|
|
|
|
{
|
2022-02-15 21:24:28 +01:00
|
|
|
return std::move(t);
|
2020-11-26 08:26:28 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 21:24:28 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
static inline tl::unexpected<Error>
|
2021-10-20 11:18:15 +02:00
|
|
|
Err(Error&& err)
|
|
|
|
{
|
2022-02-15 21:24:28 +01:00
|
|
|
return tl::unexpected(std::move(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline tl::unexpected<Error>
|
|
|
|
Err(const Error& err)
|
|
|
|
{
|
|
|
|
return tl::unexpected(err);
|
2020-11-26 08:26:28 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:49:45 +02:00
|
|
|
template<typename T>
|
|
|
|
static inline tl::unexpected<Error>
|
|
|
|
Err(const Result<T>& res)
|
|
|
|
{
|
|
|
|
return res.error();
|
|
|
|
}
|
|
|
|
|
2022-02-22 22:01:24 +01:00
|
|
|
/*
|
|
|
|
* convenience
|
|
|
|
*/
|
2023-07-03 19:41:06 +02:00
|
|
|
template <typename ...T>
|
|
|
|
inline tl::unexpected<Error>
|
|
|
|
Err(Error::Code code, fmt::format_string<T...> frm, T&&... args)
|
2022-02-22 22:01:24 +01:00
|
|
|
{
|
2023-07-03 19:41:06 +02:00
|
|
|
return Err(Error{code, frm, std::forward<T>(args)...});
|
2022-02-22 22:01:24 +01:00
|
|
|
}
|
|
|
|
|
2023-07-03 19:41:06 +02:00
|
|
|
template <typename ...T>
|
|
|
|
inline tl::unexpected<Error>
|
|
|
|
Err(Error::Code code, GError **err, fmt::format_string<T...> frm, T&&... args)
|
2022-02-22 22:01:24 +01:00
|
|
|
{
|
2023-07-03 19:41:06 +02:00
|
|
|
return Err(Error{code, err, frm, std::forward<T>(args)...});
|
2022-03-26 15:17:17 +01:00
|
|
|
}
|
|
|
|
|
2022-05-31 22:39:50 +02:00
|
|
|
/**
|
|
|
|
* Assert that some result has a value (for unit tests)
|
|
|
|
*
|
|
|
|
* @param R some result
|
|
|
|
*/
|
2023-07-05 22:10:13 +02:00
|
|
|
#define assert_valid_result(R) do { \
|
|
|
|
if(!R) { \
|
|
|
|
mu_printerrln("{}:{}: error-result: {}", \
|
|
|
|
__FILE__, __LINE__, \
|
|
|
|
(R).error().what()); \
|
|
|
|
g_assert_true(!!R); \
|
|
|
|
} \
|
2022-05-31 22:39:50 +02:00
|
|
|
} while(0)
|
2022-04-18 20:59:12 +02:00
|
|
|
|
2022-02-22 22:01:24 +01:00
|
|
|
}// namespace Mu
|
2022-02-15 21:24:28 +01:00
|
|
|
|
|
|
|
#endif /* MU_RESULT_HH__ */
|