refactor: all_are_kinds removed and get_associated_quantity simplified

This commit is contained in:
Mateusz Pusz
2024-11-29 11:10:20 +01:00
parent a223c08a39
commit 9c4671790e
2 changed files with 11 additions and 58 deletions

View File

@@ -34,73 +34,26 @@ struct common_unit;
namespace detail { namespace detail {
template<AssociatedUnit U> template<AssociatedUnit U>
[[nodiscard]] consteval auto all_are_kinds(U); [[nodiscard]] consteval auto get_associated_quantity(U u);
template<typename U, auto... Vs>
[[nodiscard]] consteval auto all_are_kinds(power<U, Vs...>)
{
return all_are_kinds(U{});
}
template<typename... Nums, typename... Dens>
[[nodiscard]] consteval bool all_are_kinds(type_list<Nums...>, type_list<Dens...>)
{
return (... && all_are_kinds(Nums{})) && (... && all_are_kinds(Dens{}));
}
template<AssociatedUnit U>
[[nodiscard]] consteval auto all_are_kinds(U)
{
if constexpr (requires { U::_quantity_spec_; })
return QuantityKindSpec<MP_UNITS_NONCONST_TYPE(U::_quantity_spec_)>;
else if constexpr (requires { U::_reference_unit_; })
return all_are_kinds(U::_reference_unit_);
else if constexpr (requires { typename U::_num_; }) {
return all_are_kinds(typename U::_num_{}, typename U::_den_{});
}
}
template<AssociatedUnit U>
[[nodiscard]] consteval auto determine_associated_quantity(U u);
template<AssociatedUnit U>
using to_quantity_spec = decltype(determine_associated_quantity(U{}));
template<typename... Us> template<typename... Us>
[[nodiscard]] consteval auto determine_associated_quantity_impl(common_unit<Us...>) [[nodiscard]] consteval auto get_associated_quantity_impl(common_unit<Us...>)
{ {
return get_common_quantity_spec(determine_associated_quantity(Us{})...); return get_common_quantity_spec(get_associated_quantity(Us{})...);
} }
template<AssociatedUnit U> template<AssociatedUnit U>
[[nodiscard]] consteval auto determine_associated_quantity_impl(U u) using to_quantity_spec = decltype(get_associated_quantity(U{}));
{
if constexpr (requires { U::_quantity_spec_; })
return remove_kind(U::_quantity_spec_);
else if constexpr (requires { U::_reference_unit_; })
return determine_associated_quantity(U::_reference_unit_);
else if constexpr (requires { typename U::_num_; }) {
return expr_map<to_quantity_spec, derived_quantity_spec, struct dimensionless>(u);
}
}
template<AssociatedUnit U>
constexpr auto determine_associated_quantity_result = determine_associated_quantity_impl(U{});
template<AssociatedUnit U>
[[nodiscard]] consteval auto determine_associated_quantity(U)
{
return determine_associated_quantity_result<U>;
}
template<AssociatedUnit U> template<AssociatedUnit U>
[[nodiscard]] consteval auto get_associated_quantity_impl(U u) [[nodiscard]] consteval auto get_associated_quantity_impl(U u)
{ {
constexpr bool all_kinds = all_are_kinds(U{}); if constexpr (requires { U::_quantity_spec_; })
if constexpr (all_kinds) return remove_kind(U::_quantity_spec_);
return kind_of<determine_associated_quantity(U{})>; else if constexpr (requires { U::_reference_unit_; })
else return get_associated_quantity(U::_reference_unit_);
return determine_associated_quantity(u); else if constexpr (requires { typename U::_num_; })
return expr_map<to_quantity_spec, derived_quantity_spec, struct dimensionless>(u);
} }
template<AssociatedUnit U> template<AssociatedUnit U>

View File

@@ -51,7 +51,7 @@ MP_UNITS_EXPORT_BEGIN
[[nodiscard]] consteval QuantitySpec auto get_quantity_spec(AssociatedUnit auto u) [[nodiscard]] consteval QuantitySpec auto get_quantity_spec(AssociatedUnit auto u)
{ {
return detail::get_associated_quantity(u); return kind_of<detail::get_associated_quantity(u)>;
} }
/** /**