From 17aed9770c44e592d3a17969987e2280b671622b Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Tue, 3 Dec 2024 19:05:28 -0500 Subject: [PATCH] Add starter code for IrErrorCode --- components/core/CMakeLists.txt | 2 ++ .../src/clp/ffi/ir_stream/IrErrorCode.cpp | 25 +++++++++++++++++++ .../src/clp/ffi/ir_stream/IrErrorCode.hpp | 23 +++++++++++++++++ components/core/tests/test-error_handling.cpp | 15 +++++++++++ 4 files changed, 65 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..43ad29354 --- /dev/null +++ b/components/core/src/clp/ffi/ir_stream/IrErrorCode.cpp @@ -0,0 +1,25 @@ +#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 "Decoding method failure."; + case IrErrorCodeEnum::EndOfStream: + return "End-of-stream has been reached."; + case IrErrorCodeEnum::IncompleteStream: + return "Incomplete IR stream."; + 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..97ca51453 --- /dev/null +++ b/components/core/src/clp/ffi/ir_stream/IrErrorCode.hpp @@ -0,0 +1,23 @@ +#ifndef CLP_IRERRORCODE_HPP +#define CLP_IRERRORCODE_HPP + +#include + +#include "../../error_handling/ErrorCode.hpp" + +namespace clp::ffi::ir_stream { +/** + * Enums that define all + */ +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)); +}