mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 10:27:16 +02:00
Basic support for deduced units printing added
This commit is contained in:
@ -141,9 +141,13 @@ namespace units {
|
|||||||
struct dimension : downcast_base<dimension<Es...>> {};
|
struct dimension : downcast_base<dimension<Es...>> {};
|
||||||
|
|
||||||
// same_dim
|
// same_dim
|
||||||
template<Dimension D1, Dimension D2>
|
template<typename D1, Dimension D2>
|
||||||
|
requires BaseDimension<D1> || Dimension<D1>
|
||||||
inline constexpr bool same_dim = std::is_same_v<typename D1::base_type, typename D2::base_type>;
|
inline constexpr bool same_dim = std::is_same_v<typename D1::base_type, typename D2::base_type>;
|
||||||
|
|
||||||
|
template<BaseDimension D1, Dimension D2>
|
||||||
|
inline constexpr bool same_dim<D1, D2> = std::is_same_v<dimension<units::exp<D1, 1>>, typename D2::base_type>;
|
||||||
|
|
||||||
// dim_invert
|
// dim_invert
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct acceleration : derived_dimension<acceleration, exp<velocity, 1>, exp<base_dim_time, -1>> {};
|
struct acceleration : derived_dimension<acceleration, exp<velocity, 1>, exp<time, -1>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept Acceleration = QuantityOf<T, acceleration>;
|
concept Acceleration = QuantityOf<T, acceleration>;
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct area : derived_dimension<area, exp<base_dim_length, 2>> {};
|
struct area : derived_dimension<area, exp<length, 2>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept Area = QuantityOf<T, area>;
|
concept Area = QuantityOf<T, area>;
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct electric_charge : derived_dimension<electric_charge, exp<base_dim_time, 1>, exp<base_dim_current, 1>> {};
|
struct electric_charge : derived_dimension<electric_charge, exp<time, 1>, exp<current, 1>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept ElectricCharge = QuantityOf<T, electric_charge>;
|
concept ElectricCharge = QuantityOf<T, electric_charge>;
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct force : derived_dimension<force, exp<base_dim_mass, 1>, exp<acceleration, 1>> {};
|
struct force : derived_dimension<force, exp<mass, 1>, exp<acceleration, 1>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept Force = QuantityOf<T, force>;
|
concept Force = QuantityOf<T, force>;
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct frequency : derived_dimension<frequency, exp<base_dim_time, -1>> {};
|
struct frequency : derived_dimension<frequency, exp<time, -1>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept Frequency = QuantityOf<T, frequency>;
|
concept Frequency = QuantityOf<T, frequency>;
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct power : derived_dimension<power, exp<energy, 1>, exp<base_dim_time, -1>> {};
|
struct power : derived_dimension<power, exp<energy, 1>, exp<time, -1>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept Power = QuantityOf<T, power>;
|
concept Power = QuantityOf<T, power>;
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct velocity : derived_dimension<velocity, exp<base_dim_length, 1>, exp<base_dim_time, -1>> {};
|
struct velocity : derived_dimension<velocity, exp<length, 1>, exp<time, -1>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept Velocity = QuantityOf<T, velocity>;
|
concept Velocity = QuantityOf<T, velocity>;
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct voltage : derived_dimension<voltage, exp<power, 1>, exp<base_dim_current, -1>> {};
|
struct voltage : derived_dimension<voltage, exp<power, 1>, exp<current, -1>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept Voltage = QuantityOf<T, voltage>;
|
concept Voltage = QuantityOf<T, voltage>;
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
struct volume : derived_dimension<volume, exp<base_dim_length, 3>> {};
|
struct volume : derived_dimension<volume, exp<length, 3>> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
concept Volume = QuantityOf<T, volume>;
|
concept Volume = QuantityOf<T, volume>;
|
||||||
|
@ -53,7 +53,7 @@ namespace units {
|
|||||||
std::is_empty_v<T> &&
|
std::is_empty_v<T> &&
|
||||||
detail::is_unit<downcast_base_t<T>>;
|
detail::is_unit<downcast_base_t<T>>;
|
||||||
|
|
||||||
// make_derived_unit
|
// deduced_derived_unit
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ namespace units {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<Dimension D, Unit... Us>
|
template<Dimension D, Unit... Us>
|
||||||
using make_derived_unit = unit<D, typename detail::derived_ratio<typename D::base_type, Us...>::ratio>;
|
using deduced_derived_unit = unit<D, typename detail::derived_ratio<typename D::base_type, Us...>::ratio>;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,11 +214,11 @@ namespace units {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename E, std::size_t Idx>
|
template<typename E, basic_fixed_string Symbol, std::size_t Idx>
|
||||||
constexpr auto exp_text()
|
constexpr auto exp_text()
|
||||||
{
|
{
|
||||||
// get calculation operator + symbol
|
// get calculation operator + symbol
|
||||||
const auto txt = operator_text<E::num < 0, Idx>() + E::dimension::symbol;
|
const auto txt = operator_text<E::num < 0, Idx>() + Symbol;
|
||||||
if constexpr(E::den != 1) {
|
if constexpr(E::den != 1) {
|
||||||
// add root part
|
// add root part
|
||||||
return txt + basic_fixed_string("^(") + regular<abs(E::num)>() + basic_fixed_string("/") + regular<E::den>() + basic_fixed_string(")");
|
return txt + basic_fixed_string("^(") + regular<abs(E::num)>() + basic_fixed_string("/") + regular<E::den>() + basic_fixed_string(")");
|
||||||
@ -233,15 +233,35 @@ namespace units {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Es, std::size_t... Idxs>
|
template<typename... Es, std::size_t... Idxs>
|
||||||
constexpr auto symbol_text_impl(dimension<Es...>, std::index_sequence<Idxs...>)
|
constexpr auto base_symbol_text_impl(dimension<Es...>, std::index_sequence<Idxs...>)
|
||||||
{
|
{
|
||||||
return (exp_text<Es, Idxs>() + ...);
|
return (exp_text<Es, Es::dimension::symbol, Idxs>() + ...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Es>
|
template<typename... Es>
|
||||||
constexpr auto symbol_text(dimension<Es...> d)
|
constexpr auto base_symbol_text(dimension<Es...> d)
|
||||||
{
|
{
|
||||||
return symbol_text_impl<>(d, std::index_sequence_for<Es...>());
|
return base_symbol_text_impl(d, std::index_sequence_for<Es...>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename E, typename U, std::size_t Idx>
|
||||||
|
constexpr auto exp_validate_and_text()
|
||||||
|
{
|
||||||
|
static_assert(same_dim<typename E::dimension, typename U::dimension>);
|
||||||
|
return exp_text<E, U::symbol, Idx>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Us, typename... Es, std::size_t... Idxs>
|
||||||
|
constexpr auto deduced_symbol_text_impl(dimension<Es...>, std::index_sequence<Idxs...>)
|
||||||
|
{
|
||||||
|
return (exp_validate_and_text<Es, Us, Idxs>() + ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Us, typename... Es>
|
||||||
|
constexpr auto deduced_symbol_text(dimension<Es...> d)
|
||||||
|
{
|
||||||
|
static_assert(sizeof...(Us) == sizeof...(Es), "`deduced_derived_unit<Us...>` should get the same number of exponents as provided to `derived_dimension<>`");
|
||||||
|
return deduced_symbol_text_impl<Us...>(d, std::index_sequence_for<Es...>());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Unit>
|
template<typename Unit>
|
||||||
@ -263,7 +283,7 @@ namespace units {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// print as a ratio of a coherent unit + coherent unit dimensions and their exponents
|
// print as a ratio of a coherent unit + coherent unit dimensions and their exponents
|
||||||
return ratio_text<ratio>() + symbol_text(dim{});
|
return ratio_text<ratio>() + base_symbol_text(dim{});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -281,7 +301,7 @@ namespace units {
|
|||||||
|
|
||||||
template<typename Child, Dimension Dim>
|
template<typename Child, Dimension Dim>
|
||||||
struct coherent_derived_unit : downcast_child<Child, unit<Dim, ratio<1>>> {
|
struct coherent_derived_unit : downcast_child<Child, unit<Dim, ratio<1>>> {
|
||||||
static constexpr auto symbol = detail::symbol_text(Dim());
|
static constexpr auto symbol = detail::base_symbol_text(Dim());
|
||||||
using prefix_type = no_prefix;
|
using prefix_type = no_prefix;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -292,14 +312,14 @@ namespace units {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename Child, Dimension Dim, basic_fixed_string Symbol, PrefixType PT, Unit U, Unit... Us>
|
template<typename Child, Dimension Dim, basic_fixed_string Symbol, PrefixType PT, Unit U, Unit... Us>
|
||||||
struct named_deduced_derived_unit : downcast_child<Child, detail::make_derived_unit<Dim, U, Us...>> {
|
struct named_deduced_derived_unit : downcast_child<Child, detail::deduced_derived_unit<Dim, U, Us...>> {
|
||||||
static constexpr auto symbol = Symbol;
|
static constexpr auto symbol = Symbol;
|
||||||
using prefix_type = PT;
|
using prefix_type = PT;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Child, Dimension Dim, Unit U, Unit... Us>
|
template<typename Child, Dimension Dim, Unit U, Unit... Us>
|
||||||
struct deduced_derived_unit : downcast_child<Child, detail::make_derived_unit<Dim, U, Us...>> {
|
struct deduced_derived_unit : downcast_child<Child, detail::deduced_derived_unit<Dim, U, Us...>> {
|
||||||
static constexpr auto symbol = basic_fixed_string("bbb");
|
static constexpr auto symbol = detail::deduced_symbol_text<U, Us...>(Dim());
|
||||||
using prefix_type = no_prefix;
|
using prefix_type = no_prefix;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user