1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| template<class UnitsTraits>
class Converter {
public:
static constexpr double factor = UnitsTraits::factor;
static constexpr double offset = UnitsTraits::offset;
static constexpr inline double toHuman(double siVal) {
return siVal / factor - offset;
}
static constexpr inline double toSI(double humanVal) {
return (humanVal + offset) * factor;
}
};
#define DECL_UNIT_CONVERTER(name, fact, off) \
struct name##_traits { \
static constexpr double factor = fact; \
static constexpr double offset = off; \
}; \
typedef Converter<name##_traits> name;
// converts to and from Pa
DECL_UNIT_CONVERTER(pressureHPa, 100.0, 0.0)
DECL_UNIT_CONVERTER(pressurePSI, 6894.75729, 0.0)
// converts to and from m3
DECL_UNIT_CONVERTER(volumeL, 1/1000.0, 0.0)
DECL_UNIT_CONVERTER(volumeML, 1/1000000.0, 0.0)
DECL_UNIT_CONVERTER(volumeGal, 0.00378541, 0.0)
// converts to and from m3/s
DECL_UNIT_CONVERTER(flowLPS, 1/1000.0, 0.0)
DECL_UNIT_CONVERTER(flowLPM, 1/60000.0, 0.0)
DECL_UNIT_CONVERTER(flowLPH, 1/3600000.0, 0.0)
DECL_UNIT_CONVERTER(flowCCM, 1/60000000.0, 0.0)
// converts to and from °K
DECL_UNIT_CONVERTER(tempC, 1.0, 273.15)
DECL_UNIT_CONVERTER(tempF, 5/9.0, 459.67)
DECL_UNIT_CONVERTER(tempR, 5/9.0, 0.0)
// converts to and from s
DECL_UNIT_CONVERTER(timeM, 60.0, 0.0)
DECL_UNIT_CONVERTER(timeH, 3600.0, 0.0)
DECL_UNIT_CONVERTER(timeD, 86400.0, 0.0)
// converts to and from kg
DECL_UNIT_CONVERTER(massLb, 0.453592, 0.0)
DECL_UNIT_CONVERTER(massG, 0.001, 0.0)
#undef DECL_UNIT_CONVERTER |
Partager