Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ffi): Add initial implementation of IrErrorCode (using the ErrorCode template) which will replace the IRErrorCode enum. #623

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 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,26 @@
#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 methods failed.";
LinZhihao-723 marked this conversation as resolved.
Show resolved Hide resolved
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.";
}
}
24 changes: 24 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,24 @@
#ifndef CLP_IRERRORCODE_HPP
#define CLP_IRERRORCODE_HPP

#include <cstdint>

#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<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));
}
LinZhihao-723 marked this conversation as resolved.
Show resolved Hide resolved
Loading