Skip to content

Commit 7176c5e

Browse files
feat(ffi): Add initial implementation of IrErrorCode (using the ErrorCode template) which will replace the IRErrorCode enum. (#623)
1 parent 2160362 commit 7176c5e

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

components/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ set(SOURCE_FILES_unitTest
382382
src/clp/ffi/ir_stream/decoding_methods.inc
383383
src/clp/ffi/ir_stream/encoding_methods.cpp
384384
src/clp/ffi/ir_stream/encoding_methods.hpp
385+
src/clp/ffi/ir_stream/IrErrorCode.cpp
386+
src/clp/ffi/ir_stream/IrErrorCode.hpp
385387
src/clp/ffi/ir_stream/IrUnitHandlerInterface.hpp
386388
src/clp/ffi/ir_stream/IrUnitType.hpp
387389
src/clp/ffi/ir_stream/ir_unit_deserialization_methods.cpp
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "IrErrorCode.hpp"
2+
3+
#include <string>
4+
5+
using IrErrorCategory = clp::error_handling::ErrorCategory<clp::ffi::ir_stream::IrErrorCodeEnum>;
6+
using clp::ffi::ir_stream::IrErrorCodeEnum;
7+
8+
template <>
9+
auto IrErrorCategory::name() const noexcept -> char const* {
10+
return "clp::ffi::ir_stream::IrErrorCode";
11+
}
12+
13+
template <>
14+
auto IrErrorCategory::message(IrErrorCodeEnum error_enum) const -> std::string {
15+
switch (error_enum) {
16+
case IrErrorCodeEnum::DecodingMethodFailure:
17+
return "The decoding method failed.";
18+
case IrErrorCodeEnum::EndOfStream:
19+
return "The end-of-stream IR unit has already been consumed.";
20+
case IrErrorCodeEnum::IncompleteStream:
21+
return "The IR stream ended with a truncated IR unit or did not terminate with an "
22+
"end-of-stream IR unit.";
23+
default:
24+
return "Unknown error code enum.";
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef CLP_IRERRORCODE_HPP
2+
#define CLP_IRERRORCODE_HPP
3+
4+
#include <cstdint>
5+
6+
#include "../../error_handling/ErrorCode.hpp"
7+
8+
namespace clp::ffi::ir_stream {
9+
/**
10+
* This enum class represents all possible error codes related to serializing or deserializing CLP
11+
* IR streams.
12+
*/
13+
enum class IrErrorCodeEnum : uint8_t {
14+
DecodingMethodFailure,
15+
EndOfStream,
16+
IncompleteStream,
17+
};
18+
19+
using IrErrorCode = clp::error_handling::ErrorCode<IrErrorCodeEnum>;
20+
} // namespace clp::ffi::ir_stream
21+
22+
CLP_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(clp::ffi::ir_stream::IrErrorCodeEnum);
23+
24+
#endif // CLP_IRERRORCODE_HPP

components/core/tests/test-error_handling.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <Catch2/single_include/catch2/catch.hpp>
1010

1111
#include "../src/clp/error_handling/ErrorCode.hpp"
12+
#include "../src/clp/ffi/ir_stream/IrErrorCode.hpp"
1213

1314
using clp::error_handling::ErrorCategory;
1415
using clp::error_handling::ErrorCode;
@@ -139,3 +140,17 @@ TEST_CASE("test_error_code_implementation", "[error_handling][ErrorCode]") {
139140
REQUIRE((AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} != success_error_code));
140141
REQUIRE((BinaryErrorCode{BinaryErrorCodeEnum::Success} != always_success_error_code));
141142
}
143+
144+
TEST_CASE("test_ir_error_code", "[error_handling][ErrorCode][IrErrorCode]") {
145+
using clp::ffi::ir_stream::IrErrorCode;
146+
using clp::ffi::ir_stream::IrErrorCodeEnum;
147+
148+
auto assert_error_code_matches_error_code_enum = [](IrErrorCodeEnum error_code_enum) -> bool {
149+
std::error_code const error_code{IrErrorCode{error_code_enum}};
150+
return error_code == IrErrorCode{error_code_enum};
151+
};
152+
153+
REQUIRE(assert_error_code_matches_error_code_enum(IrErrorCodeEnum::DecodingMethodFailure));
154+
REQUIRE(assert_error_code_matches_error_code_enum(IrErrorCodeEnum::EndOfStream));
155+
REQUIRE(assert_error_code_matches_error_code_enum(IrErrorCodeEnum::IncompleteStream));
156+
}

0 commit comments

Comments
 (0)