5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UNITS_SCALED_UNIT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UNITS_SCALED_UNIT_HPP_
12#include "arene/base/stdlib_choice/cstdint.hpp"
13#include "arene/base/stdlib_choice/enable_if.hpp"
14#include "arene/base/stdlib_choice/initializer_list.hpp"
15#include "arene/base/stdlib_choice/is_floating_point.hpp"
16#include "arene/base/stdlib_choice/ratio.hpp"
28namespace scaled_unit_detail {
32template <
typename Type>
33extern constexpr bool is_ratio_v =
false;
38template <std::
intmax_t Numerator, std::
intmax_t Denominator>
39extern constexpr bool is_ratio_v<std::ratio<Numerator, Denominator>> =
true;
43template <
typename Type,
typename = constra
ints<>>
44extern constexpr bool is_identity_ratio_v =
false;
48template <
typename Type>
49extern constexpr bool is_identity_ratio_v<Type, constraints<std::enable_if_t<is_ratio_v<Type>>>> =
50 (Type::num == Type::den);
55template <
typename Type,
typename = constra
ints<>>
56extern constexpr bool is_floating_point_wrapper_scale_v =
false;
61template <
typename Type>
62extern constexpr bool is_floating_point_wrapper_scale_v<
70template <
typename Type>
71extern constexpr bool is_valid_scale_v = is_floating_point_wrapper_scale_v<Type> || is_ratio_v<Type>;
75template <
typename Type>
76class inverse_unit_scale {
80 static constexpr double value{1. / Type::value};
86template <
typename Type,
typename = constra
ints<>>
87class inverse_unit_scale_helper {
88 static_assert(is_floating_point_wrapper_scale_v<Type>,
"The type being inverted must be a floating point wrapper");
92 using type = inverse_unit_scale<Type>;
98template <
typename BaseUnit,
typename Scale>
99class scaled_unit_impl :
public quantity_unit<scaled_unit_impl<BaseUnit, Scale>, typename BaseUnit::unit_kind_type> {
102 using scale_factor = Scale;
105 using is_implicit = void;
112template <
typename BaseUnit,
typename Scale,
typename = constra
ints<>>
113class scaled_unit_helper {
115 static_assert(is_valid_scale_v<Scale>,
"The scale factor must be a ratio or a class wrapping a floating point scale");
119 using type = scaled_unit_impl<BaseUnit, Scale>;
127template <
typename BaseUnit,
typename Scale>
128using scaled_unit =
typename scaled_unit_detail::scaled_unit_helper<BaseUnit, Scale>::type;
132template <
typename Type>
135namespace scaled_unit_detail {
141template <
typename Type, constra
ints<std::enable_if_t<std::is_
floating_po
int<decltype(Type::value)>::value>> =
nullptr>
142constexpr auto scale_value()
noexcept ->
double {
150template <
typename Type, constra
ints<std::enable_if_t<is_ratio_v<Type>>> =
nullptr>
151constexpr auto scale_value()
noexcept ->
double {
152 return static_cast<double>(Type::num) /
static_cast<double>(Type::den);
160 for (
auto const elem : values) {
169template <
typename... Scales>
170class combined_scales {
174 static constexpr double value{combined_scale_value({scale_value<Scales>()...})};
182template <
typename Scales>
183class canonicalize_scale {
193class canonicalize_scale<combined_scales<>> {
203template <
typename Scale>
204class canonicalize_scale<combined_scales<Scale>> {
217template <
typename LhsScale,
typename RhsScale>
218class scale_sorts_before {
223 static constexpr bool value{
236template <
typename... Scales>
237class canonicalize_scale<combined_scales<Scales...>> {
246template <
typename Scale1,
typename Scale2,
typename = constra
ints<>>
247class combine_scales_helper {
250 using type = combined_scales<Scale1, Scale2>;
256template <
typename Scale1,
typename Scale2>
257using combine_scales_t =
typename canonicalize_scale<typename combine_scales_helper<Scale1, Scale2>::type>::type;
264template <
typename Scale1,
typename Scale2>
265class combine_scales_helper<
280template <
typename Scale1,
typename Scale2>
281class combine_scales_helper<
287 using type = combine_scales_t<Scale2, Scale1>;
295template <
typename... Scales1,
typename Scale2>
296class combine_scales_helper<
297 combined_scales<Scales1...>,
300 std::enable_if_t<!is_ratio_v<Scale2>>,
301 std::enable_if_t<!is_one_of_v<inverse_unit_scale_t<Scale2>, Scales1...>>>> {
304 using type = combined_scales<Scales1..., Scale2>;
312template <
typename... Scales1,
typename Scale2>
313class combine_scales_helper<
314 combined_scales<Scales1...>,
317 std::enable_if_t<!is_ratio_v<Scale2>>,
322 combined_scales<Scales1...>,
332template <
typename Scale1,
typename FirstScale2,
typename... OtherScales2>
333class combine_scales_helper<
335 combined_scales<FirstScale2, OtherScales2...>,
337 std::enable_if_t<is_ratio_v<Scale1>>,
342 using type = combined_scales<Scale1, FirstScale2, OtherScales2...>;
351template <
typename Scale1,
typename FirstScale2,
typename... OtherScales2>
352class combine_scales_helper<
354 combined_scales<FirstScale2, OtherScales2...>,
358 using type = combine_scales_t<std::ratio_multiply<Scale1, FirstScale2>, combined_scales<OtherScales2...>>;
366template <
typename Scale1,
typename... Scales2>
367class combine_scales_helper<
369 combined_scales<Scales2...>,
373 using type = combined_scales<Scales2...>;
380template <
typename BaseUnit,
typename BaseScale,
typename Scale>
381class scaled_unit_helper<
382 scaled_unit_impl<BaseUnit, BaseScale>,
385 static_assert(is_valid_scale_v<Scale>,
"The scale factor must be a ratio or a class wrapping a floating point scale");
397template <
typename BaseUnit,
typename Scale>
398class scaled_unit_helper<BaseUnit, Scale,
constraints<
std::enable_if_t<is_identity_ratio_v<Scale>>>> {
400 static_assert(is_valid_scale_v<Scale>,
"The scale factor must be a ratio or a class wrapping a floating point scale");
404 using type = BaseUnit;
409template <
typename Type>
410class inverse_unit_scale_helper<Type,
constraints<
std::enable_if_t<is_ratio_v<Type>>>> {
418template <
typename Type>
419class inverse_unit_scale_helper<inverse_unit_scale<Type>> {
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
A compile-time ratio of two integers, automatically reduced to lowest terms.
Definition ratio.hpp:27
typename sort< TypeList, ComparisonPredicate >::type sort_t
A type alias for a type list holding the elements of TypeList sorted by ComparisonPredicate ....
Definition sort.hpp:256
typename remove_nth< List, Index >::type remove_nth_t
Filter a type-list to remove the element at the specified index.
Definition remove_nth.hpp:89
ARENE_MAYBE_UNUSED constexpr std::size_t index_of_v
Get the index of T in the type-list, L0.
Definition accumulate.hpp:37
typename scaled_unit_detail::scaled_unit_helper< BaseUnit, Scale >::type scaled_unit
Type alias for a quantity unit that is another unit scaled by some scale factor.
Definition scaled_unit.hpp:128
constexpr auto type_name_v
The name of a type as an inline_string.
constexpr bool is_unit_v
Type trait for detecting if a type is a unit. Evaluates to true if the type is a unit,...
typename scaled_unit_detail::inverse_unit_scale_helper< Type >::type inverse_unit_scale_t
A type that represents the inverse of a scale factor.
Definition scaled_unit.hpp:133
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
typename ratio_detail::ratio_multiply_impl< R1, R2 >::type ratio_multiply
Defines the ratio yielded by multiplying the two given ratios.
Definition ratio.hpp:213