From 2d765aae597ecc5e5de5e1fe8a3df191e417abe9 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Fri, 19 Jul 2024 01:41:26 -0400 Subject: [PATCH 01/11] Implement error code template --- components/core/CMakeLists.txt | 2 + .../core/src/clp/error_handling/ErrorCode.hpp | 122 ++++++++++++++++++ components/core/tests/test-error_handling.cpp | 113 ++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 components/core/src/clp/error_handling/ErrorCode.hpp create mode 100644 components/core/tests/test-error_handling.cpp diff --git a/components/core/CMakeLists.txt b/components/core/CMakeLists.txt index 7cba49acb..ed16a45c1 100644 --- a/components/core/CMakeLists.txt +++ b/components/core/CMakeLists.txt @@ -292,6 +292,7 @@ set(SOURCE_FILES_unitTest src/clp/DictionaryEntry.hpp src/clp/DictionaryReader.hpp src/clp/DictionaryWriter.hpp + src/clp/error_handling/ErrorCode.hpp src/clp/EncodedVariableInterpreter.cpp src/clp/EncodedVariableInterpreter.hpp src/clp/ErrorCode.hpp @@ -459,6 +460,7 @@ set(SOURCE_FILES_unitTest tests/test-BufferedFileReader.cpp tests/test-EncodedVariableInterpreter.cpp tests/test-encoding_methods.cpp + tests/test-error_handling.cpp tests/test-ffi_SchemaTree.cpp tests/test-Grep.cpp tests/test-ir_encoding_methods.cpp diff --git a/components/core/src/clp/error_handling/ErrorCode.hpp b/components/core/src/clp/error_handling/ErrorCode.hpp new file mode 100644 index 000000000..ea132b72b --- /dev/null +++ b/components/core/src/clp/error_handling/ErrorCode.hpp @@ -0,0 +1,122 @@ +#ifndef CLP_ERROR_HANDLING_ERRORCODE_HPP +#define CLP_ERROR_HANDLING_ERRORCODE_HPP + +#include +#include +#include +#include + +namespace clp::error_handling { +/** + * Concept that defines a template parameter of an integer-based error code enumeration. + * @tparam Type + */ +template +concept ErrorCodeEnumType = std::is_enum_v && requires(Type type) { + { + static_cast>(type) + } -> std::convertible_to; +}; + +/** + * Template that defines a `std::error_category` of the given set of error code enumeration. + * @tparam ErrorCodeEnum + */ +template +class ErrorCategory : public std::error_category { +public: + // Methods implementing `std::error_category` + /** + * Gets the error category name. + * Note: A specialization must be explicitly implemented for each valid `ErrorCodeEnum`. + * @return The name of the error category. + */ + [[nodiscard]] auto name() const noexcept -> char const* override; + + /** + * Gets the descriptive message associated with the given error. + * @param error_num + * @return The descriptive message for the error. + */ + [[nodiscard]] auto message(int error_num) const -> std::string override; + + // Methods + /** + * Gets the descriptive message associated with the given error. + * Note: A specialization must be explicitly implemented for each valid `ErrorCodeEnum`. + * @param error_enum. + * @return The descriptive message for the error. + */ + [[nodiscard]] auto message(ErrorCodeEnum error_enum) const -> std::string; +}; + +/** + * Template class that defines an error code. An error code is represented by a error enum value and + * the associated error category. This template class is designed to be `std::error_code` + * compatible, meaning that every instance of this class can be used to construct a corresponded + * `std::error_code` instance, or compare with a `std::error_code` instance to inspect a specific + * error. + * @tparam ErrorCodeEnum + */ +template +class ErrorCode { +public: + // Constructor + ErrorCode(ErrorCodeEnum error) : m_error{error} {} + + /** + * @return The error code as an error number. + */ + [[nodiscard]] auto get_errno() const -> int; + + /** + * @return The underlying error code enum. + */ + [[nodiscard]] auto get_err_enum() const -> ErrorCodeEnum; + + /** + * @return The reference to the singleton of the corresponded error category. + */ + [[nodiscard]] static auto get_category() -> ErrorCategory const&; + +private: + static inline ErrorCategory const cCategory; + + ErrorCodeEnum m_error; +}; + +/** + * @tparam ErrorCodeEnum + * @param error + * @return Constructed `std::error_code` from the given `ErrorCode` instance. + */ +template +[[nodiscard]] auto make_error_code(ErrorCode error) -> std::error_code; + +template +auto ErrorCategory::message(int error_num) const -> std::string { + return message(static_cast(error_num)); +} + +template +auto ErrorCode::get_errno() const -> int { + return static_cast(m_error); +} + +template +auto ErrorCode::get_err_enum() const -> ErrorCodeEnum { + return m_error; +} + +template +auto ErrorCode::get_category() -> ErrorCategory const& { + return ErrorCode::cCategory; +} + +template +[[nodiscard]] auto make_error_code(ErrorCode error) -> std::error_code { + return {error.get_errno(), ErrorCode::get_category()}; +} +} // namespace clp::error_handling + +#endif // CLP_ERROR_HANDLING_ERRORCODE_HPP diff --git a/components/core/tests/test-error_handling.cpp b/components/core/tests/test-error_handling.cpp new file mode 100644 index 000000000..c0dd85558 --- /dev/null +++ b/components/core/tests/test-error_handling.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include + +#include + +#include "../src/clp/error_handling/ErrorCode.hpp" + +using clp::error_handling::ErrorCategory; +using clp::error_handling::ErrorCode; +using std::string; +using std::string_view; + +namespace { +enum class AlwaysSuccessErrorCodeEnum : uint8_t { + Success = 0 +}; + +enum class BinaryErrorCodeEnum : uint8_t { + Success = 0, + Failure +}; + +using AlwaysSuccessErrorCode = ErrorCode; +using AlwaysSuccessErrorCategory = ErrorCategory; +using BinaryErrorCode = ErrorCode; +using BinaryErrorCategory = ErrorCategory; + +constexpr string_view cAlwaysSuccessErrorCategoryName{"Always Success Error Code"}; +constexpr string_view cBinaryTestErrorCategoryName{"Binary Error Code"}; +constexpr string_view cSuccessErrMsg{"Success"}; +constexpr string_view cFailureErrMsg{"Failure"}; +constexpr string_view cUnrecognizedErrorCode{"Unrecognized Error Code"}; +} // namespace + +namespace std { +template <> +struct is_error_code_enum : std::true_type {}; + +template <> +struct is_error_code_enum : std::true_type {}; +} // namespace std + +template <> +auto AlwaysSuccessErrorCategory::name() const noexcept -> char const* { + return cAlwaysSuccessErrorCategoryName.data(); +} + +template <> +auto AlwaysSuccessErrorCategory::message(AlwaysSuccessErrorCodeEnum error_enum) const -> string { + switch (error_enum) { + case AlwaysSuccessErrorCodeEnum::Success: + return string{cSuccessErrMsg}; + default: + return string{cUnrecognizedErrorCode}; + } +} + +template <> +auto BinaryErrorCategory::name() const noexcept -> char const* { + return cBinaryTestErrorCategoryName.data(); +} + +template <> +auto BinaryErrorCategory::message(BinaryErrorCodeEnum error_enum) const -> string { + switch (error_enum) { + case BinaryErrorCodeEnum::Success: + return string{cSuccessErrMsg}; + case BinaryErrorCodeEnum::Failure: + return string{cFailureErrMsg}; + default: + return string{cUnrecognizedErrorCode}; + } +} + +TEST_CASE("test_error_code_implementation", "[error_handling][ErrorCode]") { + // Test error codes within the same error category + BinaryErrorCode const success{BinaryErrorCodeEnum::Success}; + std::error_code const success_error_code{success}; + REQUIRE((success == success_error_code)); + REQUIRE((cSuccessErrMsg == success_error_code.message())); + REQUIRE((BinaryErrorCode::get_category() == success_error_code.category())); + REQUIRE((cBinaryTestErrorCategoryName == success_error_code.category().name())); + + BinaryErrorCode const failure{BinaryErrorCodeEnum::Failure}; + std::error_code const failure_error_code{failure}; + REQUIRE((failure == failure_error_code)); + REQUIRE((cFailureErrMsg == failure_error_code.message())); + REQUIRE((BinaryErrorCode::get_category() == failure_error_code.category())); + REQUIRE((cBinaryTestErrorCategoryName == failure_error_code.category().name())); + + REQUIRE((success_error_code != failure_error_code)); + REQUIRE((success_error_code.category() == failure_error_code.category())); + + AlwaysSuccessErrorCode const always_success{AlwaysSuccessErrorCodeEnum::Success}; + std::error_code const always_success_error_code{always_success}; + REQUIRE((always_success_error_code == always_success)); + REQUIRE((cSuccessErrMsg == always_success_error_code.message())); + REQUIRE((AlwaysSuccessErrorCode::get_category() == always_success_error_code.category())); + REQUIRE((cAlwaysSuccessErrorCategoryName == always_success_error_code.category().name())); + + // Compare error codes from different error category + // Error codes that have the same value or message won't be the same with each other if they are + // from different error categories. + REQUIRE((success_error_code.value() == always_success_error_code.value())); + REQUIRE((success_error_code.message() == always_success_error_code.message())); + REQUIRE((success_error_code.category() != always_success_error_code.category())); + REQUIRE((success_error_code != always_success_error_code)); + REQUIRE((AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} != success_error_code)); + REQUIRE((BinaryErrorCode{BinaryErrorCodeEnum::Success} != always_success_error_code)); +} From 846f59077f3c6d05cfc41f585fae94443db61191 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Fri, 19 Jul 2024 05:42:29 -0400 Subject: [PATCH 02/11] Remove redundant [[nodiscard]] attribute in the header --- components/core/src/clp/error_handling/ErrorCode.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/core/src/clp/error_handling/ErrorCode.hpp b/components/core/src/clp/error_handling/ErrorCode.hpp index ea132b72b..4e6252702 100644 --- a/components/core/src/clp/error_handling/ErrorCode.hpp +++ b/components/core/src/clp/error_handling/ErrorCode.hpp @@ -114,7 +114,7 @@ auto ErrorCode::get_category() -> ErrorCategory co } template -[[nodiscard]] auto make_error_code(ErrorCode error) -> std::error_code { +auto make_error_code(ErrorCode error) -> std::error_code { return {error.get_errno(), ErrorCode::get_category()}; } } // namespace clp::error_handling From 55f5e3aee0247d4c186eff64030095251c13473f Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Fri, 19 Jul 2024 17:02:47 -0400 Subject: [PATCH 03/11] Implement error condition support --- .../core/src/clp/error_handling/ErrorCode.hpp | 63 ++++++++++++------- components/core/tests/test-error_handling.cpp | 33 ++++++++++ 2 files changed, 72 insertions(+), 24 deletions(-) diff --git a/components/core/src/clp/error_handling/ErrorCode.hpp b/components/core/src/clp/error_handling/ErrorCode.hpp index 4e6252702..1456edbf8 100644 --- a/components/core/src/clp/error_handling/ErrorCode.hpp +++ b/components/core/src/clp/error_handling/ErrorCode.hpp @@ -38,7 +38,21 @@ class ErrorCategory : public std::error_category { * @param error_num * @return The descriptive message for the error. */ - [[nodiscard]] auto message(int error_num) const -> std::string override; + [[nodiscard]] auto message(int error_num) const -> std::string override { + return message(static_cast(error_num)); + } + + /** + * @param error_num + * @param condition + * @return Whether the error condition of the given error matches the given condition. + */ + [[nodiscard]] auto equivalent( + int error_num, + std::error_condition const& condition + ) const noexcept -> bool override { + return equivalent(static_cast(error_num), condition); + } // Methods /** @@ -48,6 +62,17 @@ class ErrorCategory : public std::error_category { * @return The descriptive message for the error. */ [[nodiscard]] auto message(ErrorCodeEnum error_enum) const -> std::string; + + /** + * Note: A specialization can be implemented to create error enum to error condition mappings. + * @param error_num + * @param condition + * @return Whether the error condition of the given error matches the given condition. + */ + [[nodiscard]] auto equivalent( + ErrorCodeEnum error_enum, + std::error_condition const& condition + ) const noexcept -> bool; }; /** @@ -65,19 +90,21 @@ class ErrorCode { ErrorCode(ErrorCodeEnum error) : m_error{error} {} /** - * @return The error code as an error number. + * @return The underlying error code enum. */ - [[nodiscard]] auto get_errno() const -> int; + [[nodiscard]] auto get_error() const -> ErrorCodeEnum { return m_error; } /** - * @return The underlying error code enum. + * @return The error code as an error number. */ - [[nodiscard]] auto get_err_enum() const -> ErrorCodeEnum; + [[nodiscard]] auto get_error_num() const -> int { return static_cast(m_error); } /** * @return The reference to the singleton of the corresponded error category. */ - [[nodiscard]] static auto get_category() -> ErrorCategory const&; + [[nodiscard]] constexpr static auto get_category() -> ErrorCategory const& { + return cCategory; + } private: static inline ErrorCategory const cCategory; @@ -94,28 +121,16 @@ template [[nodiscard]] auto make_error_code(ErrorCode error) -> std::error_code; template -auto ErrorCategory::message(int error_num) const -> std::string { - return message(static_cast(error_num)); -} - -template -auto ErrorCode::get_errno() const -> int { - return static_cast(m_error); -} - -template -auto ErrorCode::get_err_enum() const -> ErrorCodeEnum { - return m_error; -} - -template -auto ErrorCode::get_category() -> ErrorCategory const& { - return ErrorCode::cCategory; +auto ErrorCategory::equivalent( + ErrorCodeEnum error_enum, + std::error_condition const& condition +) const noexcept -> bool { + return std::error_category::default_error_condition(static_cast(error_enum)) == condition; } template auto make_error_code(ErrorCode error) -> std::error_code { - return {error.get_errno(), ErrorCode::get_category()}; + return {error.get_error_num(), ErrorCode::get_category()}; } } // namespace clp::error_handling diff --git a/components/core/tests/test-error_handling.cpp b/components/core/tests/test-error_handling.cpp index c0dd85558..f6e68ca91 100644 --- a/components/core/tests/test-error_handling.cpp +++ b/components/core/tests/test-error_handling.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -33,6 +35,8 @@ constexpr string_view cBinaryTestErrorCategoryName{"Binary Error Code"}; constexpr string_view cSuccessErrMsg{"Success"}; constexpr string_view cFailureErrMsg{"Failure"}; constexpr string_view cUnrecognizedErrorCode{"Unrecognized Error Code"}; +constexpr std::array cFailureConditions{std::errc::not_connected, std::errc::timed_out}; +constexpr std::array cNoneFailureConditions{std::errc::broken_pipe, std::errc::address_in_use}; } // namespace namespace std { @@ -75,6 +79,23 @@ auto BinaryErrorCategory::message(BinaryErrorCodeEnum error_enum) const -> strin } } +template <> +auto BinaryErrorCategory::equivalent( + BinaryErrorCodeEnum error_enum, + std::error_condition const& condition +) const noexcept -> bool { + switch (error_enum) { + case BinaryErrorCodeEnum::Failure: + return std::any_of( + cFailureConditions.cbegin(), + cFailureConditions.cend(), + [&](auto failure_condition) -> bool { return condition == failure_condition; } + ); + default: + return false; + } +} + TEST_CASE("test_error_code_implementation", "[error_handling][ErrorCode]") { // Test error codes within the same error category BinaryErrorCode const success{BinaryErrorCodeEnum::Success}; @@ -90,6 +111,18 @@ TEST_CASE("test_error_code_implementation", "[error_handling][ErrorCode]") { REQUIRE((cFailureErrMsg == failure_error_code.message())); REQUIRE((BinaryErrorCode::get_category() == failure_error_code.category())); REQUIRE((cBinaryTestErrorCategoryName == failure_error_code.category().name())); + std::for_each( + cFailureConditions.cbegin(), + cFailureConditions.cend(), + [&](auto failure_condition) { REQUIRE((failure_error_code == failure_condition)); } + ); + std::for_each( + cNoneFailureConditions.cbegin(), + cNoneFailureConditions.cend(), + [&](auto none_failure_condition) { + REQUIRE((failure_error_code != none_failure_condition)); + } + ); REQUIRE((success_error_code != failure_error_code)); REQUIRE((success_error_code.category() == failure_error_code.category())); From b7401a051e675dee6831adc6bec3b423e6d2a89d Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Fri, 19 Jul 2024 17:26:26 -0400 Subject: [PATCH 04/11] Add cmake to make error_handling as a library --- components/core/.clang-format | 4 ++-- components/core/CMakeLists.txt | 2 +- components/core/tests/test-error_handling.cpp | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/components/core/.clang-format b/components/core/.clang-format index c8e66579c..dc676288e 100644 --- a/components/core/.clang-format +++ b/components/core/.clang-format @@ -74,8 +74,8 @@ IncludeCategories: # NOTE: A header is grouped by first matching regex # Library headers. Update when adding new libraries. # NOTE: clang-format retains leading white-space on a line in violation of the YAML spec. - - Regex: "<(absl|antlr4|archive|boost|bsoncxx|catch2|curl|date|fmt|json|log_surgeon|mariadb\ -|mongocxx|msgpack|outcome|simdjson|spdlog|sqlite3|string_utils|yaml-cpp|zstd)" + - Regex: "<(absl|antlr4|archive|boost|bsoncxx|catch2|curl|date|error_handling|fmt|json\ +|log_surgeon|mariadb|mongocxx|msgpack|outcome|simdjson|spdlog|sqlite3|string_utils|yaml-cpp|zstd)" Priority: 3 # C system headers - Regex: "^<.+\\.h>" diff --git a/components/core/CMakeLists.txt b/components/core/CMakeLists.txt index c271fe8f1..4e212ef31 100644 --- a/components/core/CMakeLists.txt +++ b/components/core/CMakeLists.txt @@ -209,6 +209,7 @@ set(sqlite_DYNAMIC_LIBS "dl;m;pthread") include(cmake/Modules/FindLibraryDependencies.cmake) FindDynamicLibraryDependencies(sqlite "${sqlite_DYNAMIC_LIBS}") +add_subdirectory(src/clp/error_handling) add_subdirectory(src/clp/regex_utils) add_subdirectory(src/clp/string_utils) @@ -293,7 +294,6 @@ set(SOURCE_FILES_unitTest src/clp/DictionaryEntry.hpp src/clp/DictionaryReader.hpp src/clp/DictionaryWriter.hpp - src/clp/error_handling/ErrorCode.hpp src/clp/EncodedVariableInterpreter.cpp src/clp/EncodedVariableInterpreter.hpp src/clp/ErrorCode.hpp diff --git a/components/core/tests/test-error_handling.cpp b/components/core/tests/test-error_handling.cpp index f6e68ca91..263878d31 100644 --- a/components/core/tests/test-error_handling.cpp +++ b/components/core/tests/test-error_handling.cpp @@ -7,8 +7,7 @@ #include #include - -#include "../src/clp/error_handling/ErrorCode.hpp" +#include using clp::error_handling::ErrorCategory; using clp::error_handling::ErrorCode; From c9be6c6d71e7810967d36087c3f0a8fd2c975175 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Fri, 19 Jul 2024 17:27:28 -0400 Subject: [PATCH 05/11] Forgot to commit cmake... --- components/core/src/clp/error_handling/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 components/core/src/clp/error_handling/CMakeLists.txt diff --git a/components/core/src/clp/error_handling/CMakeLists.txt b/components/core/src/clp/error_handling/CMakeLists.txt new file mode 100644 index 000000000..10da86807 --- /dev/null +++ b/components/core/src/clp/error_handling/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(error_handling INTERFACE) +add_library(clp::error_handling ALIAS error_handling) From 1d99cb1521152c94ccd3fc5014b56740271bc67a Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Fri, 19 Jul 2024 17:31:48 -0400 Subject: [PATCH 06/11] Minor fix --- components/core/tests/test-error_handling.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/components/core/tests/test-error_handling.cpp b/components/core/tests/test-error_handling.cpp index 263878d31..2180e818e 100644 --- a/components/core/tests/test-error_handling.cpp +++ b/components/core/tests/test-error_handling.cpp @@ -31,8 +31,8 @@ using BinaryErrorCategory = ErrorCategory; constexpr string_view cAlwaysSuccessErrorCategoryName{"Always Success Error Code"}; constexpr string_view cBinaryTestErrorCategoryName{"Binary Error Code"}; -constexpr string_view cSuccessErrMsg{"Success"}; -constexpr string_view cFailureErrMsg{"Failure"}; +constexpr string_view cSuccessErrorMsg{"Success"}; +constexpr string_view cFailureErrorMsg{"Failure"}; constexpr string_view cUnrecognizedErrorCode{"Unrecognized Error Code"}; constexpr std::array cFailureConditions{std::errc::not_connected, std::errc::timed_out}; constexpr std::array cNoneFailureConditions{std::errc::broken_pipe, std::errc::address_in_use}; @@ -55,7 +55,7 @@ template <> auto AlwaysSuccessErrorCategory::message(AlwaysSuccessErrorCodeEnum error_enum) const -> string { switch (error_enum) { case AlwaysSuccessErrorCodeEnum::Success: - return string{cSuccessErrMsg}; + return string{cSuccessErrorMsg}; default: return string{cUnrecognizedErrorCode}; } @@ -70,9 +70,9 @@ template <> auto BinaryErrorCategory::message(BinaryErrorCodeEnum error_enum) const -> string { switch (error_enum) { case BinaryErrorCodeEnum::Success: - return string{cSuccessErrMsg}; + return string{cSuccessErrorMsg}; case BinaryErrorCodeEnum::Failure: - return string{cFailureErrMsg}; + return string{cFailureErrorMsg}; default: return string{cUnrecognizedErrorCode}; } @@ -100,14 +100,14 @@ TEST_CASE("test_error_code_implementation", "[error_handling][ErrorCode]") { BinaryErrorCode const success{BinaryErrorCodeEnum::Success}; std::error_code const success_error_code{success}; REQUIRE((success == success_error_code)); - REQUIRE((cSuccessErrMsg == success_error_code.message())); + REQUIRE((cSuccessErrorMsg == success_error_code.message())); REQUIRE((BinaryErrorCode::get_category() == success_error_code.category())); REQUIRE((cBinaryTestErrorCategoryName == success_error_code.category().name())); BinaryErrorCode const failure{BinaryErrorCodeEnum::Failure}; std::error_code const failure_error_code{failure}; REQUIRE((failure == failure_error_code)); - REQUIRE((cFailureErrMsg == failure_error_code.message())); + REQUIRE((cFailureErrorMsg == failure_error_code.message())); REQUIRE((BinaryErrorCode::get_category() == failure_error_code.category())); REQUIRE((cBinaryTestErrorCategoryName == failure_error_code.category().name())); std::for_each( @@ -129,7 +129,7 @@ TEST_CASE("test_error_code_implementation", "[error_handling][ErrorCode]") { AlwaysSuccessErrorCode const always_success{AlwaysSuccessErrorCodeEnum::Success}; std::error_code const always_success_error_code{always_success}; REQUIRE((always_success_error_code == always_success)); - REQUIRE((cSuccessErrMsg == always_success_error_code.message())); + REQUIRE((cSuccessErrorMsg == always_success_error_code.message())); REQUIRE((AlwaysSuccessErrorCode::get_category() == always_success_error_code.category())); REQUIRE((cAlwaysSuccessErrorCategoryName == always_success_error_code.category().name())); From 322d2cfe120d7828ed0b1eddb624c930b6f66324 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Fri, 19 Jul 2024 19:55:52 -0400 Subject: [PATCH 07/11] Update cmakle --- components/core/src/clp/error_handling/CMakeLists.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/core/src/clp/error_handling/CMakeLists.txt b/components/core/src/clp/error_handling/CMakeLists.txt index 10da86807..16a23625d 100644 --- a/components/core/src/clp/error_handling/CMakeLists.txt +++ b/components/core/src/clp/error_handling/CMakeLists.txt @@ -1,2 +1,8 @@ -add_library(error_handling INTERFACE) +set( + ERROR_HANDLING_HEADER_LIST + "ErrorCode.hpp" +) +add_library(error_handling INTERFACE ${ERROR_HANDLING_HEADER_LIST}) add_library(clp::error_handling ALIAS error_handling) +target_include_directories(error_handling INTERFACE ../) +target_compile_features(error_handling INTERFACE cxx_std_20) From 8b9177159f15c77bb13a54ca6fab12073fc5ba56 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Sat, 20 Jul 2024 17:01:58 -0400 Subject: [PATCH 08/11] Update cmake to use target_sources --- components/core/src/clp/error_handling/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/core/src/clp/error_handling/CMakeLists.txt b/components/core/src/clp/error_handling/CMakeLists.txt index 16a23625d..612987a14 100644 --- a/components/core/src/clp/error_handling/CMakeLists.txt +++ b/components/core/src/clp/error_handling/CMakeLists.txt @@ -2,7 +2,8 @@ set( ERROR_HANDLING_HEADER_LIST "ErrorCode.hpp" ) -add_library(error_handling INTERFACE ${ERROR_HANDLING_HEADER_LIST}) +add_library(error_handling INTERFACE) add_library(clp::error_handling ALIAS error_handling) +target_sources(error_handling INTERFACE ${ERROR_HANDLING_HEADER_LIST}) target_include_directories(error_handling INTERFACE ../) target_compile_features(error_handling INTERFACE cxx_std_20) From 82233a44a1ef4d195325c8e6cc3a82665e165f80 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Tue, 30 Jul 2024 19:22:42 -0400 Subject: [PATCH 09/11] Add macro --- .../core/src/clp/error_handling/ErrorCode.hpp | 13 +++++++++++++ components/core/tests/test-error_handling.cpp | 9 ++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/components/core/src/clp/error_handling/ErrorCode.hpp b/components/core/src/clp/error_handling/ErrorCode.hpp index 1456edbf8..2612e7768 100644 --- a/components/core/src/clp/error_handling/ErrorCode.hpp +++ b/components/core/src/clp/error_handling/ErrorCode.hpp @@ -134,4 +134,17 @@ auto make_error_code(ErrorCode 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> : std::true_type { \ + static_assert(std::is_enum_v); \ + }; +// NOLINTEND(bugprone-macro-parentheses, cppcoreguidelines-macro-usage) + #endif // CLP_ERROR_HANDLING_ERRORCODE_HPP diff --git a/components/core/tests/test-error_handling.cpp b/components/core/tests/test-error_handling.cpp index 2180e818e..b5319663a 100644 --- a/components/core/tests/test-error_handling.cpp +++ b/components/core/tests/test-error_handling.cpp @@ -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 : std::true_type {}; - -template <> -struct is_error_code_enum : 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* { From a14fa78098d52c7ccb972b16270db6b36f69551c Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Tue, 26 Nov 2024 14:05:08 -0500 Subject: [PATCH 10/11] Remove the cmake --- components/core/src/clp/error_handling/CMakeLists.txt | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 components/core/src/clp/error_handling/CMakeLists.txt diff --git a/components/core/src/clp/error_handling/CMakeLists.txt b/components/core/src/clp/error_handling/CMakeLists.txt deleted file mode 100644 index 612987a14..000000000 --- a/components/core/src/clp/error_handling/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -set( - ERROR_HANDLING_HEADER_LIST - "ErrorCode.hpp" -) -add_library(error_handling INTERFACE) -add_library(clp::error_handling ALIAS error_handling) -target_sources(error_handling INTERFACE ${ERROR_HANDLING_HEADER_LIST}) -target_include_directories(error_handling INTERFACE ../) -target_compile_features(error_handling INTERFACE cxx_std_20) From cc6370a4092ad769e6e0b895d16cf12853bffe79 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Tue, 26 Nov 2024 14:27:39 -0500 Subject: [PATCH 11/11] Remove system error --- components/core/tests/test-error_handling.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/core/tests/test-error_handling.cpp b/components/core/tests/test-error_handling.cpp index b5319663a..2d640ed57 100644 --- a/components/core/tests/test-error_handling.cpp +++ b/components/core/tests/test-error_handling.cpp @@ -7,7 +7,8 @@ #include #include -#include + +#include "../src/clp/error_handling/ErrorCode.hpp" using clp::error_handling::ErrorCategory; using clp::error_handling::ErrorCode;