From 7176c5ecd32cfe1b0efa41a972476fa0caf5eb1c Mon Sep 17 00:00:00 2001 From: Lin Zhihao <59785146+LinZhihao-723@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:44:26 -0500 Subject: [PATCH] feat(ffi): Add initial implementation of `IrErrorCode` (using the `ErrorCode` template) which will replace the `IRErrorCode` enum. (#623) --- components/core/CMakeLists.txt | 2 ++ .../src/clp/ffi/ir_stream/IrErrorCode.cpp | 26 +++++++++++++++++++ .../src/clp/ffi/ir_stream/IrErrorCode.hpp | 24 +++++++++++++++++ components/core/tests/test-error_handling.cpp | 15 +++++++++++ 4 files changed, 67 insertions(+) create mode 100644 components/core/src/clp/ffi/ir_stream/IrErrorCode.cpp create mode 100644 components/core/src/clp/ffi/ir_stream/IrErrorCode.hpp diff --git a/components/core/CMakeLists.txt b/components/core/CMakeLists.txt index 193d167d8..632d31afb 100644 --- a/components/core/CMakeLists.txt +++ b/components/core/CMakeLists.txt @@ -382,6 +382,8 @@ set(SOURCE_FILES_unitTest src/clp/ffi/ir_stream/decoding_methods.inc src/clp/ffi/ir_stream/encoding_methods.cpp src/clp/ffi/ir_stream/encoding_methods.hpp + src/clp/ffi/ir_stream/IrErrorCode.cpp + src/clp/ffi/ir_stream/IrErrorCode.hpp src/clp/ffi/ir_stream/IrUnitHandlerInterface.hpp src/clp/ffi/ir_stream/IrUnitType.hpp src/clp/ffi/ir_stream/ir_unit_deserialization_methods.cpp diff --git a/components/core/src/clp/ffi/ir_stream/IrErrorCode.cpp b/components/core/src/clp/ffi/ir_stream/IrErrorCode.cpp new file mode 100644 index 000000000..f9a00ca1e --- /dev/null +++ b/components/core/src/clp/ffi/ir_stream/IrErrorCode.cpp @@ -0,0 +1,26 @@ +#include "IrErrorCode.hpp" + +#include + +using IrErrorCategory = clp::error_handling::ErrorCategory; +using clp::ffi::ir_stream::IrErrorCodeEnum; + +template <> +auto IrErrorCategory::name() const noexcept -> char const* { + return "clp::ffi::ir_stream::IrErrorCode"; +} + +template <> +auto IrErrorCategory::message(IrErrorCodeEnum error_enum) const -> std::string { + switch (error_enum) { + case IrErrorCodeEnum::DecodingMethodFailure: + return "The decoding method failed."; + case IrErrorCodeEnum::EndOfStream: + return "The end-of-stream IR unit has already been consumed."; + case IrErrorCodeEnum::IncompleteStream: + return "The IR stream ended with a truncated IR unit or did not terminate with an " + "end-of-stream IR unit."; + default: + return "Unknown error code enum."; + } +} diff --git a/components/core/src/clp/ffi/ir_stream/IrErrorCode.hpp b/components/core/src/clp/ffi/ir_stream/IrErrorCode.hpp new file mode 100644 index 000000000..8eaad4e16 --- /dev/null +++ b/components/core/src/clp/ffi/ir_stream/IrErrorCode.hpp @@ -0,0 +1,24 @@ +#ifndef CLP_IRERRORCODE_HPP +#define CLP_IRERRORCODE_HPP + +#include + +#include "../../error_handling/ErrorCode.hpp" + +namespace clp::ffi::ir_stream { +/** + * This enum class represents all possible error codes related to serializing or deserializing CLP + * IR streams. + */ +enum class IrErrorCodeEnum : uint8_t { + DecodingMethodFailure, + EndOfStream, + IncompleteStream, +}; + +using IrErrorCode = clp::error_handling::ErrorCode; +} // namespace clp::ffi::ir_stream + +CLP_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(clp::ffi::ir_stream::IrErrorCodeEnum); + +#endif // CLP_IRERRORCODE_HPP diff --git a/components/core/tests/test-error_handling.cpp b/components/core/tests/test-error_handling.cpp index 2d640ed57..44327c833 100644 --- a/components/core/tests/test-error_handling.cpp +++ b/components/core/tests/test-error_handling.cpp @@ -9,6 +9,7 @@ #include #include "../src/clp/error_handling/ErrorCode.hpp" +#include "../src/clp/ffi/ir_stream/IrErrorCode.hpp" using clp::error_handling::ErrorCategory; using clp::error_handling::ErrorCode; @@ -139,3 +140,17 @@ TEST_CASE("test_error_code_implementation", "[error_handling][ErrorCode]") { REQUIRE((AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} != success_error_code)); REQUIRE((BinaryErrorCode{BinaryErrorCodeEnum::Success} != always_success_error_code)); } + +TEST_CASE("test_ir_error_code", "[error_handling][ErrorCode][IrErrorCode]") { + using clp::ffi::ir_stream::IrErrorCode; + using clp::ffi::ir_stream::IrErrorCodeEnum; + + auto assert_error_code_matches_error_code_enum = [](IrErrorCodeEnum error_code_enum) -> bool { + std::error_code const error_code{IrErrorCode{error_code_enum}}; + return error_code == IrErrorCode{error_code_enum}; + }; + + REQUIRE(assert_error_code_matches_error_code_enum(IrErrorCodeEnum::DecodingMethodFailure)); + REQUIRE(assert_error_code_matches_error_code_enum(IrErrorCodeEnum::EndOfStream)); + REQUIRE(assert_error_code_matches_error_code_enum(IrErrorCodeEnum::IncompleteStream)); +}