Repository navigation
Expand file tree
/
Copy pathunits.cppm
More file actions
58 lines (49 loc) · 1.61 KB
/
Copy pathunits.cppm
File metadata and controls
58 lines (49 loc) · 1.61 KB
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
/**
* @brief Predefined physical units based on the definitions
* provided by the international system
*/
export module physics.quantities:units;
import std;
import math;
import concepts;
import :ratios;
import :dimensions;
import :units.symbols;
import :quantities.detail;
using namespace quantities::__detail;
using namespace zero::math;
export namespace zero::physics {
/* Base units */
template<Ratio r, Symbol s>
struct base_unit {
using ratio = r;
using symbol = s;
};
template <typename T>
concept BaseUnit =
Ratio<typename T::ratio> &&
Symbol<typename T::symbol> &&
requires {
typename T::ratio;
typename T::symbol;
};
/* Derived units */
template <typename DerivedDim, BaseUnit... BaseUnits>
requires (DerivedDimension<DerivedDim>)
struct derived_unit {
using derived_dimension = DerivedDim;
using units = std::tuple<BaseUnits...>;
static constexpr double dimensionality = []<std::size_t... Is>(std::index_sequence<Is...>) {
using dm_exp = dimensions_exponents<typename DerivedDim::dimensions>;
return (
1.0 * ... * power_of(BaseUnits::ratio::value, dm_exp::value[Is])
);
} (std::make_index_sequence<DerivedDim::total_dimensions>{});
};
template <typename T, std::size_t... Is>
concept DerivedUnit = requires {
typename T::units;
typename T::derived_dimension;
T::dimensionality;
} && (std::is_base_of_v<derived_unit<typename T::derived_dimension, std::tuple_element_t<Is, typename T::units>>, T> && ...);
}