From 63986a4e5fe89440bc2eb554cf257ebd7fa0ae79 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Tue, 16 Jul 2024 10:18:16 +0200 Subject: [PATCH] throw range exception via function call -> reduces code size --- libsrc/core/exception.cpp | 22 ++++++++++++++++++++++ libsrc/core/exception.hpp | 33 ++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/libsrc/core/exception.cpp b/libsrc/core/exception.cpp index 9c99a138..9ad11ca9 100644 --- a/libsrc/core/exception.cpp +++ b/libsrc/core/exception.cpp @@ -22,7 +22,22 @@ namespace ngcore } + RangeException :: RangeException (// const std::string & where, + const char * where, + int ind, int imin, int imax) : Exception("") + { + std::stringstream str; + str << where << ": index " << ind << " out of range [" << imin << "," << imax << ")\n"; + Append (str.str()); + Append (GetBackTrace()); + } + + void ThrowRangeException(const char * s, int ind, int imin, int imax) + { + throw RangeException(s, ind, imin, imax); + } + void ThrowException(const std::string & s) { throw Exception (s); @@ -32,6 +47,13 @@ namespace ngcore { throw Exception (s); } + + + void ThrowNotTheSameException(const char * s, long int a, long int b) + { + throw ngcore::Exception(std::string(s) + ", a="+ToString(a) + ", b="+ToString(b) + GetBackTrace()); + } + } // namespace ngcore diff --git a/libsrc/core/exception.hpp b/libsrc/core/exception.hpp index 6cd5e4bf..c8b281e1 100644 --- a/libsrc/core/exception.hpp +++ b/libsrc/core/exception.hpp @@ -57,15 +57,18 @@ namespace ngcore { public: /// where it occurs, index, minimal and maximal indices - RangeException (const std::string & where, - int ind, int imin, int imax) : Exception("") + RangeException (// const std::string & where, + const char * where, + int ind, int imin, int imax); + /* + : Exception("") { std::stringstream str; str << where << ": index " << ind << " out of range [" << imin << "," << imax << ")\n"; Append (str.str()); Append (GetBackTrace()); } - + */ template RangeException(const std::string& where, const T& value) { @@ -75,6 +78,10 @@ namespace ngcore } }; + NGCORE_API void ThrowRangeException(const char * s, int ind, int imin, int imax); + NGCORE_API void ThrowNotTheSameException(const char * s, long int a, long int b); + + // Exception used if no simd implementation is available to fall back to standard evaluation class NGCORE_API ExceptionNOSIMD : public Exception { public: using Exception::Exception; }; @@ -88,19 +95,23 @@ namespace ngcore #if defined(NETGEN_ENABLE_CHECK_RANGE) && !defined(__CUDA_ARCH__) #define NETGEN_CHECK_RANGE(value, min, max_plus_one) \ - { if ((value)<(min) || (value)>=(max_plus_one)) \ - throw ngcore::RangeException(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t", int(value), int(min), int(max_plus_one)); } -#define NETGEN_CHECK_SHAPE(a,b) \ - { if(a.Shape() != b.Shape()) \ - throw ngcore::Exception(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t: shape don't match"); } + { if ((value)<(min) || (value)>=(max_plus_one)) \ + ThrowRangeException(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t", int(value), int(min), int(max_plus_one)); } +// #define NETGEN_CHECK_SHAPE(a,b) \ +// { if(a.Shape() != b.Shape()) \ +// ngcore::ThrowException(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t: shapes don't match"); } #define NETGEN_CHECK_SAME(a,b) \ - { if(a != b) \ - throw ngcore::Exception(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t: not the same, a="+ToString(a) + ", b="+ToString(b) + GetBackTrace()); } + { if(a != b) { \ + if constexpr(std::is_same() && std::is_same()) \ + ThrowNotTheSameException(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t: not the same, a=", long(a), long(b)); \ + else \ + throw ngcore::Exception(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t: not the same, a="+ToString(a) + ", b="+ToString(b) + GetBackTrace()); \ + } } #define NETGEN_NOEXCEPT #else // defined(NETGEN_ENABLE_CHECK_RANGE) && !defined(__CUDA_ARCH__) #define NETGEN_CHECK_RANGE(value, min, max) #define NETGEN_CHECK_SAME(a,b) -#define NETGEN_CHECK_SHAPE(a,b) +// #define NETGEN_CHECK_SHAPE(a,b) #define NETGEN_NOEXCEPT noexcept #endif // defined(NETGEN_ENABLE_CHECK_RANGE) && !defined(__CUDA_ARCH__)