|
| 1 | +#ifndef YSTDLIB_ERROR_HANDLING_ERRORCODE_HPP |
| 2 | +#define YSTDLIB_ERROR_HANDLING_ERRORCODE_HPP |
| 3 | + |
| 4 | +#include <concepts> |
| 5 | +#include <string> |
| 6 | +#include <system_error> |
| 7 | +#include <type_traits> |
| 8 | + |
| 9 | +namespace ystdlib::error_handling { |
| 10 | +/** |
| 11 | + * Concept that defines a template parameter of an integer-based error code enumeration. |
| 12 | + * @tparam Type |
| 13 | + */ |
| 14 | +template <typename Type> |
| 15 | +concept ErrorCodeEnumType = std::is_enum_v<Type> && requires(Type type) { |
| 16 | + { |
| 17 | + static_cast<std::underlying_type_t<Type>>(type) |
| 18 | + } -> std::convertible_to<int>; |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Template that defines a `std::error_category` of the given set of error code enumeration. |
| 23 | + * @tparam ErrorCodeEnum |
| 24 | + */ |
| 25 | +template <ErrorCodeEnumType ErrorCodeEnum> |
| 26 | +class ErrorCategory : public std::error_category { |
| 27 | +public: |
| 28 | + // Methods implementing `std::error_category` |
| 29 | + /** |
| 30 | + * Gets the error category name. |
| 31 | + * Note: A specialization must be explicitly implemented for each valid `ErrorCodeEnum`. |
| 32 | + * @return The name of the error category. |
| 33 | + */ |
| 34 | + [[nodiscard]] auto name() const noexcept -> char const* override; |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets the descriptive message associated with the given error. |
| 38 | + * @param error_num |
| 39 | + * @return The descriptive message for the error. |
| 40 | + */ |
| 41 | + [[nodiscard]] auto message(int error_num) const -> std::string override { |
| 42 | + return message(static_cast<ErrorCodeEnum>(error_num)); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param error_num |
| 47 | + * @param condition |
| 48 | + * @return Whether the error condition of the given error matches the given condition. |
| 49 | + */ |
| 50 | + [[nodiscard]] auto |
| 51 | + equivalent(int error_num, std::error_condition const& condition) const noexcept |
| 52 | + -> bool override { |
| 53 | + return equivalent(static_cast<ErrorCodeEnum>(error_num), condition); |
| 54 | + } |
| 55 | + |
| 56 | + // Methods |
| 57 | + /** |
| 58 | + * Gets the descriptive message associated with the given error. |
| 59 | + * Note: A specialization must be explicitly implemented for each valid `ErrorCodeEnum`. |
| 60 | + * @param error_enum. |
| 61 | + * @return The descriptive message for the error. |
| 62 | + */ |
| 63 | + [[nodiscard]] auto message(ErrorCodeEnum error_enum) const -> std::string; |
| 64 | + |
| 65 | + /** |
| 66 | + * Note: A specialization can be implemented to create error enum to error condition mappings. |
| 67 | + * @param error_num |
| 68 | + * @param condition |
| 69 | + * @return Whether the error condition of the given error matches the given condition. |
| 70 | + */ |
| 71 | + [[nodiscard]] auto |
| 72 | + equivalent(ErrorCodeEnum error_enum, std::error_condition const& condition) const noexcept |
| 73 | + -> bool; |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Template class that defines an error code. An error code is represented by a error enum value and |
| 78 | + * the associated error category. This template class is designed to be `std::error_code` |
| 79 | + * compatible, meaning that every instance of this class can be used to construct a corresponded |
| 80 | + * `std::error_code` instance, or compare with a `std::error_code` instance to inspect a specific |
| 81 | + * error. |
| 82 | + * @tparam ErrorCodeEnum |
| 83 | + */ |
| 84 | +template <ErrorCodeEnumType ErrorCodeEnum> |
| 85 | +class ErrorCode { |
| 86 | +public: |
| 87 | + /** |
| 88 | + * @return The reference to the singleton of the corresponded error category. |
| 89 | + */ |
| 90 | + [[nodiscard]] constexpr static auto get_category() -> ErrorCategory<ErrorCodeEnum> const& { |
| 91 | + return cCategory; |
| 92 | + } |
| 93 | + |
| 94 | + // Constructor |
| 95 | + ErrorCode(ErrorCodeEnum error) : m_error{error} {} |
| 96 | + |
| 97 | + /** |
| 98 | + * @return The underlying error code enum. |
| 99 | + */ |
| 100 | + [[nodiscard]] auto get_error() const -> ErrorCodeEnum { return m_error; } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return The error code as an error number. |
| 104 | + */ |
| 105 | + [[nodiscard]] auto get_error_num() const -> int { return static_cast<int>(m_error); } |
| 106 | + |
| 107 | +private: |
| 108 | + static inline ErrorCategory<ErrorCodeEnum> const cCategory; |
| 109 | + |
| 110 | + ErrorCodeEnum m_error; |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * @tparam ErrorCodeEnum |
| 115 | + * @param error |
| 116 | + * @return Constructed `std::error_code` from the given `ErrorCode` instance. |
| 117 | + */ |
| 118 | +template <typename ErrorCodeEnum> |
| 119 | +[[nodiscard]] auto make_error_code(ErrorCode<ErrorCodeEnum> error) -> std::error_code; |
| 120 | + |
| 121 | +template <ErrorCodeEnumType ErrorCodeEnum> |
| 122 | +auto ErrorCategory<ErrorCodeEnum>::equivalent( |
| 123 | + ErrorCodeEnum error_enum, |
| 124 | + std::error_condition const& condition |
| 125 | +) const noexcept -> bool { |
| 126 | + return std::error_category::default_error_condition(static_cast<int>(error_enum)) == condition; |
| 127 | +} |
| 128 | + |
| 129 | +template <typename ErrorCodeEnum> |
| 130 | +auto make_error_code(ErrorCode<ErrorCodeEnum> error) -> std::error_code { |
| 131 | + return {error.get_error_num(), ErrorCode<ErrorCodeEnum>::get_category()}; |
| 132 | +} |
| 133 | +} // namespace ystdlib::error_handling |
| 134 | + |
| 135 | +/** |
| 136 | + * The macro to create a specialization of `std::is_error_code_enum` for a given type T. Only types |
| 137 | + * that are marked with this macro will be considered as a valid YStdlib error code enum, and thus |
| 138 | + * used to specialize `ErrorCode` and `ErrorCategory` templates. |
| 139 | + */ |
| 140 | +// NOLINTBEGIN(bugprone-macro-parentheses, cppcoreguidelines-macro-usage) |
| 141 | +#define YSTDLIB_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(T) \ |
| 142 | + template <> \ |
| 143 | + struct std::is_error_code_enum<ystdlib::error_handling::ErrorCode<T>> : std::true_type { \ |
| 144 | + static_assert(std::is_enum_v<T>); \ |
| 145 | + }; |
| 146 | +// NOLINTEND(bugprone-macro-parentheses, cppcoreguidelines-macro-usage) |
| 147 | + |
| 148 | +#endif // YSTDLIB_ERROR_HANDLING_ERRORCODE_HPP |
0 commit comments