wren
Vulkan-based game engine
Loading...
Searching...
No Matches
result.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <fmt/core.h>
4#include <fmt/format.h>
5#include <fmt/ostream.h>
6
7#include <boost/describe.hpp>
8#include <boost/preprocessor.hpp>
9#include <expected>
10#include <optional>
11#include <system_error>
12
13#include "enums.hpp" // IWYU pragma: export
14#include "macros.hpp"
15
16namespace wren {
17
18class Err {
19 public:
20 template <typename T>
21 Err(T error) : error_code_(make_error_code(error)) {}
22 Err(const std::error_code& ec) : error_code_(ec) {}
23 Err(int32_t ec, const std::error_category& e_cat) : error_code_(ec, e_cat) {}
24
25 [[nodiscard]] auto error() const { return error_code_; }
26
27 [[nodiscard]] auto message() const { return error_code_.message(); }
28
29 [[nodiscard]] auto extra_msg() const { return extra_message_; }
30
31 private:
32 std::error_code error_code_;
33 std::optional<std::string> extra_message_;
34};
35
36// struct error_code : public std::error_code {
37// error_code(int32_t ec, std::error_category const& cat)
38// : ::error_code(ec, cat) {}
39
40// error_code(std::error_code ec) : std::error_code(ec) {}
41
42// friend auto operator<<(std::ostream& os, error_code const& ec)
43// -> std::ostream& {
44// return os << ec.category().name() << " : " << ec.message();
45// }
46// };
47
48template <typename T>
49using expected = std::expected<T, Err>;
50
51} // namespace wren
52
53template <>
54struct fmt::formatter<wren::Err> : fmt::formatter<std::string> {
55 auto format(wren::Err, fmt::format_context& ctx) const -> decltype(ctx.out());
56};
57
60#define DEFINE_ERROR_IMPL(CAT_NAME, ERROR_ENUM) \
61 class ERROR_ENUM##_category_t : public std::error_category { \
62 public: \
63 [[nodiscard]] auto name() const noexcept -> const char* final { \
64 return CAT_NAME; \
65 } \
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)}; \
70 } \
71 }; \
72 inline auto ERROR_ENUM##_category()->const ERROR_ENUM##_category_t& { \
73 static const ERROR_ENUM##_category_t kC; \
74 return kC; \
75 } \
76 inline auto make_error_code(ERROR_ENUM ec) noexcept -> std::error_code { \
77 return {static_cast<int32_t>(ec), ERROR_ENUM##_category()}; \
78 }
79
80/* @brief This macro defines an enum with BOOST_DEFINE_ENUM_CLASS and
81 * hooks into the std::error_code system The NAMSPACE arg can be
82 * skipped if the enum is in the global namespace.
83 * @param cat_name The name to use for the Error's category, should
84 * be something similar to the current namespace
85 * @param name the error enum name
86 * @param ... The enum variants to define
87 */
88#define DEFINE_ERROR(cat_name, name, ...) \
89 BOOST_DEFINE_ENUM_CLASS(name, __VA_ARGS__); \
90 DEFINE_ERROR_IMPL(cat_name, name)
91
92#define RESULT_UNIQUE_NAME() BOOST_PP_CAT(__result_tmp, __COUNTER__)
93
94#define TRY_RESULT_IMPL(unique, expr) \
95 auto unique = (expr); \
96 if (!(unique).has_value()) return std::unexpected(unique.error());
97
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();
102
103#define TRY_RESULT(...) \
104 BOOST_PP_OVERLOAD(TRY_RESULT_, __VA_ARGS__) \
105 (RESULT_UNIQUE_NAME(), __VA_ARGS__)
106
107#define TRY_RESULT_VOID_IMPL(unique, expr) \
108 auto unique = (expr); \
109 if (!(unique).has_value()) return;
110
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__)
118
128// NOLINTNEXTLINE
129#define ERR_VAL_OR(out, err, on_err) \
130 const auto LINEIZE(res, __LINE__) = err; \
131 if (!LINEIZE(res, __LINE__).has_value()) { \
132 on_err \
133 } \
134 out = LINEIZE(res, __LINE__).value();
135
142// NOLINTNEXTLINE
143#define ERR_VOID_OR(err, on_err) \
144 const auto LINEIZE(res, __LINE__) = err; \
145 if (!LINEIZE(res, __LINE__).has_value()) { \
146 on_err \
147 }
Definition result.hpp:18
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