Skip to content

Commit

Permalink
refactor: all_are_kinds removed and get_associated_quantity simpl…
Browse files Browse the repository at this point in the history
…ified
  • Loading branch information
mpusz committed Nov 29, 2024
1 parent a223c08 commit 9c46717
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 56 deletions.
63 changes: 8 additions & 55 deletions src/core/include/mp-units/bits/get_associated_quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,73 +34,26 @@ struct common_unit;
namespace detail {

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)
template<typename... Us>
[[nodiscard]] consteval auto get_associated_quantity_impl(common_unit<Us...>)
{
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_{});
}
return get_common_quantity_spec(get_associated_quantity(Us{})...);
}

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>
[[nodiscard]] consteval auto determine_associated_quantity_impl(common_unit<Us...>)
{
return get_common_quantity_spec(determine_associated_quantity(Us{})...);
}
using to_quantity_spec = decltype(get_associated_quantity(U{}));

template<AssociatedUnit U>
[[nodiscard]] consteval auto determine_associated_quantity_impl(U u)
[[nodiscard]] consteval auto get_associated_quantity_impl(U 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 get_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>
[[nodiscard]] consteval auto get_associated_quantity_impl(U u)
{
constexpr bool all_kinds = all_are_kinds(U{});
if constexpr (all_kinds)
return kind_of<determine_associated_quantity(U{})>;
else
return determine_associated_quantity(u);
}

template<AssociatedUnit U>
Expand Down
2 changes: 1 addition & 1 deletion src/core/include/mp-units/framework/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ MP_UNITS_EXPORT_BEGIN

[[nodiscard]] consteval QuantitySpec auto get_quantity_spec(AssociatedUnit auto u)
{
return detail::get_associated_quantity(u);
return kind_of<detail::get_associated_quantity(u)>;
}

/**
Expand Down

0 comments on commit 9c46717

Please sign in to comment.