forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iox-eclipse-iceoryx#1032 Add ErrorProxy
Signed-off-by: Matthias Killat <[email protected]>
- Loading branch information
1 parent
4bc23ed
commit 9fe1c83
Showing
5 changed files
with
164 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/proxy.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#pragma once | ||
|
||
#include "error_codes.hpp" // this must be provided by the module | ||
#include "error_levels.hpp" // do we want this dependency? | ||
#include "location.hpp" | ||
|
||
#include <iostream> | ||
#include <type_traits> | ||
|
||
namespace eh | ||
{ | ||
void terminate() | ||
{ | ||
std::cout << "TERMINATE" << std::endl; | ||
} | ||
|
||
template <class T> | ||
struct is_fatal | ||
{ | ||
static constexpr bool value = std::is_same<T, Fatal_t>::value; | ||
}; | ||
|
||
// alternatively map to default error code | ||
template <class Level> | ||
void report(const SourceLocation& location, Level level) | ||
{ | ||
auto name = error_level_to_name(level); | ||
std::cout << name << "@" << location.file << " " << location.line << " " << location.function << std::endl; | ||
} | ||
|
||
template <class Level, class Code> | ||
void report(const SourceLocation& location, Level level, Code code) | ||
{ | ||
auto levelName = error_level_to_name(level); | ||
auto codeName = error_code_to_name(code); | ||
std::cout << levelName << "@" << location.file << " " << location.line << " " << location.function << " : " | ||
<< codeName << std::endl; | ||
} | ||
|
||
template <class Level> | ||
void report(const SourceLocation& location, Level level, error_code_t code) | ||
{ | ||
auto levelName = error_level_to_name(level); | ||
auto codeName = error_code_to_name(code); | ||
std::cout << levelName << "@" << location.file << " " << location.line << " " << location.function << " : " | ||
<< codeName << std::endl; | ||
} | ||
|
||
// kind of an exception | ||
template <class Level> | ||
struct ErrorProxy | ||
{ | ||
ErrorProxy() | ||
{ | ||
} | ||
|
||
ErrorProxy(const SourceLocation& location, Level level) | ||
: location(location) | ||
, level(level) | ||
, code(0) // fix magic number for no code | ||
{ | ||
error = true; | ||
} | ||
|
||
template <class Code> | ||
ErrorProxy(const SourceLocation& location, Level level, Code code) | ||
: location(location) | ||
, level(level) | ||
, code(error_code_to_num(code)) | ||
{ | ||
error = true; | ||
} | ||
|
||
ErrorProxy(const ErrorProxy&) = delete; | ||
|
||
ErrorProxy(ErrorProxy&&) | ||
{ | ||
// should not be used (exists for RVO in C++14) | ||
std::terminate(); | ||
} | ||
|
||
~ErrorProxy() | ||
{ | ||
if (error) | ||
{ | ||
// can be compile time dispatched later | ||
if (code > 0) | ||
{ | ||
report(location, level, code); | ||
} | ||
else | ||
{ | ||
report(location, level); | ||
} | ||
if (is_fatal<Level>::value) | ||
{ | ||
terminate(); | ||
} | ||
} | ||
} | ||
|
||
SourceLocation location; | ||
Level level; | ||
error_code_t code; | ||
bool error{false}; | ||
}; | ||
|
||
} // namespace eh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,5 +40,4 @@ TEST(EH_test, assert) | |
IOX_ASSERT(x < 10, ModuleError::OutOfBounds); | ||
IOX_ASSERT(x < 10); | ||
} | ||
|
||
} // namespace |