From 56bfe8f01100f1c4963d6aea56f7555318c58e8a Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 25 Apr 2024 16:08:23 +0200 Subject: [PATCH] refactor: `underlying_type` split to `wrapped_type` and `value_type` and used in code --- src/core/include/mp-units/bits/sudo_cast.h | 14 +++++------ src/core/include/mp-units/ext/type_traits.h | 24 +++++++++++++------ .../mp-units/framework/customization_points.h | 10 ++------ .../framework/representation_concepts.h | 7 ++---- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/core/include/mp-units/bits/sudo_cast.h b/src/core/include/mp-units/bits/sudo_cast.h index 65e4ea2a..322423b4 100644 --- a/src/core/include/mp-units/bits/sudo_cast.h +++ b/src/core/include/mp-units/bits/sudo_cast.h @@ -70,13 +70,13 @@ template constexpr Magnitude auto irr = c_mag * (den / num); using c_rep_type = maybe_common_type::rep, typename To::rep>; using c_mag_type = common_magnitude_type; - using multiplier_type = conditional, - // ensure that the multiplier is also floating-point - conditional>, - // reuse user's type if possible - std::common_type_t>, - std::common_type_t>, - c_mag_type>; + using multiplier_type = conditional< + treat_as_floating_point, + // ensure that the multiplier is also floating-point + conditional>, + // reuse user's type if possible + std::common_type_t>, std::common_type_t>, + c_mag_type>; using c_type = maybe_common_type; constexpr auto val = [](Magnitude auto m) { return get_value(m); }; if constexpr (std::is_floating_point_v) { diff --git a/src/core/include/mp-units/ext/type_traits.h b/src/core/include/mp-units/ext/type_traits.h index 244ae160..ce68be69 100644 --- a/src/core/include/mp-units/ext/type_traits.h +++ b/src/core/include/mp-units/ext/type_traits.h @@ -106,20 +106,30 @@ struct get_element_type { } // namespace detail -template -struct underlying_type { - using type = T; -}; - template requires requires { typename T::value_type; } || requires { typename T::element_type; } -struct underlying_type { +struct wrapped_type { using type = MP_UNITS_TYPENAME conditional, detail::get_element_type>::type; }; template -using underlying_type_t = MP_UNITS_TYPENAME underlying_type::type; + requires requires { typename T::value_type; } || requires { typename T::element_type; } +using wrapped_type_t = MP_UNITS_TYPENAME wrapped_type::type; + +template +struct value_type { + using type = T; +}; + +template + requires requires { typename wrapped_type_t; } +struct value_type { + using type = MP_UNITS_TYPENAME wrapped_type_t; +}; + +template +using value_type_t = MP_UNITS_TYPENAME value_type::type; template concept one_of = (false || ... || std::same_as); diff --git a/src/core/include/mp-units/framework/customization_points.h b/src/core/include/mp-units/framework/customization_points.h index 4e19ae27..59259c4d 100644 --- a/src/core/include/mp-units/framework/customization_points.h +++ b/src/core/include/mp-units/framework/customization_points.h @@ -48,14 +48,8 @@ template inline constexpr bool treat_as_floating_point = std::is_floating_point_v; template - requires requires { typename Rep::value_type; } || requires { typename Rep::element_type; } -inline constexpr bool treat_as_floating_point = requires { - typename Rep::value_type; - requires treat_as_floating_point; -} || requires { - typename Rep::element_type; - requires treat_as_floating_point>; -}; + requires requires { typename wrapped_type_t; } +inline constexpr bool treat_as_floating_point = treat_as_floating_point>; /** * @brief Specifies a type to have a scalar character diff --git a/src/core/include/mp-units/framework/representation_concepts.h b/src/core/include/mp-units/framework/representation_concepts.h index 3528eea4..7c7be67e 100644 --- a/src/core/include/mp-units/framework/representation_concepts.h +++ b/src/core/include/mp-units/framework/representation_concepts.h @@ -72,11 +72,8 @@ concept CastableNumber = CommonTypeWith && ScalableNumber concept Scalable = - CastableNumber || - (requires { typename T::value_type; } && CastableNumber && - ScalableNumber>) || - (requires { typename T::element_type; } && CastableNumber> && - ScalableNumber, std::intmax_t>>); + CastableNumber || (requires { typename wrapped_type_t; } && CastableNumber> && + ScalableNumber, std::intmax_t>>); } // namespace detail