Skip to content

Commit

Permalink
Add macro
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Jul 30, 2024
1 parent 8b91771 commit 82233a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 13 additions & 0 deletions components/core/src/clp/error_handling/ErrorCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,17 @@ auto make_error_code(ErrorCode<ErrorCodeEnum> error) -> std::error_code {
}
} // namespace clp::error_handling

/**
* The macro to create a specialization of `std::is_error_code_enum` for a given type T. Only types
* that are marked with this macro will be considered as a valid CLP error code enum, and thus used
* to specialize `ErrorCode` and `ErrorCategory` templates.
*/
// NOLINTBEGIN(bugprone-macro-parentheses, cppcoreguidelines-macro-usage)
#define CLP_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(T) \
template <> \
struct std::is_error_code_enum<clp::error_handling::ErrorCode<T>> : std::true_type { \
static_assert(std::is_enum_v<T>); \
};
// NOLINTEND(bugprone-macro-parentheses, cppcoreguidelines-macro-usage)

#endif // CLP_ERROR_HANDLING_ERRORCODE_HPP
9 changes: 2 additions & 7 deletions components/core/tests/test-error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ constexpr std::array cFailureConditions{std::errc::not_connected, std::errc::tim
constexpr std::array cNoneFailureConditions{std::errc::broken_pipe, std::errc::address_in_use};
} // namespace

namespace std {
template <>
struct is_error_code_enum<BinaryErrorCode> : std::true_type {};

template <>
struct is_error_code_enum<AlwaysSuccessErrorCode> : std::true_type {};
} // namespace std
CLP_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(AlwaysSuccessErrorCodeEnum);
CLP_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(BinaryErrorCodeEnum);

template <>
auto AlwaysSuccessErrorCategory::name() const noexcept -> char const* {
Expand Down

0 comments on commit 82233a4

Please sign in to comment.