[Libreoffice-commits] core.git: include/tools

Stephan Bergmann sbergman at redhat.com
Wed Mar 5 08:47:15 PST 2014


 include/tools/helpers.hxx |   34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

New commits:
commit f51f03a46102333bac6a7fe06bc8538492f413a5
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Wed Mar 5 12:24:14 2014 +0100

    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 at redhat.com>
    Tested-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/include/tools/helpers.hxx b/include/tools/helpers.hxx
index c3cb88f..8b04050 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 )


More information about the Libreoffice-commits mailing list