3#include <boost/describe.hpp>
4#include <boost/describe/enum_to_string.hpp>
5#include <boost/outcome.hpp>
6#include <boost/preprocessor/cat.hpp>
7#include <boost/preprocessor/stringize.hpp>
11#include <system_error>
25 template <IsErrorCode T>
26 Err(
const T& ec,
const std::optional<std::string>& msg = {})
27 : ec_(make_error_code(ec)), extra_msg_(msg) {}
29 Err(
const std::error_code& ec,
const std::optional<std::string>& msg = {})
30 : ec_(ec), extra_msg_(msg) {}
32 template <IsErrorCode T>
34 return ec_ == make_error_code(ec);
37 template <IsErrorCode T>
52 std::optional<std::string> extra_msg_;
54 BOOST_DESCRIBE_CLASS(
Err, (), (), (), (ec_, extra_msg_));
58 return boost::outcome_v2::success();
68 boost::outcome_v2::policy::terminate>;
72#define DEFINE_ERROR_IMPL(module_name, ERROR_ENUM) \
73 class ERROR_ENUM##_category_t : public std::error_category { \
75 const char* name() const noexcept override { \
76 return (module_name BOOST_PP_STRINGIZE(_##ERROR_ENUM)); \
78 std::string message(int32_t e) const override { \
79 const auto ec = static_cast<ERROR_ENUM>(e); \
80 return boost::describe::enum_to_string(ec, ""); \
83 inline auto ERROR_ENUM##_category()->const ERROR_ENUM##_category_t& { \
84 static const ERROR_ENUM##_category_t e_cat{}; \
87 inline auto make_error_code(ERROR_ENUM ec) -> std::error_code { \
88 return {static_cast<int32_t>(ec), ERROR_ENUM##_category()}; \
91#define DEFINE_ERROR(module_name, name, ...) \
92 BOOST_DEFINE_ENUM_CLASS(name, __VA_ARGS__) \
93 DEFINE_ERROR_IMPL(module_name, name)
95#define RESULT_UNIQUE_NAME() BOOST_PP_CAT(__result_, __COUNTER__)
97#define TRY_RESULT(...) BOOST_OUTCOME_TRY(__VA_ARGS__)
99#define TRY_RESULT_IMPL(unique_name, expr, alt) \
100 auto unique_name = expr; \
103#define TRY_RESULT_OR(expr, alt) \
104 TRY_RESULT_IMPL(RESULT_UNIQUE_NAME(), expr, alt)
auto operator==(const T &ec) const
Definition result.hpp:33
Err(const T &ec, const std::optional< std::string > &msg={})
Definition result.hpp:26
Err(const std::error_code &ec, const std::optional< std::string > &msg={})
Definition result.hpp:29
auto Code()
Definition result.hpp:42
auto operator!=(const T &ec) const
Definition result.hpp:38
auto message()
Definition result.hpp:46
auto Ok()
Definition result.hpp:57
boost::outcome_v2::result< T, result::Err, boost::outcome_v2::policy::terminate > result_t
Definition result.hpp:66