forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
units_energy.h
76 lines (58 loc) · 1.98 KB
/
units_energy.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
#pragma once
#ifndef CATA_SRC_UNITS_ENERGY_H
#define CATA_SRC_UNITS_ENERGY_H
#include <algorithm>
#include "units_def.h"
namespace units
{
class energy_in_joule_tag
{
};
using energy = quantity<int, energy_in_joule_tag>;
const energy energy_min = units::energy( std::numeric_limits<units::energy::value_type>::min(),
units::energy::unit_type{} );
const energy energy_max = units::energy( std::numeric_limits<units::energy::value_type>::max(),
units::energy::unit_type{} );
template<typename value_type>
inline constexpr quantity<value_type, energy_in_joule_tag> from_joule(
const value_type v )
{
return quantity<value_type, energy_in_joule_tag>( v, energy_in_joule_tag{} );
}
template<typename value_type>
inline constexpr quantity<value_type, energy_in_joule_tag> from_kilojoule( const value_type v )
{
const value_type max_energy_joules = std::numeric_limits<value_type>::max() / 1000;
value_type energy = v * 1000 > max_energy_joules ? max_energy_joules : v * 1000;
return from_joule<value_type>( energy );
}
template<typename value_type>
inline constexpr value_type to_joule( const quantity<value_type, energy_in_joule_tag> &v )
{
return v / from_joule<value_type>( 1 );
}
template<typename value_type>
inline constexpr value_type to_kilojoule( const quantity<value_type, energy_in_joule_tag> &v )
{
return to_joule( v ) / 1000.0;
}
} // namespace units
inline constexpr units::energy operator"" _J( const unsigned long long v )
{
return units::from_joule( v );
}
inline constexpr units::quantity<double, units::energy_in_joule_tag> operator"" _J(
const long double v )
{
return units::from_joule( v );
}
inline constexpr units::energy operator"" _kJ( const unsigned long long v )
{
return units::from_kilojoule( v );
}
inline constexpr units::quantity<double, units::energy_in_joule_tag> operator"" _kJ(
const long double v )
{
return units::from_kilojoule( v );
}
#endif // CATA_SRC_UNITS_ENERGY_H