diff options
author | Claudius Holeksa <mail@keldu.de> | 2023-04-18 18:34:29 +0200 |
---|---|---|
committer | Claudius Holeksa <mail@keldu.de> | 2023-04-18 18:34:29 +0200 |
commit | c4033b8c2028efeb9bac236e6b279bdcd8efec34 (patch) | |
tree | fa5a336d0870240604e9037c37e63414764be7e1 /forstio/core/error.cpp |
First commit to restructure the forstio repo
Diffstat (limited to 'forstio/core/error.cpp')
-rw-r--r-- | forstio/core/error.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/forstio/core/error.cpp b/forstio/core/error.cpp new file mode 100644 index 0000000..757e6a8 --- /dev/null +++ b/forstio/core/error.cpp @@ -0,0 +1,73 @@ +#include "error.h" + +namespace saw { +error::error() : error_{static_cast<error::code>(0)} {} + +error::error(const std::string_view &msg, error::code code) + : error_message_{msg}, error_{static_cast<error::code>(code)} {} + +error::error(std::string &&msg, error::code code) + : error_message_{std::move(msg)}, error_{static_cast<error::code>(code)} {} + +error::error(error &&error) + : error_message_{std::move(error.error_message_)}, error_{std::move( + error.error_)} {} + +const std::string_view error::message() const { + + return std::visit( + [this](auto &&arg) -> const std::string_view { + using T = std::decay_t<decltype(arg)>; + + if constexpr (std::is_same_v<T, std::string>) { + return std::string_view{arg}; + } else if constexpr (std::is_same_v<T, std::string_view>) { + return arg; + } else { + return "Error in class Error. Good luck :)"; + } + }, + error_message_); +} + +bool error::failed() const { + return static_cast<std::underlying_type_t<error::code>>(error_) != 0; +} + +bool error::is_critical() const { + return static_cast<std::underlying_type_t<error::code>>(error_) < 0; +} + +bool error::is_recoverable() const { + return static_cast<std::underlying_type_t<error::code>>(error_) > 0; +} + +error error::copy_error() const { + error error; + error.error_ = error_; + try { + error.error_message_ = error_message_; + } catch (const std::bad_alloc &) { + error.error_message_ = + std::string_view{"Error while copying Error string. Out of memory"}; + } + return error; +} + +error::code error::id() const { return static_cast<error::code>(error_); } + +error make_error(const std::string_view &generic, error::code code) { + return error{generic, code}; +} + +error critical_error(const std::string_view &generic, error::code c) { + return make_error(generic, c); +} + +error recoverable_error(const std::string_view &generic, error::code c) { + return make_error(generic, c); +} + +error no_error() { return error{}; } + +} // namespace saw |