5#include <fmt/ostream.h>
7#include <boost/describe.hpp>
8#include <boost/preprocessor.hpp>
11#include <system_error>
23 Err(int32_t ec,
const std::error_category& e_cat) :
error_code_(ec, e_cat) {}
54struct fmt::formatter<
wren::Err> : fmt::formatter<std::string> {
55 auto format(
wren::Err, fmt::format_context& ctx)
const ->
decltype(ctx.out());
60#define DEFINE_ERROR_IMPL(CAT_NAME, ERROR_ENUM) \
61 class ERROR_ENUM##_category_t : public std::error_category { \
63 [[nodiscard]] auto name() const noexcept -> const char* final { \
66 [[nodiscard]] auto message(int32_t c) const -> std::string final { \
67 auto e = static_cast<ERROR_ENUM>(c); \
68 using namespace boost::describe; \
69 return std::string{wren::utils::enum_to_string(e)}; \
72 inline auto ERROR_ENUM##_category()->const ERROR_ENUM##_category_t& { \
73 static const ERROR_ENUM##_category_t kC; \
76 inline auto make_error_code(ERROR_ENUM ec) noexcept -> std::error_code { \
77 return {static_cast<int32_t>(ec), ERROR_ENUM##_category()}; \
88#define DEFINE_ERROR(cat_name, name, ...) \
89 BOOST_DEFINE_ENUM_CLASS(name, __VA_ARGS__); \
90 DEFINE_ERROR_IMPL(cat_name, name)
92#define RESULT_UNIQUE_NAME() BOOST_PP_CAT(__result_tmp, __COUNTER__)
94#define TRY_RESULT_IMPL(unique, expr) \
95 auto unique = (expr); \
96 if (!(unique).has_value()) return std::unexpected(unique.error());
98#define TRY_RESULT_1(unique, expr) TRY_RESULT_IMPL(unique, expr)
99#define TRY_RESULT_2(unique, out, expr) \
100 TRY_RESULT_IMPL(unique, expr) \
101 out = (unique).value();
103#define TRY_RESULT(...) \
104 BOOST_PP_OVERLOAD(TRY_RESULT_, __VA_ARGS__) \
105 (RESULT_UNIQUE_NAME(), __VA_ARGS__)
107#define TRY_RESULT_VOID_IMPL(unique, expr) \
108 auto unique = (expr); \
109 if (!(unique).has_value()) return;
111#define TRY_RESULT_VOID_1(unique, expr) TRY_RESULT_VOID_IMPL(unique, expr)
112#define TRY_RESULT_VOID_2(unique, out, expr) \
113 TRY_RESULT_VOID_IMPL(unique, expr) \
114 out = (unique).value();
115#define TRY_RESULT_VOID(...) \
116 BOOST_PP_OVERLOAD(TRY_RESULT_VOID_, __VA_ARGS__) \
117 (RESULT_UNIQUE_NAME(), __VA_ARGS__)
129#define ERR_VAL_OR(out, err, on_err) \
130 const auto LINEIZE(res, __LINE__) = err; \
131 if (!LINEIZE(res, __LINE__).has_value()) { \
134 out = LINEIZE(res, __LINE__).value();
143#define ERR_VOID_OR(err, on_err) \
144 const auto LINEIZE(res, __LINE__) = err; \
145 if (!LINEIZE(res, __LINE__).has_value()) { \
Err(const std::error_code &ec)
Definition result.hpp:22
auto extra_msg() const
Definition result.hpp:29
auto error() const
Definition result.hpp:25
auto message() const
Definition result.hpp:27
Err(T error)
Definition result.hpp:21
std::error_code error_code_
Definition result.hpp:32
Err(int32_t ec, const std::error_category &e_cat)
Definition result.hpp:23
std::optional< std::string > extra_message_
Definition result.hpp:33
Definition editor_scene.hpp:5
std::expected< T, Err > expected
Definition result.hpp:49