-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquantity.cppm
More file actions
299 lines (264 loc) · 12.2 KB
/
quantity.cppm
File metadata and controls
299 lines (264 loc) · 12.2 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* @brief Module for design types and behavior associated with Physical Quantities.
*
* A physical quantity can be understand as a real world magnitude, related with
* their units in the international system
*/
export module physics.quantities:quantity;
import std;
import concepts;
import type_info;
import str_manip;
import :ratios;
import :dimensions;
import :units;
import :units.symbols;
import :quantities.detail;
export namespace zero::physics {
template <typename T>
concept BaseMagnitude = requires {
BaseUnit<T>;
BaseDimension<typename T::dimension>;
};
template<typename T>
struct is_base_magnitude : std::false_type {};
template<typename T>
requires BaseMagnitude<std::remove_reference_t<T>>
struct is_base_magnitude<T> : std::true_type {};
template <typename T>
concept DerivedMagnitude = requires {
DerivedUnit<T>;
DerivedDimension<typename T::derived_dimension>;
};
template <typename T>
concept Magnitude = is_base_magnitude<T>::value || DerivedMagnitude<T>;
template <typename T, typename R>
concept SameDimension = requires {
requires BaseMagnitude<T> && BaseMagnitude<R>;
requires std::is_same_v<
typename T::dimension,
typename R::dimension
>;
};
template <typename T, typename R>
concept SameDimensions = requires {
requires DerivedMagnitude<T> && DerivedMagnitude<R>;
requires std::is_same_v<
typename T::derived_dimension,
typename R::derived_dimension
>;
};
template <typename T>
concept ValidAmountType = (std::is_integral_v<T> || std::is_floating_point_v<T>)
&& !std::is_same_v<T, char>;
/**
* A measurable property of a physical body, expressed in the terms of a scalar value with
* their units
* @tparam M a type that satisfies the {@link Magnitude} concept
* @tparam T a valid type for represent the scalar numeric value, constrained by the {@link ValidAmountType} concept
*/
template <Magnitude M, ValidAmountType T = double>
struct quantity {
T amount;
constexpr quantity<M, T>() noexcept = default;
constexpr explicit quantity<M, T>(T val) noexcept : amount(val) {}
/**
* Converts a quantity of a dimension to another one with the same dimension
*/
template <Magnitude Target>
constexpr auto to() const noexcept -> quantity<Target, T> {
if constexpr (is_base_magnitude<Target>::value)
return quantity<Target, T>(
amount * M::ratio::value / Target::ratio::value *
M::ratio::base_denominator / Target::ratio::base_denominator
);
else
return quantity<Target, T>((amount * M::dimensionality) / Target::dimensionality);
}
/**
* @return an {@link std::vector} with the dimensions declared for the Magnitude M
* as {@link std::string}
*/
template <typename Dummy = void, typename = std::enable_if_t<DerivedMagnitude<M>, Dummy>>
std::vector<std::string> dimensions() const {
std::vector<std::string> stringified_dimensions;
std::apply([&](auto... dim) {
((stringified_dimensions.emplace_back(zero::split_str(zero::types::type_name<decltype(dim)>()).back())), ...);
}, typename M::derived_dimension::dimensions{});
return stringified_dimensions;
}
/**
* prints a formatted version of the dimensions that are declared for the Magnitude M
*/
template <typename Dummy = void, typename = std::enable_if_t<DerivedMagnitude<M>, Dummy>>
void print_dimensions() const requires DerivedMagnitude<M> {
std::string dimension_names;
std::apply([&](auto... dim) {
((dimension_names +=
zero::split_str(zero::types::type_name<decltype(dim)>(), "::").back() + ", "
), ...);
}, typename M::derived_dimension::dimensions{});
auto magnitude_str_t = zero::split_str(zero::types::type_name<M>(), "::").back();
std::cout << magnitude_str_t << " has dimensions of: ["
<< dimension_names.substr(0, dimension_names.size() - 2) << "]\n";
}
};
/**
* @brief Sum of two scalar values in a binary expression for the - operator
* @return the resultant scalar value of adding the amount of the two quantities, with
* the return type of the one that has the bigger ratio given their common dimension
*/
template<BaseMagnitude M1, BaseMagnitude M2, ValidAmountType T1 = double, ValidAmountType T2 = T1>
requires SameDimension<M1, M2>
[[nodiscard]]
constexpr auto operator+(const quantity<M1, T1>& lhs, const quantity<M2, T2>& rhs)
-> quantity<std::conditional_t<(M1::ratio::value > M2::ratio::value), M1, M2>>
{
constexpr auto m1_ratio_v = M1::ratio::value;
constexpr auto m2_ratio_v = M2::ratio::value;
if constexpr (m1_ratio_v > m2_ratio_v)
return quantity<M1, T1>(
(lhs.amount * m1_ratio_v + rhs.amount * m2_ratio_v) / m1_ratio_v
);
else
return quantity<M2, T2>(
(lhs.amount * m1_ratio_v + rhs.amount * m2_ratio_v) / m2_ratio_v
);
}
/**
* @brief same as the operator+() overload for the {@link BaseMagnitude}, but for {@link DerivedMagnitude}
*/
template<DerivedMagnitude DM1, DerivedMagnitude DM2, ValidAmountType T1 = double, ValidAmountType T2 = T1>
requires SameDimensions<DM1, DM2>
[[nodiscard]]
consteval auto operator+(const quantity<DM1, T1>& lhs, const quantity<DM2, T2>& rhs) {
constexpr double dm1_dimensionality = DM1::dimensionality;
constexpr double dm2_dimensionality = DM2::dimensionality;
if constexpr (dm1_dimensionality > dm2_dimensionality)
return quantity<DM1, T1>((lhs.amount * dm1_dimensionality) + (rhs.amount * dm2_dimensionality));
else
return quantity<DM2, T2>((lhs.amount * dm1_dimensionality) + (rhs.amount * dm2_dimensionality));
}
/**
* @brief Subtraction of two scalar values in a binary expression for the - operator
* @return the resultant scalar value of subtracting the amount of the two quantities, with
* the return type of the one that has the bigger ratio given their common dimension
*/
template<Magnitude M1, Magnitude M2, ValidAmountType T1 = double, ValidAmountType T2 = T1>
requires SameDimension<M1, M2>
[[nodiscard]]
constexpr auto operator-(const quantity<M1, T1>& lhs, const quantity<M2, T2>& rhs)
-> quantity<std::conditional_t<(M1::ratio::value > M2::ratio::value), M1, M2>>
{
constexpr auto m1_ratio_v = M1::ratio::value;
constexpr auto m2_ratio_v = M2::ratio::value;
if constexpr (m1_ratio_v > m2_ratio_v)
return quantity<M1, T1>(
(lhs.amount * m1_ratio_v - rhs.amount * m2_ratio_v) / m1_ratio_v
);
else
return quantity<M2, T2>(
(lhs.amount * m1_ratio_v - rhs.amount * m2_ratio_v) / m2_ratio_v
);
}
/**
* @brief same as the operator-() overload for the {@link BaseMagnitude}, but for {@link DerivedMagnitude}
*/
template<DerivedMagnitude DM1, DerivedMagnitude DM2, ValidAmountType T1 = double, ValidAmountType T2 = T1>
requires SameDimensions<DM1, DM2>
[[nodiscard]]
consteval auto operator-(const quantity<DM1, T1>& lhs, const quantity<DM2, T2>& rhs) {
constexpr double dm1_dimensionality = DM1::dimensionality;
constexpr double dm2_dimensionality = DM2::dimensionality;
if constexpr (dm1_dimensionality > dm2_dimensionality)
return quantity<DM1, T1>((lhs.amount * dm1_dimensionality) - (rhs.amount * dm2_dimensionality));
else
return quantity<DM2, T2>((lhs.amount * dm1_dimensionality) - (rhs.amount * dm2_dimensionality));
}
/**
* @brief Multiplication of two scalar values in a binary expression for the - operator
* @return the resultant scalar value of multiply the amount of the two quantities, with
* the return type of the one that has the bigger ratio given their common dimension
*/
template<Magnitude M1, Magnitude M2, ValidAmountType T1 = double, ValidAmountType T2 = T1>
requires SameDimension<M1, M2>
[[nodiscard]]
constexpr auto operator*(const quantity<M1, T1>& lhs, const quantity<M2, T2>& rhs)
-> quantity<std::conditional_t<(M1::ratio::value > M2::ratio::value), M1, M2>>
{
constexpr auto m1_ratio_v = M1::ratio::value;
constexpr auto m2_ratio_v = M2::ratio::value;
if constexpr (m1_ratio_v > m2_ratio_v)
return quantity<M1, T1>(
(lhs.amount * m1_ratio_v * rhs.amount * m2_ratio_v) / m1_ratio_v
);
else
return quantity<M2, T2>(
(lhs.amount * m1_ratio_v * rhs.amount * m2_ratio_v) / m2_ratio_v
);
}
/**
* @brief same as the operator*() overload for the {@link BaseMagnitude}, but for {@link DerivedMagnitude}
*/
template<DerivedMagnitude DM1, DerivedMagnitude DM2, ValidAmountType T1 = double, ValidAmountType T2 = T1>
requires SameDimensions<DM1, DM2>
[[nodiscard]]
consteval auto operator*(const quantity<DM1, T1>& lhs, const quantity<DM2, T2>& rhs) {
constexpr double dm1_dimensionality = DM1::dimensionality;
constexpr double dm2_dimensionality = DM2::dimensionality;
if constexpr (dm1_dimensionality > dm2_dimensionality)
return quantity<DM1, T1>((lhs.amount * dm1_dimensionality) * (rhs.amount * dm2_dimensionality));
else
return quantity<DM2, T2>((lhs.amount * dm1_dimensionality) * (rhs.amount * dm2_dimensionality));
}
/**
* @brief Division of two scalar values in a binary expression for the - operator
* @return the resultant scalar value of divide the amount of the two quantities, with
* the return type of the one that has the bigger ratio given their common dimension
*/
template<Magnitude M1, Magnitude M2, ValidAmountType T1 = double, ValidAmountType T2 = T1>
requires SameDimension<M1, M2>
[[nodiscard]]
constexpr auto operator/(const quantity<M1, T1>& lhs, const quantity<M2, T2>& rhs)
-> quantity<std::conditional_t<(M1::ratio::value > M2::ratio::value), M1, M2>>
{
constexpr auto m1_ratio_v = M1::ratio::value;
constexpr auto m2_ratio_v = M2::ratio::value;
if constexpr (m1_ratio_v > m2_ratio_v)
return quantity<M1, T1>(
((lhs.amount * m1_ratio_v) / (rhs.amount * m2_ratio_v)) / m1_ratio_v
);
else
return quantity<M2, T2>(
((lhs.amount * m1_ratio_v) / (rhs.amount * m2_ratio_v)) / m2_ratio_v
);
}
/**
* @brief same as the operator/() overload for the {@link BaseMagnitude}, but for {@link DerivedMagnitude}
*/
template<DerivedMagnitude DM1, DerivedMagnitude DM2, ValidAmountType T1 = double, ValidAmountType T2 = T1>
requires SameDimensions<DM1, DM2>
[[nodiscard]]
consteval auto operator/(const quantity<DM1, T1>& lhs, const quantity<DM2, T2>& rhs) {
constexpr double dm1_dimensionality = DM1::dimensionality;
constexpr double dm2_dimensionality = DM2::dimensionality;
if constexpr (dm1_dimensionality > dm2_dimensionality)
return quantity<DM1, T1>((lhs.amount * dm1_dimensionality) / (rhs.amount * dm2_dimensionality));
else
return quantity<DM2, T2>((lhs.amount * dm1_dimensionality) / (rhs.amount * dm2_dimensionality));
}
/**
* Sends to an output stream a formatted version of some {@link quantity}
*/
template<typename M>
constexpr std::ostream& operator<<(std::ostream& os, const quantity<M>& q) {
if constexpr (is_base_magnitude<M>::value)
os << q.amount << " " << zero::split_str(zero::types::type_name<typename M::symbol>(), "::").back();
else {
std::string out;
derived_magnitude_symbols<M>(out);
os << q.amount << out;
}
return os;
}
}