summaryrefslogtreecommitdiff
path: root/modules/core/error.cpp
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2023-12-04 12:18:14 +0100
committerClaudius "keldu" Holeksa <mail@keldu.de>2023-12-04 12:18:14 +0100
commita14896f9ed209dd3f9597722e5a5697bd7dbf531 (patch)
tree089ca5cbbd206d1921f8f6b53292f5bc1902ca5c /modules/core/error.cpp
parent84ecdcbca9e55b1f57fbb832e12ff4fdbb86e7c9 (diff)
meta: Renamed folder containing source
Diffstat (limited to 'modules/core/error.cpp')
-rw-r--r--modules/core/error.cpp156
1 files changed, 156 insertions, 0 deletions
diff --git a/modules/core/error.cpp b/modules/core/error.cpp
new file mode 100644
index 0000000..360e628
--- /dev/null
+++ b/modules/core/error.cpp
@@ -0,0 +1,156 @@
+#include "error.h"
+
+namespace saw {
+error::error(error::code code_, bool is_critical__)
+ : error_code_{static_cast<error::code>(code_)}, is_critical_{is_critical__} {}
+
+error::error(error::code code_, bool is_critical__, const std::string_view &msg)
+ :
+ error_code_{static_cast<error::code>(code_)}
+ , is_critical_{is_critical__}, error_message_{msg}{}
+
+error::error(error &&error)
+ :
+ error_code_{std::move(error.error_code_)}
+ , is_critical_{std::move(error.is_critical_)}
+ , error_message_{std::move(error.error_message_)}{}
+
+const std::string_view error::get_category() const {
+ auto& reg = impl::get_error_registry();
+
+ auto eov = reg.search_category(error_code_);
+ SAW_ASSERT(eov.is_value()){
+ return "Error category not found. Report this error to the forstio maintainer";
+ }
+
+ return eov.get_value();
+}
+
+const std::string_view error::get_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 !this->is_type<err::no_error>();
+}
+
+bool error::is_critical() const {
+ return is_critical_;
+}
+
+bool error::is_recoverable() const {
+ return !is_critical_;
+}
+
+error error::copy_error() const {
+ auto copy_error_code = error_code_;
+ error error{copy_error_code, is_critical_};
+
+ 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::get_id() const { return error_code_; }
+
+namespace impl {
+error_registry& get_error_registry() {
+ static own<error_registry> reg = nullptr;
+ if(!reg){
+ reg = heap<error_registry>();
+ }
+
+ assert(reg);
+ return *reg;
+}
+}
+
+error no_error(){
+ return make_error<err::no_error>();
+}
+
+namespace impl {
+error_registry::error_registry():
+ infos_{
+ {
+ err::no_error::description,
+ err::no_error::is_critical
+ },
+ {
+ err::not_found::description,
+ err::not_found::is_critical
+ },
+ {
+ err::out_of_memory::description,
+ err::out_of_memory::is_critical
+ }
+ }
+{}
+
+error_or<const std::string_view> error_registry::search_category(const error::code& id) const {
+ if( id >= infos_.size()){
+ return make_error<err::not_found>();
+ }
+
+ return infos_.at(id).description;
+}
+
+error_or<error::code> error_registry::search_id(const std::string_view& desc)const{
+ /**
+ * Search the index in the vector
+ */
+ size_t i{};
+ size_t info_max_size = std::min<std::size_t>(infos_.size(), std::numeric_limits<error::code>::max());
+ for(i = 0; i < info_max_size; ++i){
+ if(infos_.at(i).description == desc){
+ break;
+ }
+ }
+
+ if(i == info_max_size){
+ return make_error<err::not_found>();
+ }
+
+ return static_cast<error::code>(i);
+}
+
+error_or<error::code> error_registry::search_or_register_id(const std::string_view& desc, bool is_critical){
+ auto err_or_id = search_id(desc);
+
+ if(err_or_id.is_value()){
+ return err_or_id.get_value();
+ }
+
+ auto& err = err_or_id.get_error();
+
+ if(err.is_type<err::not_found>()){
+ size_t new_index = infos_.size();
+ if(new_index == std::numeric_limits<error::code>::max()){
+ return make_error<err::out_of_memory>("Error registry ids are exhausted");
+ }
+ infos_.emplace_back(error_info{desc, is_critical});
+ return static_cast<error::code>(new_index);
+ }
+
+ return std::move(err);
+}
+}
+
+} // namespace saw