Elements  5.12.0
A C++ base framework for the Euclid Software.
Number.h
Go to the documentation of this file.
1 
26 #ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_NUMBER_H_
27 #define ELEMENTSKERNEL_ELEMENTSKERNEL_NUMBER_H_
28 
29 #include <type_traits> // for is_floating_point, is_integral
30 #include <cmath> // for round
31 
32 #include "ElementsKernel/Export.h" // ELEMENTS_API
33 
34 namespace Elements {
35 
47 template<typename TargetType, typename SourceType>
48 ELEMENTS_API TargetType numberCast(const SourceType& s) {
49 
51  using std::is_integral;
52  using std::round;
53 
54  TargetType t = static_cast<TargetType>(s);
55 
56  if (is_floating_point<SourceType>::value && is_integral<TargetType>::value) {
57  t = static_cast<TargetType>(round(s));
58  }
59 
60  return t;
61 }
62 
63 } // namespace Elements
64 
65 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_NUMBER_H_
66 
defines the macros to be used for explicit export of the symbols
ELEMENTS_API TargetType numberCast(const SourceType &s)
this function is a number cast. It behaves exactly as a static_cast except when casting from a floati...
Definition: Number.h:48
#define ELEMENTS_API
Dummy definitions for the backward compatibility mode.
Definition: Export.h:74
constexpr double s
T round(T... args)