LibreOffice Module tools (master) 1
helpers.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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
10
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>
17
18template<typename T>
19inline
20typename std::enable_if<
21 std::is_signed<T>::value || std::is_floating_point<T>::value, long >::type
22MinMax(T nVal, tools::Long nMin, tools::Long nMax)
23{
24 assert(nMin <= nMax);
25 if (nVal >= nMin)
26 {
27 if (nVal <= nMax)
28 return static_cast<tools::Long>(nVal);
29 else
30 return nMax;
31 }
32 else
33 {
34 return nMin;
35 }
36}
37
38template<typename T>
39inline
40typename std::enable_if<
41 std::is_unsigned<T>::value, long >::type
42MinMax(T nVal, tools::Long nMin, tools::Long nMax)
43{
44 assert(nMin <= nMax);
45 if (nMax < 0)
46 {
47 return nMax;
48 }
49 else
50 {
51 if (nMin < 0 || nVal >= static_cast<unsigned long>(nMin))
52 {
53 if (nVal <= static_cast<unsigned long>(nMax))
54 return static_cast<tools::Long>(nVal);
55 else
56 return nMax;
57 }
58 else
59 {
60 return nMin;
61 }
62 }
63}
64
65inline sal_uInt32 AlignedWidth4Bytes(sal_uInt32 nWidthBits)
66{
67 if (nWidthBits > SAL_MAX_UINT32 - 31)
68 nWidthBits = SAL_MAX_UINT32;
69 else
70 nWidthBits += 31;
71 return (nWidthBits >> 5) << 2;
72}
73
74inline tools::Long FRound( double fVal )
75{
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 );
80}
81
82//valid range: (-180,180]
83template <typename T>
84[[nodiscard]] inline typename std::enable_if<std::is_signed<T>::value, T>::type
86{
87 while (angle <= -180)
88 angle += 360;
89 while (angle > 180)
90 angle -= 360;
91 return angle;
92}
93
94//valid range: [0,360)
95template <typename T> [[nodiscard]] inline T NormAngle360(T angle)
96{
97 while (angle < 0)
98 angle += 360;
99 while (angle >= 360)
100 angle -= 360;
101 return angle;
102}
103
104/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
T NormAngle360(T angle)
Definition: helpers.hxx:95
sal_uInt32 AlignedWidth4Bytes(sal_uInt32 nWidthBits)
Definition: helpers.hxx:65
tools::Long FRound(double fVal)
Definition: helpers.hxx:74
std::enable_if< std::is_signed< T >::value, T >::type NormAngle180(T angle)
Definition: helpers.hxx:85
std::enable_if< std::is_signed< T >::value||std::is_floating_point< T >::value, long >::type MinMax(T nVal, tools::Long nMin, tools::Long nMax)
Definition: helpers.hxx:22
long Long
Definition: long.hxx:34
#define SAL_MAX_UINT32