From 41043c25f206dde9b110ca20c5dab4397d46f06d Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Mon, 13 Feb 2023 18:30:41 +0100 Subject: [PATCH] refactor: base unit definition refactored --- src/core/include/mp_units/unit.h | 88 ++++++++++++++++---------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/src/core/include/mp_units/unit.h b/src/core/include/mp_units/unit.h index 3de2d86f..adc1b248 100644 --- a/src/core/include/mp_units/unit.h +++ b/src/core/include/mp_units/unit.h @@ -107,13 +107,13 @@ struct named_unit; * the `cgs::centimetre` that is a base unit for `isq::length` in the CGS system. * * @tparam Symbol a short text representation of the unit - * @tparam BaseQuantitySpec base quantity measured with this unit + * @tparam QuantitySpec a specification of a base quantity to be measured with this unit */ -template - requires(!Symbol.empty()) -struct named_unit { +template + requires(!Symbol.empty()) && BaseDimension> +struct named_unit { static constexpr auto symbol = Symbol; ///< Unique base unit identifier - static constexpr auto base_quantity = Q; + static constexpr auto quantity_spec = QS; }; /** @@ -556,6 +556,45 @@ template canonical_lhs.mag == canonical_rhs.mag; } +/** + * @brief Computes the value of a unit raised to the `Num/Den` power + * + * @tparam Num Exponent numerator + * @tparam Den Exponent denominator + * @param u Unit being the base of the operation + * + * @return Unit The result of computation + */ +template + requires detail::non_zero +[[nodiscard]] consteval Unit auto pow(U u) +{ + if constexpr (requires { U::symbol; }) { + if constexpr (Den == 1) + return derived_unit>{}; + else + return derived_unit>{}; + } else if constexpr (detail::is_specialization_of_scaled_unit) { + return scaled_unit(U::mag), std::remove_const_t(U::reference_unit))>>{}; + } else { + return detail::expr_pow(u); + } +} + +// Helper variable templates to create common powers +template +inline constexpr decltype(U * U) square; + +template +inline constexpr decltype(U * U * U) cubic; + +// common dimensionless units +// clang-format off +inline constexpr struct percent : named_unit<"%", mag * one> {} percent; +inline constexpr struct per_mille : named_unit * one> {} per_mille; +// clang-format on + + // convertible_to [[nodiscard]] consteval bool convertible_to(Unit auto u1, Unit auto u2) { @@ -595,45 +634,6 @@ template return common_unit(common_unit(u1, u2), u3, rest...); } -/** - * @brief Computes the value of a unit raised to the `Num/Den` power - * - * @tparam Num Exponent numerator - * @tparam Den Exponent denominator - * @param u Unit being the base of the operation - * - * @return Unit The result of computation - */ -template - requires detail::non_zero -[[nodiscard]] consteval Unit auto pow(U u) -{ - if constexpr (requires { U::symbol; }) { - if constexpr (Den == 1) - return derived_unit>{}; - else - return derived_unit>{}; - } else if constexpr (detail::is_specialization_of_scaled_unit) { - return scaled_unit(U::mag), std::remove_const_t(U::reference_unit))>>{}; - } else { - return detail::expr_pow(u); - } -} - - -// Helper variable templates to create common powers -template -inline constexpr decltype(U * U) square; - -template -inline constexpr decltype(U * U * U) cubic; - -// common dimensionless units -// clang-format off -inline constexpr struct percent : named_unit<"%", mag * one> {} percent; -inline constexpr struct per_mille : named_unit * one> {} per_mille; -// clang-format on - // get_unit_symbol