COMMON: Use assert for CLIP() if bounds are not properly ordered (#2298)

COMMON: Use assert in debug builds to prevent bad ordering of bounds

The only macro to check against for debug vs release builds that I found is RELEASE_BUILD
This commit is contained in:
Antoniou Athanasios 2020-06-06 17:53:47 +03:00 committed by GitHub
parent b53a7db0da
commit c2a12908c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,7 +49,18 @@ template<typename T> inline T ABS(T x) { return (x >= 0) ? x : -x; }
template<typename T> inline T MIN(T a, T b) { return (a < b) ? a : b; }
template<typename T> inline T MAX(T a, T b) { return (a > b) ? a : b; }
template<typename T> inline T CLIP(T v, T amin, T amax)
{ if (v < amin) return amin; else if (v > amax) return amax; else return v; }
{
#if !defined(RELEASE_BUILD)
// debug builds use this assert to pinpoint
// any problematic cases, where amin and amax
// are incorrectly ordered
// and thus CLIP() would return an invalid result
assert(amin <= amax);
#endif
if (v < amin) return amin;
else if (v > amax) return amax;
return v;
}
/**
* Template method which swaps the values of its two parameters.