Skip to content

Commit

Permalink
refactor: make_reference added and used in constraints for `quantit…
Browse files Browse the repository at this point in the history
…y` and `quantity_point`

`quantity_spec[unit]` syntax will not work for natural units and we want the interface to be widely applicable to all domains.
  • Loading branch information
mpusz committed Sep 20, 2023
1 parent f9ffacc commit 49da1ce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/core/include/mp-units/quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ class quantity {

// conversions
template<Unit U>
requires detail::QuantityConvertibleTo<quantity, quantity<quantity_spec[U{}], Rep>>
[[nodiscard]] constexpr quantity<quantity_spec[U{}], Rep> in(U) const
requires detail::QuantityConvertibleTo<quantity, quantity<detail::make_reference(quantity_spec, U{}), Rep>>
[[nodiscard]] constexpr quantity<detail::make_reference(quantity_spec, U{}), Rep> in(U) const
{
return quantity<quantity_spec[U{}], Rep>{*this};
return quantity<detail::make_reference(quantity_spec, U{}), Rep>{*this};
}

template<Unit U>
requires requires(quantity q) { value_cast<U{}>(q); }
[[nodiscard]] constexpr quantity<quantity_spec[U{}], Rep> force_in(U) const
[[nodiscard]] constexpr quantity<detail::make_reference(quantity_spec, U{}), Rep> force_in(U) const
{
return value_cast<U{}>(*this);
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/include/mp-units/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ class quantity_point {
}

template<Unit U>
requires detail::QuantityConvertibleTo<quantity_type, quantity<quantity_spec[U{}], Rep>>
[[nodiscard]] constexpr quantity_point<quantity_spec[U{}], PO, Rep> in(U) const
requires detail::QuantityConvertibleTo<quantity_type, quantity<detail::make_reference(quantity_spec, U{}), Rep>>
[[nodiscard]] constexpr quantity_point<detail::make_reference(quantity_spec, U{}), PO, Rep> in(U) const
{
return make_quantity_point<PO>(quantity_ref_from(PO).in(U{}));
}

template<Unit U>
requires requires(quantity_type q) { value_cast<U{}>(q); }
[[nodiscard]] constexpr quantity_point<quantity_spec[U{}], PO, Rep> force_in(U) const
[[nodiscard]] constexpr quantity_point<detail::make_reference(quantity_spec, U{}), PO, Rep> force_in(U) const
{
return make_quantity_point<PO>(quantity_ref_from(PO).force_in(U{}));
}
Expand Down
25 changes: 13 additions & 12 deletions src/core/include/mp-units/quantity_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ namespace mp_units {

namespace detail {


template<QuantitySpec QS, Unit U>
[[nodiscard]] consteval Reference auto make_reference(QS qs, U u)
{
if constexpr (detail::QuantityKindSpec<QS>)
return u;
else
return reference<qs, u>{};
}

// TODO revise the note in the below comment
/**
* @brief Returns the most restrictive character from the list
Expand Down Expand Up @@ -100,10 +110,7 @@ struct quantity_spec_interface {
template<typename Self, UnitOf<Self{}> U>
[[nodiscard]] consteval Reference auto operator[](this Self self, U u)
{
if constexpr (detail::QuantityKindSpec<Self>)
return u;
else
return reference<self, u>{};
return detail::make_reference(self, u);
}

template<typename Self, typename Q>
Expand All @@ -117,10 +124,7 @@ struct quantity_spec_interface {
template<typename Self_ = Self, UnitOf<Self_{}> U>
[[nodiscard]] MP_UNITS_CONSTEVAL Reference auto operator[](U u) const
{
if constexpr (detail::QuantityKindSpec<Self_>)
return u;
else
return reference<Self{}, u>{};
return detail::make_reference(Self{}, u);
}

template<typename Q, typename Self_ = Self>
Expand Down Expand Up @@ -296,10 +300,7 @@ struct quantity_spec<Self, QS, Args...> : std::remove_const_t<decltype(QS)> {
template<typename Self_ = Self, UnitOf<Self_{}> U>
[[nodiscard]] MP_UNITS_CONSTEVAL Reference auto operator[](U u) const
{
if constexpr (detail::QuantityKindSpec<Self>)
return u;
else
return reference<Self{}, u>{};
return detail::make_reference(Self{}, u);
}

template<typename Q, typename Self_ = Self>
Expand Down

0 comments on commit 49da1ce

Please sign in to comment.