diff --git a/include/tools/helpers.hxx b/include/tools/helpers.hxx index c3cb88f3e348..8b04050bc918 100644 --- a/include/tools/helpers.hxx +++ b/include/tools/helpers.hxx @@ -9,9 +9,39 @@ #ifndef INCLUDED_TOOLS_HELPERS_HXX #define INCLUDED_TOOLS_HELPERS_HXX -inline long MinMax( long nVal, long nMin, long nMax ) +#include + +#include + +#include +#include +#include +#include +#include + +template +inline +typename boost::enable_if< + boost::mpl::or_< boost::is_signed, boost::is_floating_point >, long> + ::type +MinMax(T nVal, long nMin, long nMax) { - return nVal >= nMin ? ( nVal <= nMax ? nVal : nMax ) : nMin; + assert(nMin <= nMax); + return nVal >= nMin + ? (nVal <= nMax ? static_cast(nVal) : nMax) : nMin; +} + +template +inline typename boost::enable_if, long>::type MinMax( + T nVal, long nMin, long nMax) +{ + assert(nMin <= nMax); + return nMax < 0 + ? nMax + : ((nMin < 0 || nVal >= static_cast(nMin)) + ? (nVal <= static_cast(nMax) + ? static_cast(nVal) : nMax) + : nMin); } inline long AlignedWidth4Bytes( long nWidthBits )