-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitsInterface.hh
82 lines (59 loc) · 2.19 KB
/
UnitsInterface.hh
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#if !defined __Units_Interface_hh__
#define __Units_Interface_hh__
// incorporate Units by using the suffixes defined below
// to do so, a user should put this using directive:
// using namespace Units;
// in any scope where they want use Units -- that will allow statements like
// auto length = 200_m;
// omitting the using directive would require a user to use fully qualified names, and thus ugly notation, e.g.
// auto length = Units::operator""_m(200);
#include <exception>
#include "cpp_utilities/Units.hh"
#include "cpp_utilities/Numerics.hh"
namespace Units
{
using namespace std;
using ULLI = unsigned long long int;
using LD = long double;
using Numerics::IsFeasibleCast;
// append one of these suffixes to a number in order to assign units
constexpr Quantity<Meter> operator"" _m(LD value);
constexpr Quantity<Meter> operator""_m(ULLI value);
constexpr Quantity<eV> operator"" _eV(LD value);
constexpr Quantity<eV> operator"" _eV(ULLI value);
constexpr Quantity<Second> operator"" _s(LD value);
constexpr Quantity<Second> operator"" _s(ULLI value);
// definitions
constexpr Quantity<Meter> operator"" _m(LD value)
{
return IsFeasibleCast<LD,double>(value)?
Quantity<Meter>{static_cast<double>(value)} : throw logic_error("!");
}
constexpr Quantity<Meter> operator""_m(ULLI value)
{
return IsFeasibleCast<ULLI,double>(value)?
Quantity<Meter>{static_cast<double>(value)} : throw logic_error("!");
}
constexpr Quantity<eV> operator"" _eV(LD value)
{
return IsFeasibleCast<LD,double>(value)?
Quantity<eV>{static_cast<double>(value)} : throw logic_error("!");
}
constexpr Quantity<eV> operator"" _eV(ULLI value)
{
return IsFeasibleCast<ULLI,double>(value)?
Quantity<eV>{static_cast<double>(value)} : throw logic_error("!");
}
constexpr Quantity<Second> operator"" _s(LD value)
{
return IsFeasibleCast<LD,double>(value)?
Quantity<Second>{static_cast<double>(value)} : throw logic_error("!");
}
constexpr Quantity<Second> operator"" _s(ULLI value)
{
return IsFeasibleCast<ULLI,double>(value)?
Quantity<Second>{static_cast<double>(value)} : throw logic_error("!");
}
} // namespace Units
#endif
// end header guard