throw range exception via function call -> reduces code size

This commit is contained in:
Joachim Schoeberl 2024-07-16 10:18:16 +02:00
parent 304ce7364a
commit 63986a4e5f
2 changed files with 44 additions and 11 deletions

View File

@ -22,6 +22,21 @@ 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)
{
@ -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

View File

@ -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<typename T>
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; };
@ -89,18 +96,22 @@ 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"); }
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<decltype(a),size_t>() && std::is_same<decltype(b),size_t>()) \
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__)