android: Update app-specific/MIME type icons
[LibreOffice.git] / include / tools / helpers.hxx
blob5f61ba80836ce20bfe3501990ec9fabcf370437a
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 #pragma once
11 #include <sal/config.h>
12 #include <sal/types.h>
13 #include <tools/long.hxx>
14 #include <cassert>
15 #include <limits>
16 #include <type_traits>
18 template<typename T>
19 inline
20 typename std::enable_if<
21 std::is_signed<T>::value || std::is_floating_point<T>::value, long >::type
22 MinMax(T nVal, tools::Long nMin, tools::Long nMax)
24 assert(nMin <= nMax);
25 if (nVal >= nMin)
27 if (nVal <= nMax)
28 return static_cast<tools::Long>(nVal);
29 else
30 return nMax;
32 else
34 return nMin;
38 template<typename T>
39 inline
40 typename std::enable_if<
41 std::is_unsigned<T>::value, long >::type
42 MinMax(T nVal, tools::Long nMin, tools::Long nMax)
44 assert(nMin <= nMax);
45 if (nMax < 0)
47 return nMax;
49 else
51 if (nMin < 0 || nVal >= static_cast<unsigned long>(nMin))
53 if (nVal <= static_cast<unsigned long>(nMax))
54 return static_cast<tools::Long>(nVal);
55 else
56 return nMax;
58 else
60 return nMin;
65 inline sal_uInt32 AlignedWidth4Bytes(sal_uInt32 nWidthBits)
67 if (nWidthBits > SAL_MAX_UINT32 - 31)
68 nWidthBits = SAL_MAX_UINT32;
69 else
70 nWidthBits += 31;
71 return (nWidthBits >> 5) << 2;
74 inline tools::Long FRound( double fVal )
76 return fVal > 0.0
77 ? fVal == double(std::numeric_limits<tools::Long>::max())
78 ? std::numeric_limits<tools::Long>::max() : static_cast<tools::Long>( fVal + 0.5 )
79 : static_cast<tools::Long>( fVal - 0.5 );
82 //valid range: (-180,180]
83 template <typename T>
84 [[nodiscard]] inline typename std::enable_if<std::is_signed<T>::value, T>::type
85 NormAngle180(T angle)
87 while (angle <= -180)
88 angle += 360;
89 while (angle > 180)
90 angle -= 360;
91 return angle;
94 //valid range: [0,360)
95 template <typename T> [[nodiscard]] inline T NormAngle360(T angle)
97 while (angle < 0)
98 angle += 360;
99 while (angle >= 360)
100 angle -= 360;
101 return angle;
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */