forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
units_mass.h
134 lines (104 loc) · 3.38 KB
/
units_mass.h
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#pragma once
#ifndef CATA_SRC_UNITS_MASS_H
#define CATA_SRC_UNITS_MASS_H
#include <cstdint>
#include <algorithm>
#include "units_def.h"
// yeah this is very cursed position for constant
// but if it's on game_constants.h it causes circular dependency
// TODO: move this to constants_mass or something
// 9.81m/(s^2) or newtons per kilogram
constexpr double GRAVITY_OF_EARTH = 9.81;
namespace units
{
class mass_in_milligram_tag
{
};
using mass = quantity<std::int64_t, mass_in_milligram_tag>;
constexpr mass mass_min = units::mass( std::numeric_limits<units::mass::value_type>::min(),
units::mass::unit_type{} );
constexpr mass mass_max = units::mass( std::numeric_limits<units::mass::value_type>::max(),
units::mass::unit_type{} );
template<typename value_type>
inline constexpr quantity<value_type, mass_in_milligram_tag> from_milligram(
const value_type v )
{
return quantity<value_type, mass_in_milligram_tag>( v, mass_in_milligram_tag{} );
}
template<typename value_type>
inline constexpr quantity<value_type, mass_in_milligram_tag> from_gram(
const value_type v )
{
return from_milligram( v * 1000 );
}
template<typename value_type>
inline constexpr quantity<value_type, mass_in_milligram_tag> from_kilogram(
const value_type v )
{
return from_gram( v * 1000 );
}
template<typename value_type>
inline constexpr quantity<value_type, mass_in_milligram_tag> from_newton(
const value_type v )
{
return from_kilogram( v / GRAVITY_OF_EARTH );
}
template<typename value_type>
inline constexpr value_type to_milligram( const quantity<value_type, mass_in_milligram_tag> &v )
{
return v.value();
}
template<typename value_type>
inline constexpr value_type to_gram( const quantity<value_type, mass_in_milligram_tag> &v )
{
return v.value() / 1000.0;
}
template<typename value_type>
inline constexpr value_type to_kilogram( const quantity<value_type, mass_in_milligram_tag> &v )
{
return v.value() / 1000000.0;
}
template<typename value_type>
inline constexpr value_type to_newton( const quantity<value_type, mass_in_milligram_tag> &v )
{
return to_kilogram( v ) * GRAVITY_OF_EARTH;
}
} // namespace units
// Implicitly converted to mass, which has int as value_type!
inline constexpr units::mass operator"" _milligram( const unsigned long long v )
{
return units::from_milligram( v );
}
inline constexpr units::mass operator"" _gram( const unsigned long long v )
{
return units::from_gram( v );
}
inline constexpr units::mass operator"" _kilogram( const unsigned long long v )
{
return units::from_kilogram( v );
}
inline constexpr units::mass operator"" _newton( const unsigned long long v )
{
return units::from_newton( v );
}
inline constexpr units::quantity<double, units::mass_in_milligram_tag> operator"" _milligram(
const long double v )
{
return units::from_milligram( v );
}
inline constexpr units::quantity<double, units::mass_in_milligram_tag> operator"" _gram(
const long double v )
{
return units::from_gram( v );
}
inline constexpr units::quantity<double, units::mass_in_milligram_tag> operator"" _kilogram(
const long double v )
{
return units::from_kilogram( v );
}
inline constexpr units::quantity<double, units::mass_in_milligram_tag> operator"" _newton(
const long double v )
{
return units::from_newton( v );
}
#endif // CATA_SRC_UNITS_MASS_H