Downcasting facility refactoring

This commit is contained in:
Mateusz Pusz
2019-11-02 13:54:43 +01:00
parent 219af8e173
commit 2bde0e62b2
9 changed files with 58 additions and 47 deletions
+4 -4
View File
@@ -41,8 +41,8 @@ namespace units {
std::derived_from<T, downcast_base<typename T::base_type>>;
template<typename Target, Downcastable T>
struct downcast_helper : T {
friend auto downcast_guide(typename downcast_helper::downcast_base) { return Target(); }
struct downcast_child : T {
friend auto downcast_guide(typename downcast_child::downcast_base) { return Target(); }
};
namespace detail {
@@ -53,7 +53,7 @@ namespace units {
};
template<typename T>
constexpr auto downcast_target_impl()
constexpr auto downcast_impl()
{
if constexpr(has_downcast<T>)
return decltype(downcast_guide(std::declval<downcast_base<T>>()))();
@@ -64,7 +64,7 @@ namespace units {
}
template<Downcastable T>
using downcast_target = decltype(detail::downcast_target_impl<T>());
using downcast = decltype(detail::downcast_impl<T>());
template<Downcastable T>
using downcast_base_t = T::base_type;
+1 -1
View File
@@ -48,7 +48,7 @@ namespace units {
{
if constexpr(Ratio::num != 1 || Ratio::den != 1) {
if(!std::same_as<PrefixType, no_prefix>) {
using prefix = downcast_target<detail::prefix_base<PrefixType, Ratio>>;
using prefix = downcast<detail::prefix_base<PrefixType, Ratio>>;
if constexpr(!std::same_as<prefix, prefix_base<PrefixType, Ratio>>) {
// print as a prefixed unit
+5 -5
View File
@@ -151,7 +151,7 @@ namespace units {
struct dim_invert_impl;
template<typename... Es>
struct dim_invert_impl<dimension<Es...>> : std::type_identity<downcast_target<dimension<exp_invert<Es>...>>> {};
struct dim_invert_impl<dimension<Es...>> : std::type_identity<downcast<dimension<exp_invert<Es>...>>> {};
}
@@ -221,7 +221,7 @@ namespace units {
// derived_dimension
template<typename Child, Exponent... Es>
struct derived_dimension : downcast_helper<Child, typename detail::make_dimension<Es...>::type> {};
struct derived_dimension : downcast_child<Child, typename detail::make_dimension<Es...>> {};
// merge_dimension
namespace detail {
@@ -243,7 +243,7 @@ namespace units {
struct dimension_multiply_impl;
template<typename... E1, typename... E2>
struct dimension_multiply_impl<dimension<E1...>, dimension<E2...>> : std::type_identity<downcast_target<merge_dimension<dimension<E1...>, dimension<E2...>>>> {};
struct dimension_multiply_impl<dimension<E1...>, dimension<E2...>> : std::type_identity<downcast<merge_dimension<dimension<E1...>, dimension<E2...>>>> {};
}
@@ -273,7 +273,7 @@ namespace units {
struct dimension_sqrt_impl;
template<typename... Es>
struct dimension_sqrt_impl<dimension<Es...>> : std::type_identity<downcast_target<dimension<exp_multiply<Es, 1, 2>...>>> {};
struct dimension_sqrt_impl<dimension<Es...>> : std::type_identity<downcast<dimension<exp_multiply<Es, 1, 2>...>>> {};
}
@@ -287,7 +287,7 @@ namespace units {
struct dimension_pow_impl;
template<typename... Es, std::size_t N>
struct dimension_pow_impl<dimension<Es...>, N> : std::type_identity<downcast_target<dimension<exp_multiply<Es, N, 1>...>>> {};
struct dimension_pow_impl<dimension<Es...>, N> : std::type_identity<downcast<dimension<exp_multiply<Es, N, 1>...>>> {};
}
+2 -2
View File
@@ -39,7 +39,7 @@ namespace units {
{
using dim = dimension_pow<typename U::dimension, N>;
using r = ratio_pow<typename U::ratio, N>;
return quantity<downcast_target<unit<dim, r>>, Rep>(static_cast<Rep>(std::pow(q.count(), N)));
return quantity<downcast<unit<dim, r>>, Rep>(static_cast<Rep>(std::pow(q.count(), N)));
}
template<typename U, typename Rep>
@@ -47,7 +47,7 @@ namespace units {
{
using dim = dimension_sqrt<typename U::dimension>;
using r = ratio_sqrt<typename U::ratio>;
return quantity<downcast_target<unit<dim, r>>, Rep>(static_cast<Rep>(std::sqrt(q.count())));
return quantity<downcast<unit<dim, r>>, Rep>(static_cast<Rep>(std::sqrt(q.count())));
}
} // namespace units
+5 -5
View File
@@ -76,7 +76,7 @@ namespace units {
requires same_dim<typename U1::dimension, typename U2::dimension>
struct common_quantity_impl<quantity<U1, Rep1>, quantity<U2, Rep2>, Rep> {
using type =
quantity<downcast_target<unit<typename U1::dimension, common_ratio<typename U1::ratio, typename U2::ratio>>>,
quantity<downcast<unit<typename U1::dimension, common_ratio<typename U1::ratio, typename U2::ratio>>>,
Rep>;
};
@@ -291,7 +291,7 @@ namespace units {
using dim = quantity::unit::dimension;
if constexpr(!detail::is_dimension<dim>) {
// print as a prefix or ratio of a coherent unit symbol defined by the user
using coherent_unit = downcast_target<units::unit<dim, units::ratio<1>>>;
using coherent_unit = downcast<units::unit<dim, units::ratio<1>>>;
detail::print_prefix_or_ratio<ratio, typename coherent_unit::prefix_type>(os);
os << coherent_unit::symbol;
}
@@ -358,7 +358,7 @@ namespace units {
{
using dim = dimension_multiply<typename U1::dimension, typename U2::dimension>;
using common_rep = decltype(lhs.count() * rhs.count());
using ret = quantity<downcast_target<unit<dim, ratio_multiply<typename U1::ratio, typename U2::ratio>>>, common_rep>;
using ret = quantity<downcast<unit<dim, ratio_multiply<typename U1::ratio, typename U2::ratio>>>, common_rep>;
return ret(lhs.count() * rhs.count());
}
@@ -370,9 +370,9 @@ namespace units {
using dim = dim_invert<typename U::dimension>;
using common_rep = decltype(v / q.count());
using ret = quantity<downcast_target<unit<dim, ratio<U::ratio::den, U::ratio::num>>>, common_rep>;
using den = quantity<U, common_rep>;
return ret(v / den(q).count());
using ret = quantity<downcast<unit<dim, ratio<U::ratio::den, U::ratio::num>>>, common_rep>;
}
template<typename U, typename Rep1, Scalar Rep2>
@@ -407,7 +407,7 @@ namespace units {
using common_rep = decltype(lhs.count() / rhs.count());
using dim = dimension_divide<typename U1::dimension, typename U2::dimension>;
using ret = quantity<downcast_target<unit<dim, ratio_divide<typename U1::ratio, typename U2::ratio>>>, common_rep>;
using ret = quantity<downcast<unit<dim, ratio_divide<typename U1::ratio, typename U2::ratio>>>, common_rep>;
return ret(lhs.count() / rhs.count());
}
+5 -5
View File
@@ -63,7 +63,7 @@ namespace units {
}
template<typename Child, typename PrefixType, Ratio R, basic_fixed_string Symbol>
struct prefix : downcast_helper<Child, detail::prefix_base<PrefixType, R>> {
struct prefix : downcast_child<Child, detail::prefix_base<PrefixType, R>> {
static constexpr auto symbol = Symbol;
};
@@ -150,25 +150,25 @@ namespace units {
struct no_prefix;
template<typename Child, basic_fixed_string Symbol, Dimension D, typename PrefixType = no_prefix>
struct coherent_derived_unit : downcast_helper<Child, unit<D, ratio<1>>> {
struct coherent_derived_unit : downcast_child<Child, unit<D, ratio<1>>> {
static constexpr auto symbol = Symbol;
using prefix_type = PrefixType;
};
template<typename Child, basic_fixed_string Symbol, Dimension D, Ratio R>
struct derived_unit : downcast_helper<Child, unit<D, R>> {
struct derived_unit : downcast_child<Child, unit<D, R>> {
static constexpr auto symbol = Symbol;
};
template<typename Child, Prefix P, Unit U>
requires requires { U::symbol; }
struct prefixed_derived_unit : downcast_helper<Child, unit<typename U::dimension, ratio_multiply<typename P::ratio, typename U::ratio>>> {
struct prefixed_derived_unit : downcast_child<Child, unit<typename U::dimension, ratio_multiply<typename P::ratio, typename U::ratio>>> {
static constexpr auto symbol = P::symbol + U::symbol;
using prefix_type = P::prefix_type;
};
template<typename Child, basic_fixed_string Symbol, Dimension D, Unit U, Unit... Us>
struct deduced_derived_unit : downcast_helper<Child, detail::make_derived_unit<D, U, Us...>> {
struct deduced_derived_unit : downcast_child<Child, detail::make_derived_unit<D, U, Us...>> {
static constexpr auto symbol = Symbol;
};