Basic support for deduced units printing added

This commit is contained in:
Mateusz Pusz
2019-11-10 00:16:41 +00:00
parent 03e3691d47
commit c2e5532ae2
11 changed files with 47 additions and 23 deletions

View File

@ -141,9 +141,13 @@ namespace units {
struct dimension : downcast_base<dimension<Es...>> {};
// 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>;
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
namespace detail {

View File

@ -26,7 +26,7 @@
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>
concept Acceleration = QuantityOf<T, acceleration>;

View File

@ -26,7 +26,7 @@
namespace units {
struct area : derived_dimension<area, exp<base_dim_length, 2>> {};
struct area : derived_dimension<area, exp<length, 2>> {};
template<typename T>
concept Area = QuantityOf<T, area>;

View File

@ -28,7 +28,7 @@
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>
concept ElectricCharge = QuantityOf<T, electric_charge>;

View File

@ -28,7 +28,7 @@
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>
concept Force = QuantityOf<T, force>;

View File

@ -28,7 +28,7 @@
namespace units {
struct frequency : derived_dimension<frequency, exp<base_dim_time, -1>> {};
struct frequency : derived_dimension<frequency, exp<time, -1>> {};
template<typename T>
concept Frequency = QuantityOf<T, frequency>;

View File

@ -28,7 +28,7 @@
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>
concept Power = QuantityOf<T, power>;

View File

@ -27,7 +27,7 @@
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>
concept Velocity = QuantityOf<T, velocity>;

View File

@ -30,7 +30,7 @@
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>
concept Voltage = QuantityOf<T, voltage>;

View File

@ -26,7 +26,7 @@
namespace units {
struct volume : derived_dimension<volume, exp<base_dim_length, 3>> {};
struct volume : derived_dimension<volume, exp<length, 3>> {};
template<typename T>
concept Volume = QuantityOf<T, volume>;

View File

@ -53,7 +53,7 @@ namespace units {
std::is_empty_v<T> &&
detail::is_unit<downcast_base_t<T>>;
// make_derived_unit
// deduced_derived_unit
namespace detail {
@ -110,7 +110,7 @@ namespace units {
};
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()
{
// 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) {
// add root part
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>
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>
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>
@ -263,7 +283,7 @@ namespace units {
}
else {
// 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>
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;
};
@ -292,14 +312,14 @@ namespace units {
};
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;
using prefix_type = PT;
};
template<typename Child, Dimension Dim, Unit U, Unit... Us>
struct deduced_derived_unit : downcast_child<Child, detail::make_derived_unit<Dim, U, Us...>> {
static constexpr auto symbol = basic_fixed_string("bbb");
struct deduced_derived_unit : downcast_child<Child, detail::deduced_derived_unit<Dim, U, Us...>> {
static constexpr auto symbol = detail::deduced_symbol_text<U, Us...>(Dim());
using prefix_type = no_prefix;
};