Skip to content

Commit

Permalink
Add starter code for IrErrorCode
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Dec 4, 2024
1 parent ec0821d commit 17aed97
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions components/core/src/clp/ffi/ir_stream/IrErrorCode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "IrErrorCode.hpp"

#include <string>

using IrErrorCategory = clp::error_handling::ErrorCategory<clp::ffi::ir_stream::IrErrorCodeEnum>;
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.";
}
}
23 changes: 23 additions & 0 deletions components/core/src/clp/ffi/ir_stream/IrErrorCode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef CLP_IRERRORCODE_HPP
#define CLP_IRERRORCODE_HPP

#include <cstdint>

#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<IrErrorCodeEnum>;
} // namespace clp::ffi::ir_stream

CLP_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(clp::ffi::ir_stream::IrErrorCodeEnum);

#endif // CLP_IRERRORCODE_HPP
15 changes: 15 additions & 0 deletions components/core/tests/test-error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <Catch2/single_include/catch2/catch.hpp>

#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;
Expand Down Expand Up @@ -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));
}

0 comments on commit 17aed97

Please sign in to comment.