Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
error_codes_pp.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <sysio/vm/config.hpp>
4
5#include <string>
6
7#ifdef SYS_VM_USE_BOOST
8# include <boost/system/error_code.hpp>
9# include <boost/type_traits.hpp>
10using error_category_t = boost::system::error_category;
11using error_code_t = boost::system::error_code;
12# define ERROR_CODE_NAMESPACE boost::system
13# define TRUE_TYPE boost::true_type
14#else
15# include <system_error>
16# include <type_traits>
17using error_category_t = std::error_category;
18using error_code_t = std::error_code;
19# define ERROR_CODE_NAMESPACE std
20# define TRUE_TYPE std::true_type
21#endif
22
23#define GENERATE_ERROR_CATEGORY(CATEGORY, NAME) \
24 namespace sysio { namespace vm { \
25 struct CATEGORY##_category : error_category_t { \
26 const char* name() const noexcept override { return #NAME; } \
27 std::string message(int ev) const override; \
28 }; \
29 const CATEGORY##_category __##CATEGORY##_category{}; \
30 template <typename T> \
31 static inline constexpr auto is_a(const error_code_t& ec) \
32 -> std::enable_if_t<std::is_same_v<T, CATEGORY##_category>, bool> { \
33 return ec.category() == __##CATEGORY##_category; \
34 } \
35 } \
36 }
37
38#define GENERATE_ENUM_ELEM(PARENT, ITEM) ITEM,
39
40#define GENERATE_STR_ELEM(PARENT, ITEM) \
41 case PARENT::ITEM: \
42 return #ITEM;
43
44#define CREATE_ERROR_CODES(CATEGORY, ERRORS) \
45 namespace sysio { namespace vm { \
46 enum class CATEGORY { ERRORS(GENERATE_ENUM_ELEM) }; \
47 } \
48 } \
49 namespace ERROR_CODE_NAMESPACE { \
50 template <> \
51 struct is_error_code_enum<sysio::vm::CATEGORY> : TRUE_TYPE {}; \
52 } \
53 namespace sysio { namespace vm { \
54 inline std::string CATEGORY##_category::message(int ev) const { \
55 switch (static_cast<CATEGORY>(ev)) { ERRORS(GENERATE_STR_ELEM) } \
56 return ""; \
57 } \
58 inline error_code_t make_error_code(CATEGORY e) noexcept { \
59 return { static_cast<int>(e), __##CATEGORY##_category }; \
60 } \
61 } \
62 }
std::error_code error_code_t
std::error_category error_category_t