wren
Vulkan-based game engine
Loading...
Searching...
No Matches
result.hpp File Reference
#include <fmt/core.h>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <boost/describe.hpp>
#include <boost/preprocessor.hpp>
#include <expected>
#include <optional>
#include <system_error>
#include "enums.hpp"
#include "macros.hpp"
Include dependency graph for result.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  wren::Err
 
struct  fmt::formatter< wren::Err >
 

Namespaces

namespace  wren
 

Macros

#define DEFINE_ERROR_IMPL(CAT_NAME, ERROR_ENUM)
 This macro creates the hooks into std::error_code for a given error enum.
 
#define DEFINE_ERROR(cat_name, name, ...)
 
#define RESULT_UNIQUE_NAME()   BOOST_PP_CAT(__result_tmp, __COUNTER__)
 
#define TRY_RESULT_IMPL(unique, expr)
 
#define TRY_RESULT_1(unique, expr)   TRY_RESULT_IMPL(unique, expr)
 
#define TRY_RESULT_2(unique, out, expr)
 
#define TRY_RESULT(...)
 
#define TRY_RESULT_VOID_IMPL(unique, expr)
 
#define TRY_RESULT_VOID_1(unique, expr)   TRY_RESULT_VOID_IMPL(unique, expr)
 
#define TRY_RESULT_VOID_2(unique, out, expr)
 
#define TRY_RESULT_VOID(...)
 
#define ERR_VAL_OR(out, err, on_err)
 Checks a tl::expected for error and either sets the out param or runs the on_err.
 
#define ERR_VOID_OR(err, on_err)
 Checks a tl::expected for error runs the on_err.
 

Typedefs

template<typename T >
using wren::expected = std::expected<T, Err>
 

Macro Definition Documentation

◆ DEFINE_ERROR

#define DEFINE_ERROR ( cat_name,
name,
... )
Value:
BOOST_DEFINE_ENUM_CLASS(name, __VA_ARGS__); \
DEFINE_ERROR_IMPL(cat_name, name)

◆ DEFINE_ERROR_IMPL

#define DEFINE_ERROR_IMPL ( CAT_NAME,
ERROR_ENUM )
Value:
class ERROR_ENUM##_category_t : public std::error_category { \
public: \
[[nodiscard]] auto name() const noexcept -> const char* final { \
return CAT_NAME; \
} \
[[nodiscard]] auto message(int32_t c) const -> std::string final { \
auto e = static_cast<ERROR_ENUM>(c); \
using namespace boost::describe; \
return std::string{wren::utils::enum_to_string(e)}; \
} \
}; \
inline auto ERROR_ENUM##_category()->const ERROR_ENUM##_category_t& { \
static const ERROR_ENUM##_category_t kC; \
return kC; \
} \
inline auto make_error_code(ERROR_ENUM ec) noexcept -> std::error_code { \
return {static_cast<int32_t>(ec), ERROR_ENUM##_category()}; \
}
auto enum_to_string(E e) -> std::string
Definition enums.hpp:14

This macro creates the hooks into std::error_code for a given error enum.

◆ ERR_VAL_OR

#define ERR_VAL_OR ( out,
err,
on_err )
Value:
const auto LINEIZE(res, __LINE__) = err; \
if (!LINEIZE(res, __LINE__).has_value()) { \
on_err \
} \
out = LINEIZE(res, __LINE__).value();
#define LINEIZE(n, line)
Definition macros.hpp:4

Checks a tl::expected for error and either sets the out param or runs the on_err.

Parameters
outA variable to save the resulting expected's value, on success
errThe expression resulting in an expected value
on_errThe expression to run if there's an error

◆ ERR_VOID_OR

#define ERR_VOID_OR ( err,
on_err )
Value:
const auto LINEIZE(res, __LINE__) = err; \
if (!LINEIZE(res, __LINE__).has_value()) { \
on_err \
}

Checks a tl::expected for error runs the on_err.

Parameters
errThe expression resulting in an expected value
on_errThe expression to run if there's an error

◆ RESULT_UNIQUE_NAME

#define RESULT_UNIQUE_NAME ( )    BOOST_PP_CAT(__result_tmp, __COUNTER__)

◆ TRY_RESULT

#define TRY_RESULT ( ...)
Value:
BOOST_PP_OVERLOAD(TRY_RESULT_, __VA_ARGS__) \
(RESULT_UNIQUE_NAME(), __VA_ARGS__)
#define RESULT_UNIQUE_NAME()
Definition result.hpp:92

◆ TRY_RESULT_1

#define TRY_RESULT_1 ( unique,
expr )   TRY_RESULT_IMPL(unique, expr)

◆ TRY_RESULT_2

#define TRY_RESULT_2 ( unique,
out,
expr )
Value:
TRY_RESULT_IMPL(unique, expr) \
out = (unique).value();
#define TRY_RESULT_IMPL(unique, expr)
Definition result.hpp:94

◆ TRY_RESULT_IMPL

#define TRY_RESULT_IMPL ( unique,
expr )
Value:
auto unique = (expr); \
if (!(unique).has_value()) return std::unexpected(unique.error());

◆ TRY_RESULT_VOID

#define TRY_RESULT_VOID ( ...)
Value:
BOOST_PP_OVERLOAD(TRY_RESULT_VOID_, __VA_ARGS__) \
(RESULT_UNIQUE_NAME(), __VA_ARGS__)

◆ TRY_RESULT_VOID_1

#define TRY_RESULT_VOID_1 ( unique,
expr )   TRY_RESULT_VOID_IMPL(unique, expr)

◆ TRY_RESULT_VOID_2

#define TRY_RESULT_VOID_2 ( unique,
out,
expr )
Value:
TRY_RESULT_VOID_IMPL(unique, expr) \
out = (unique).value();
#define TRY_RESULT_VOID_IMPL(unique, expr)
Definition result.hpp:107

◆ TRY_RESULT_VOID_IMPL

#define TRY_RESULT_VOID_IMPL ( unique,
expr )
Value:
auto unique = (expr); \
if (!(unique).has_value()) return;