diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2014-03-05 12:24:14 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2014-03-05 10:46:46 -0600 |
commit | f51f03a46102333bac6a7fe06bc8538492f413a5 (patch) | |
tree | 06f959080e33f18a1ffb3dc05417f2b83cd17253 /include/tools | |
parent | 0e78ce148371f8ae27f4660403dd057a1f632ecf (diff) |
Do not needlessly truncate MinMax argument before comparison with bounds
Change-Id: I218e70d6a19901107fd037af255ad29692c850d4
Reviewed-on: https://gerrit.libreoffice.org/8461
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Tested-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include/tools')
-rw-r--r-- | include/tools/helpers.hxx | 34 |
1 files changed, 32 insertions, 2 deletions
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 <sal/config.h> + +#include <cassert> + +#include <boost/mpl/or.hpp> +#include <boost/type_traits/is_floating_point.hpp> +#include <boost/type_traits/is_signed.hpp> +#include <boost/type_traits/is_unsigned.hpp> +#include <boost/utility/enable_if.hpp> + +template<typename T> +inline +typename boost::enable_if< + boost::mpl::or_< boost::is_signed<T>, boost::is_floating_point<T> >, long> + ::type +MinMax(T nVal, long nMin, long nMax) +{ + assert(nMin <= nMax); + return nVal >= nMin + ? (nVal <= nMax ? static_cast<long>(nVal) : nMax) : nMin; +} + +template<typename T> +inline typename boost::enable_if<boost::is_unsigned<T>, long>::type MinMax( + T nVal, long nMin, long nMax) { - return nVal >= nMin ? ( nVal <= nMax ? nVal : nMax ) : nMin; + assert(nMin <= nMax); + return nMax < 0 + ? nMax + : ((nMin < 0 || nVal >= static_cast<unsigned long>(nMin)) + ? (nVal <= static_cast<unsigned long>(nMax) + ? static_cast<long>(nVal) : nMax) + : nMin); } inline long AlignedWidth4Bytes( long nWidthBits ) |