diff options
author | Sven Göthel <[email protected]> | 2024-05-19 17:01:45 +0200 |
---|---|---|
committer | Sven Göthel <[email protected]> | 2024-05-19 17:01:45 +0200 |
commit | dc12cbc34296c8f399fb90c74ad4d666bf705ad1 (patch) | |
tree | cbaa040f020993e954bc50a8fcad3dabf694dba1 /include/jau/math/math_error.hpp | |
parent | c4b0a992237c8355c85a7edb6a58b1f86221cb2d (diff) |
cleanup C++20: Cleanup and test Exception* OO tree, ensure only one std::exception OO tree instance is included (as they are not using virtual base classes)
Diffstat (limited to 'include/jau/math/math_error.hpp')
-rw-r--r-- | include/jau/math/math_error.hpp | 105 |
1 files changed, 75 insertions, 30 deletions
diff --git a/include/jau/math/math_error.hpp b/include/jau/math/math_error.hpp index fdd496f..4ecf3f5 100644 --- a/include/jau/math/math_error.hpp +++ b/include/jau/math/math_error.hpp @@ -43,62 +43,107 @@ namespace jau::math { */ /** Error types as specified by [C++ Math Error Handling](https://en.cppreference.com/w/cpp/numeric/math/math_errhandling) */ - enum class math_error_t { - /** See FE_INVALID */ + enum class math_error_t : uint16_t { + /** no math error */ + none = 0, + /** See FE_INVALID, i.e. MathDomainError, std::domain_error : std::logic_error */ invalid, - /** See FE_DIVBYZERO */ + /** See FE_DIVBYZERO, i.e. MathDivByZeroError, std::domain_error : std::logic_error*/ div_by_zero, - /** See FE_OVERFLOW */ + /** See FE_OVERFLOW, i.e. MathOverflowError, std::overflow_error : std::runtime_error */ overflow, - /** See FE_UNDERFLOW */ + /** See FE_UNDERFLOW, i.e. MathUnderflowError, std::underflow_error : std::runtime_error */ underflow, - /** See FE_INEXACT */ - inexact + /** See FE_INEXACT, i.e. MathInexactError, std::runtime_error */ + inexact, + /** undefined math error */ + undefined = 1U << 15, }; /** Returns std::string representation of math_error_t */ std::string to_string(const math_error_t v) noexcept; - class MathError : public RuntimeException { + class MathErrorBase : public ExceptionBase { private: math_error_t m_error; - public: - MathError(math_error_t err, std::string const& m, const char* file, int line) noexcept - : RuntimeException("MathError("+to_string(err)+")", m, file, line), m_error(err) {} - - math_error_t error() const noexcept; + protected: + MathErrorBase(math_error_t err, std::string const& m, const char* file, int line) noexcept + : ExceptionBase("MathError("+to_string(err)+")", m, file, line), m_error(err) {} + + public: + math_error_t error() const noexcept; }; - /** math_error_t::invalid */ - class MathDomainError : public MathError { + class MathRuntimeErrorBase : public MathErrorBase { + protected: + MathRuntimeErrorBase(math_error_t err, std::string const& m, const char* file, int line) noexcept + : MathErrorBase(err, m, file, line) {} + }; + + class MathError : public MathErrorBase, public std::exception { public: - MathDomainError(std::string const& m, const char* file, int line) noexcept - : MathError(math_error_t::invalid, m, file, line) {} + MathError(math_error_t err, std::string const& m, const char* file, int line) noexcept + : MathErrorBase(err, m, file, line), exception() {} + + const char* what() const noexcept override { + return whole_message().c_str(); + } }; - /** math_error_t::div_by_zero, i.e. pole error */ - class MathDivByZeroError : public MathError { + + /** math_error_t::inexact */ + class MathInexactError : public MathRuntimeErrorBase, public std::runtime_error { public: - MathDivByZeroError(std::string const& m, const char* file, int line) noexcept - : MathError(math_error_t::div_by_zero, m, file, line) {} + MathInexactError(std::string const& m, const char* file, int line) noexcept + : MathRuntimeErrorBase(math_error_t::inexact, m, file, line), runtime_error(whole_message()) {} + + const char* what() const noexcept override { + return whole_message().c_str(); + } }; + /** math_error_t::overflow */ - class MathOverflowError : public MathError { + class MathOverflowError : public MathRuntimeErrorBase, public std::overflow_error { public: MathOverflowError(std::string const& m, const char* file, int line) noexcept - : MathError(math_error_t::overflow, m, file, line) {} + : MathRuntimeErrorBase(math_error_t::overflow, m, file, line), overflow_error(whole_message()) {} + + const char* what() const noexcept override { + return whole_message().c_str(); + } }; + /** math_error_t::underflow */ - class MathUnderflowError : public MathError { + class MathUnderflowError : public MathRuntimeErrorBase, public std::underflow_error { public: MathUnderflowError(std::string const& m, const char* file, int line) noexcept - : MathError(math_error_t::underflow, m, file, line) {} + : MathRuntimeErrorBase(math_error_t::underflow, m, file, line), underflow_error(whole_message()) {} + + const char* what() const noexcept override { + return whole_message().c_str(); + } }; - /** math_error_t::inexact */ - class MathInexactError : public MathError { + + /** math_error_t::invalid */ + class MathDomainError : public MathErrorBase, public std::domain_error { + protected: + MathDomainError(math_error_t err, std::string const& m, const char* file, int line) noexcept + : MathErrorBase(err, m, file, line), domain_error(whole_message()) {} + public: - MathInexactError(std::string const& m, const char* file, int line) noexcept - : MathError(math_error_t::inexact, m, file, line) {} + MathDomainError(std::string const& m, const char* file, int line) noexcept + : MathErrorBase(math_error_t::invalid, m, file, line), domain_error(whole_message()) {} + + const char* what() const noexcept override { + return whole_message().c_str(); + } }; - + + /** math_error_t::div_by_zero, i.e. pole error */ + class MathDivByZeroError : public MathDomainError { + public: + MathDivByZeroError(std::string const& m, const char* file, int line) noexcept + : MathDomainError(math_error_t::div_by_zero, m, file, line) {} + }; + /**@}*/ } // namespace jau |