5#include <fmt/ostream.h>
7#include <boost/describe.hpp>
8#include <boost/preprocessor.hpp>
11#include <system_error>
21 requires std::is_error_code_enum_v<T>
24 Err(int32_t ec,
const std::error_category& e_cat) :
error_code_(ec, e_cat) {}
55struct fmt::formatter<
wren::Err> : fmt::formatter<std::string> {
56 auto format(
wren::Err, fmt::format_context& ctx)
const ->
decltype(ctx.out());
61#define DEFINE_ERROR_IMPL(CAT_NAME, ERROR_ENUM) \
62 class ERROR_ENUM##_category_t : public std::error_category { \
64 [[nodiscard]] auto name() const noexcept -> const char* final { \
67 [[nodiscard]] auto message(int32_t c) const -> std::string final { \
68 auto e = static_cast<ERROR_ENUM>(c); \
69 using namespace boost::describe; \
70 return std::string{wren::utils::enum_to_string(e)}; \
73 inline auto ERROR_ENUM##_category()->const ERROR_ENUM##_category_t& { \
74 static const ERROR_ENUM##_category_t kC; \
77 inline auto make_error_code(ERROR_ENUM ec) noexcept -> std::error_code { \
78 return {static_cast<int32_t>(ec), ERROR_ENUM##_category()}; \
89#define DEFINE_ERROR(cat_name, name, ...) \
90 BOOST_DEFINE_ENUM_CLASS(name, __VA_ARGS__); \
91 DEFINE_ERROR_IMPL(cat_name, name)
93#define RESULT_UNIQUE_NAME() BOOST_PP_CAT(__result_tmp, __COUNTER__)
95#define TRY_RESULT_IMPL(unique, expr) \
96 auto unique = (expr); \
97 if (!(unique).has_value()) return std::unexpected(unique.error());
99#define TRY_RESULT_1(unique, expr) TRY_RESULT_IMPL(unique, expr)
100#define TRY_RESULT_2(unique, out, expr) \
101 TRY_RESULT_IMPL(unique, expr) \
102 out = (unique).value();
104#define TRY_RESULT(...) \
105 BOOST_PP_OVERLOAD(TRY_RESULT_, __VA_ARGS__) \
106 (RESULT_UNIQUE_NAME(), __VA_ARGS__)
108#define TRY_RESULT_VOID_IMPL(unique, expr) \
109 auto unique = (expr); \
110 if (!(unique).has_value()) return;
112#define TRY_RESULT_VOID_1(unique, expr) TRY_RESULT_VOID_IMPL(unique, expr)
113#define TRY_RESULT_VOID_2(unique, out, expr) \
114 TRY_RESULT_VOID_IMPL(unique, expr) \
115 out = (unique).value();
116#define TRY_RESULT_VOID(...) \
117 BOOST_PP_OVERLOAD(TRY_RESULT_VOID_, __VA_ARGS__) \
118 (RESULT_UNIQUE_NAME(), __VA_ARGS__)
130#define ERR_VAL_OR(out, err, on_err) \
131 const auto LINEIZE(res, __LINE__) = err; \
132 if (!LINEIZE(res, __LINE__).has_value()) { \
135 out = LINEIZE(res, __LINE__).value();
144#define ERR_VOID_OR(err, on_err) \
145 const auto LINEIZE(res, __LINE__) = err; \
146 if (!LINEIZE(res, __LINE__).has_value()) { \
Err(T error)
Definition result.hpp:22
Err(const std::error_code &ec)
Definition result.hpp:23
auto extra_msg() const
Definition result.hpp:30
auto error() const
Definition result.hpp:26
auto message() const
Definition result.hpp:28
std::error_code error_code_
Definition result.hpp:33
Err(int32_t ec, const std::error_category &e_cat)
Definition result.hpp:24
std::optional< std::string > extra_message_
Definition result.hpp:34
Definition editor_scene.hpp:5
std::expected< T, Err > expected
Definition result.hpp:50