KiCad PCB EDA Suite
util.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
5  * Copyright (C) CERN
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef UTIL_H
27 #define UTIL_H
28 
29 #include <cstdint>
30 #include <limits>
31 #include <typeinfo>
32 #include <wx/debug.h>
33 #include <wx/log.h>
34 
45 template <typename T> inline const T& Clamp( const T& lower, const T& value, const T& upper )
46 {
47  wxASSERT( lower <= upper );
48  if( value < lower )
49  return lower;
50  else if( upper < value )
51  return upper;
52  return value;
53 }
54 
60 template <typename fp_type, typename ret_type = int>
61 constexpr ret_type KiROUND( fp_type v )
62 {
63  using max_ret = long long int;
64  fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
65 
66  if( std::numeric_limits<ret_type>::max() < ret ||
67  std::numeric_limits<ret_type>::lowest() > ret )
68  {
69  wxLogDebug
70  ( "Overflow KiROUND converting value %f to %s", double( v ), typeid(ret_type).name() );
71  return 0;
72  }
73 
74  return ret_type( max_ret( ret ) );
75 }
76 
83 template <typename T>
84 T rescale( T aNumerator, T aValue, T aDenominator )
85 {
86  return aNumerator * aValue / aDenominator;
87 }
88 
89 template <typename T>
90 int sign( T val )
91 {
92  return ( T( 0 ) < val) - ( val < T( 0 ) );
93 }
94 
95 // explicit specializations for integer types, taking care of overflow.
96 template <>
97 int rescale( int aNumerator, int aValue, int aDenominator );
98 
99 template <>
100 int64_t rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
101 
102 #endif // UTIL_H
int sign(T val)
Definition: util.h:90
const char * name
Definition: DXF_plotter.cpp:60
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:84
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61
const T & Clamp(const T &lower, const T &value, const T &upper)
Function Clamp limits value within the range lower <= value <= upper.
Definition: util.h:45