Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
dunesim
dunesim
DetSim
Utility
SignalTypeConverter.h
Go to the documentation of this file.
1
// SignalTypeConverter.h
2
//
3
// David Adams
4
// November 2015
5
//
6
// Utility class to convert between int and floating point types.
7
// Float to int conversion is rounding to nearest int:
8
// (-1.5 , 0.5] --> -1
9
// (-0.5 , 0.5) --> 0
10
// [ 0.5 , 1.5) --> 1
11
//
12
// Future development may add parameters which specify different
13
// conversion strategies.
14
//
15
// Examples of usage.
16
// float val = 1.2;
17
// SignalTypeConverter sigcon.
18
// int ival1 = sigcon.floatToInt(val);
19
// short ival2 = sigcon.convert<short>(val);
20
21
#include <type_traits>
22
23
class
SignalTypeConverter
{
24
25
public
:
26
27
// Ctor.
28
SignalTypeConverter
();
29
30
// Convert type T1 to type T2.
31
// Uses floatToInt if If T1 is floating and T2 is integral.
32
// Otherwise, standard type conversion.
33
// Usage: int ival = convert<int>(myfloat);
34
template
<
typename
T2,
typename
T1>
35
T2
convert
(
T1
val
)
const
{
36
if
( std::is_floating_point<T1>() && std::is_integral<T2>() ) {
37
return
floatToInt<T2, T1>(
val
);
38
}
else
{
39
return
T2
(val);
40
}
41
}
42
43
// Conversion from floating point to int with arbitrary types.
44
template
<
typename
INT =
int
,
typename
FLOAT>
45
INT
floatToInt
(FLOAT
val
)
const
{
46
if
( val < 0.0 )
return
-floatToInt<INT, FLOAT>(-
val
);
47
return
INT(val + 0.5);
48
}
49
50
};
val
Definition:
registry_via_id_test_2.cc:15
SignalTypeConverter::floatToInt
INT floatToInt(FLOAT val) const
Definition:
SignalTypeConverter.h:45
T2
Definition:
013_class.h:14
SignalTypeConverter
Definition:
SignalTypeConverter.h:23
SignalTypeConverter::convert
T2 convert(T1 val) const
Definition:
SignalTypeConverter.h:35
T1
Definition:
013_class.h:10
testsqlite3.val
list val
Definition:
testsqlite3.py:13
SignalTypeConverter::SignalTypeConverter
SignalTypeConverter()
Definition:
SignalTypeConverter.cxx:7
Generated by
1.8.11