Branch libreoffice-5-0-4
[LibreOffice.git] / include / tools / helpers.hxx
blob8b04050bc91878a690fc80e5c7098910ed888c9a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 #ifndef INCLUDED_TOOLS_HELPERS_HXX
10 #define INCLUDED_TOOLS_HELPERS_HXX
12 #include <sal/config.h>
14 #include <cassert>
16 #include <boost/mpl/or.hpp>
17 #include <boost/type_traits/is_floating_point.hpp>
18 #include <boost/type_traits/is_signed.hpp>
19 #include <boost/type_traits/is_unsigned.hpp>
20 #include <boost/utility/enable_if.hpp>
22 template<typename T>
23 inline
24 typename boost::enable_if<
25 boost::mpl::or_< boost::is_signed<T>, boost::is_floating_point<T> >, long>
26 ::type
27 MinMax(T nVal, long nMin, long nMax)
29 assert(nMin <= nMax);
30 return nVal >= nMin
31 ? (nVal <= nMax ? static_cast<long>(nVal) : nMax) : nMin;
34 template<typename T>
35 inline typename boost::enable_if<boost::is_unsigned<T>, long>::type MinMax(
36 T nVal, long nMin, long nMax)
38 assert(nMin <= nMax);
39 return nMax < 0
40 ? nMax
41 : ((nMin < 0 || nVal >= static_cast<unsigned long>(nMin))
42 ? (nVal <= static_cast<unsigned long>(nMax)
43 ? static_cast<long>(nVal) : nMax)
44 : nMin);
47 inline long AlignedWidth4Bytes( long nWidthBits )
49 return ( ( nWidthBits + 31 ) >> 5 ) << 2;
52 inline long FRound( double fVal )
54 return fVal > 0.0 ? static_cast<long>( fVal + 0.5 ) : -static_cast<long>( -fVal + 0.5 );
57 #endif
59 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */