forked from mpusz/mp-units
feat: runtime conversion factors between units support added
This commit is contained in:
@@ -35,6 +35,32 @@ class quantity;
|
||||
|
||||
namespace detail {
|
||||
|
||||
// The default implementation for the number scaling customization point
|
||||
template<typename Rep, Unit From, Unit To>
|
||||
[[nodiscard]] constexpr auto scale_quantity_number(Rep v, From from, To to)
|
||||
requires(have_same_canonical_reference_unit(from, to))
|
||||
{
|
||||
using multiplier_type = decltype([] {
|
||||
// widen the type to prevent overflows
|
||||
using wider_type = decltype(Rep{} * std::intmax_t{});
|
||||
// check if `wider_type` supports scaling operations
|
||||
if constexpr (requires(wider_type v) { v* v / v; })
|
||||
// if the `wider_type` can handle scaling operations then use it to improve accuracy
|
||||
return wider_type{};
|
||||
else
|
||||
// needed for example for linear algebra where `op/` on matrix types is not available
|
||||
return std::intmax_t{};
|
||||
}());
|
||||
|
||||
constexpr Magnitude auto c_mag = detail::get_canonical_unit(from).mag / detail::get_canonical_unit(to).mag;
|
||||
constexpr Magnitude auto num = numerator(c_mag);
|
||||
constexpr Magnitude auto den = denominator(c_mag);
|
||||
constexpr Magnitude auto irr = c_mag * (den / num);
|
||||
|
||||
constexpr auto val = [](Magnitude auto m) { return get_value<multiplier_type>(m); };
|
||||
return v * val(num) / val(den) * val(irr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Explicit cast of entire quantity
|
||||
*
|
||||
@@ -45,7 +71,7 @@ namespace detail {
|
||||
template<Quantity To, auto R, typename Rep>
|
||||
requires(castable(get_quantity_spec(R), To::quantity_spec)) &&
|
||||
((get_unit(R) == To::unit && std::constructible_from<typename To::rep, Rep>) ||
|
||||
(get_unit(R) != To::unit)) // && scalable_with_<typename To::rep>))
|
||||
(get_unit(R) != To::unit && convertible(get_unit(R), To::unit))) // && scalable_with_<typename To::rep>))
|
||||
// TODO how to constrain the second part here?
|
||||
[[nodiscard]] constexpr Quantity auto sudo_cast(const quantity<R, Rep>& q)
|
||||
{
|
||||
@@ -66,26 +92,11 @@ template<Quantity To, auto R, typename Rep>
|
||||
else
|
||||
return Rep{};
|
||||
}());
|
||||
using multiplier_type = decltype([] {
|
||||
// widen the type to prevent overflows
|
||||
using wider_type = decltype(rep_type{} * std::intmax_t{});
|
||||
// check if `wider_type` supports scaling operations
|
||||
if constexpr (requires(wider_type v) { v* v / v; })
|
||||
// if the `wider_type` can handle scaling operations then use it to improve accuracy
|
||||
return wider_type{};
|
||||
else
|
||||
// needed for example for linear algebra where `op/` on matrix types is not available
|
||||
return std::intmax_t{};
|
||||
}());
|
||||
|
||||
constexpr Magnitude auto c_mag =
|
||||
detail::get_canonical_unit(get_unit(R)).mag / detail::get_canonical_unit(To::unit).mag;
|
||||
constexpr Magnitude auto num = numerator(c_mag);
|
||||
constexpr Magnitude auto den = denominator(c_mag);
|
||||
constexpr Magnitude auto irr = c_mag * (den / num);
|
||||
|
||||
constexpr auto val = [](Magnitude auto m) { return get_value<multiplier_type>(m); };
|
||||
return static_cast<TYPENAME To::rep>(static_cast<rep_type>(q.number()) * val(num) / val(den) * val(irr)) *
|
||||
// `scale_quantity_number` is a customization point
|
||||
// Will be found only via ADL (if provided) for user-defined units
|
||||
return static_cast<TYPENAME To::rep>(
|
||||
scale_quantity_number(static_cast<rep_type>(q.number()), get_unit(R), To::unit)) *
|
||||
To::reference;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,18 +50,13 @@ template<typename T, typename Arg>
|
||||
concept RepSafeConstructibleFrom = // exposition only
|
||||
std::constructible_from<T, Arg> && (treat_as_floating_point<T> || !treat_as_floating_point<Arg>);
|
||||
|
||||
// UFrom ratio is an exact multiple of UTo
|
||||
template<auto UFrom, auto UTo>
|
||||
concept Harmonic = // exposition only
|
||||
Unit<decltype(UFrom)> && Unit<decltype(UTo)> &&
|
||||
is_integral(get_canonical_unit(UFrom).mag / get_canonical_unit(UTo).mag);
|
||||
|
||||
template<typename QFrom, typename QTo>
|
||||
concept QuantityConvertibleTo = // exposition only
|
||||
Quantity<QFrom> && Quantity<QTo> && implicitly_convertible(QFrom::quantity_spec, QTo::quantity_spec) &&
|
||||
convertible(QFrom::unit, QTo::unit) && requires(QFrom q) { detail::sudo_cast<QTo>(q); } &&
|
||||
(treat_as_floating_point<typename QTo::rep> ||
|
||||
(!treat_as_floating_point<typename QFrom::rep> && Harmonic<QFrom::unit, QTo::unit>));
|
||||
(!treat_as_floating_point<typename QFrom::rep> &&
|
||||
is_unit_convertible<QFrom::unit, QTo::unit> == is_unit_convertible_result::integral_factor));
|
||||
|
||||
template<quantity_character Ch, typename Func, typename T, typename U>
|
||||
concept InvokeResultOf = std::regular_invocable<Func, T, U> && RepresentationOf<std::invoke_result_t<Func, T, U>, Ch>;
|
||||
|
||||
@@ -594,11 +594,21 @@ inline constexpr struct percent : named_unit<"%", mag<ratio{1, 100}> * one> {} p
|
||||
inline constexpr struct per_mille : named_unit<basic_symbol_text{"‰", "%o"}, mag<ratio(1, 1000)> * one> {} per_mille;
|
||||
// clang-format on
|
||||
|
||||
// is_unit_convertible customization point
|
||||
enum class is_unit_convertible_result { no, integral_factor, non_integral_factor };
|
||||
|
||||
// convertible_to
|
||||
template<Unit auto From, Unit auto To>
|
||||
inline constexpr is_unit_convertible_result is_unit_convertible =
|
||||
detail::have_same_canonical_reference_unit(From, To)
|
||||
? (is_integral(detail::get_canonical_unit(From).mag / detail::get_canonical_unit(To).mag)
|
||||
? is_unit_convertible_result::integral_factor
|
||||
: is_unit_convertible_result::non_integral_factor)
|
||||
: is_unit_convertible_result::no;
|
||||
|
||||
// convertible
|
||||
[[nodiscard]] consteval bool convertible(Unit auto from, Unit auto to)
|
||||
{
|
||||
return detail::have_same_canonical_reference_unit(from, to);
|
||||
return is_unit_convertible<from, to> != is_unit_convertible_result::no;
|
||||
}
|
||||
|
||||
// Common unit
|
||||
|
||||
Reference in New Issue
Block a user