From e2a20a44bccf9de70db5ff55d4b39f60c339241f Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 20 Nov 2024 20:52:54 +0100 Subject: [PATCH] Put IsSafe to ngcore namespace, separate functions for range check macros for readability --- libsrc/core/exception.hpp | 46 +++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/libsrc/core/exception.hpp b/libsrc/core/exception.hpp index a9ca7166..72d0ce9c 100644 --- a/libsrc/core/exception.hpp +++ b/libsrc/core/exception.hpp @@ -6,6 +6,7 @@ #include // for string #include "ngcore_api.hpp" // for NGCORE_API +#include "utils.hpp" // for ToString namespace ngcore @@ -91,6 +92,33 @@ namespace ngcore // Exception used if no simd implementation is available to fall back to standard evaluation class NGCORE_API ExceptionNOSIMD : public Exception { public: using Exception::Exception; }; + + template + struct IsSafe { + constexpr operator bool() const { return false; } }; + + namespace detail { + template + inline static void CheckRange(const char * s, const T& n, int first, int next) + { + if constexpr (!IsSafe()) + if (n=next) + ThrowRangeException(s, int(n), first, next); + } + + template + inline static void CheckSame(const char * s, const Ta& a, const Tb& b) + { + if constexpr (!IsSafe() || !IsSafe()) + if(a != b) + { + if constexpr(std::is_same() && std::is_same()) + ThrowNotTheSameException(s, long(a), long(b)); \ + else + throw Exception(std::string(s) + "\t: not the same"+ToString(a) + ", b="+ngcore::ToString(b) + GetBackTrace()); + } + } + } // namespace detail } // namespace ngcore #define NETGEN_CORE_NGEXEPTION_STR_HELPER(x) #x @@ -99,24 +127,10 @@ namespace ngcore // Convenience macro to append file name and line of exception origin to the string #define NG_EXCEPTION(s) ngcore::Exception(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t"+std::string(s)) - -template -struct IsSafe { - constexpr operator bool() const { return false; } }; - #if defined(NETGEN_ENABLE_CHECK_RANGE) && !defined(__CUDA_ARCH__) -#define NETGEN_CHECK_RANGE(value, min, max_plus_one) \ - { if constexpr (!IsSafe()) { \ - 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_RANGE(value, min, max_plus_one) ngcore::detail::CheckRange(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t", value, int(min), int(max_plus_one)); +#define NETGEN_CHECK_SAME(a,b) ngcore::detail::CheckSame(__FILE__ ":" NETGEN_CORE_NGEXEPTION_STR(__LINE__) "\t", a, b); -#define NETGEN_CHECK_SAME(a,b) \ - { 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)