2 parital specializations of a derived_dimension split to different types

This commit is contained in:
Mateusz Pusz
2019-12-14 11:44:56 +01:00
parent 156ab7ae7f
commit b1b63e1b3a
10 changed files with 152 additions and 166 deletions
+2 -2
View File
@@ -68,7 +68,7 @@ constexpr auto exp_text()
}
template<typename... Us, typename... Es, std::size_t... Idxs>
constexpr auto deduced_symbol_text(derived_dimension<Es...>, std::index_sequence<Idxs...>)
constexpr auto deduced_symbol_text(exp_list<Es...>, std::index_sequence<Idxs...>)
{
return (exp_text<Es, Us::symbol, Idxs>() + ...);
}
@@ -76,7 +76,7 @@ constexpr auto deduced_symbol_text(derived_dimension<Es...>, std::index_sequence
template<DerivedDimension Dim, Unit... Us>
constexpr auto deduced_symbol_text()
{
return deduced_symbol_text<Us...>(Dim(), std::index_sequence_for<Us...>());
return deduced_symbol_text<Us...>(typename Dim::recipe(), std::index_sequence_for<Us...>());
}
} // namespace units::detail
+6 -6
View File
@@ -27,11 +27,11 @@
namespace units::detail {
// same_scaled_units
template<DerivedDimension D, Unit... Us>
template<typename ExpList, Unit... Us>
inline constexpr bool same_scaled_units = false;
template<typename... Es, Unit... Us>
inline constexpr bool same_scaled_units<derived_dimension<Es...>, Us...> = (UnitOf<Us, typename Es::dimension> && ...);
inline constexpr bool same_scaled_units<exp_list<Es...>, Us...> = (UnitOf<Us, typename Es::dimension> && ...);
// deduced_unit
template<typename Result, int UnitExpNum, int UnitExpDen, typename UnitRatio>
@@ -50,17 +50,17 @@ struct ratio_op {
using ratio = ratio_op<calc_ratio, value, UnitExpDen, UnitRatio>::ratio;
};
template<DerivedDimension D, Unit... Us>
template<typename ExpList, Unit... Us>
struct derived_ratio;
template<Unit... Us>
struct derived_ratio<derived_dimension<>, Us...> {
struct derived_ratio<exp_list<>, Us...> {
using ratio = ::units::ratio<1>;
};
template<typename E, typename... ERest, Unit U, Unit... URest>
struct derived_ratio<derived_dimension<E, ERest...>, U, URest...> {
using rest_ratio = derived_ratio<derived_dimension<ERest...>, URest...>::ratio;
struct derived_ratio<exp_list<E, ERest...>, U, URest...> {
using rest_ratio = derived_ratio<exp_list<ERest...>, URest...>::ratio;
using ratio = ratio_op<rest_ratio, E::num, E::den, typename U::ratio>::ratio;
};
+25 -22
View File
@@ -51,7 +51,7 @@ struct equivalent_derived_dim : std::false_type {};
template<typename... Es1, typename... Es2>
requires (sizeof...(Es1) == sizeof...(Es2))
struct equivalent_derived_dim<derived_dimension<Es1...>, derived_dimension<Es2...>> : std::conjunction<equivalent_exp<Es1, Es2>...> {};
struct equivalent_derived_dim<derived_dimension_base<Es1...>, derived_dimension_base<Es2...>> : std::conjunction<equivalent_exp<Es1, Es2>...> {};
template<DerivedDimension D1, DerivedDimension D2>
struct equivalent_dim_impl<D1, D2> : std::disjunction<std::is_same<D1, D2>, equivalent_derived_dim<downcast_base_t<D1>, downcast_base_t<D2>>> {};
@@ -84,7 +84,7 @@ struct check_unknown {
// downcast did not find a user predefined type
template<typename... Es>
struct check_unknown<derived_dimension<Es...>> {
struct check_unknown<derived_dimension_base<Es...>> {
using type = unknown_dimension<Es...>;
};
@@ -114,17 +114,17 @@ struct dim_invert_impl;
template<BaseDimension D>
struct dim_invert_impl<D> {
using type = downcast_dimension<derived_dimension<exp<D, -1>>>;
using type = downcast_dimension<derived_dimension_base<exp<D, -1>>>;
};
template<BaseDimension D>
struct dim_invert_impl<derived_dimension<exp<D, -1>>> {
struct dim_invert_impl<derived_dimension_base<exp<D, -1>>> {
using type = D;
};
template<typename... Es>
struct dim_invert_impl<derived_dimension<Es...>> {
using type = downcast_dimension<derived_dimension<exp_invert<Es>...>>;
struct dim_invert_impl<derived_dimension_base<Es...>> {
using type = downcast_dimension<derived_dimension_base<exp_invert<Es>...>>;
};
template<DerivedDimension D>
@@ -139,18 +139,21 @@ using dim_invert = detail::dim_invert_impl<D>::type;
// dimension_multiply
namespace detail {
template<Dimension D>
struct dim_unpack {
using type = D;
template<typename T>
struct to_dimension;
template<Exponent... Es>
struct to_dimension<exp_list<Es...>> {
using type = derived_dimension_base<Es...>;
};
template<BaseDimension D>
struct dim_unpack<derived_dimension<exp<D, 1>>> {
struct to_dimension<exp_list<exp<D, 1>>> {
using type = D;
};
/**
* @brief Merges 2 sorted derived dimensions into one units::derived_dimension
* @brief Merges 2 sorted derived dimensions into one units::derived_dimension_base
*
* A result of a dimensional calculation may result with many exponents of the same base dimension orginated
* from different parts of the equation. As the exponents lists of both operands it is enough to merge them
@@ -159,19 +162,19 @@ struct dim_unpack<derived_dimension<exp<D, 1>>> {
* dimension itself.
*/
template<Dimension D1, Dimension D2>
using merge_dimension = dim_unpack<typename dim_consolidate<type_list_merge_sorted<D1, D2, exp_less>>::type>::type;
using merge_dimension = to_dimension<typename dim_consolidate<type_list_merge_sorted<typename D1::exponents, typename D2::exponents, exp_less>>::type>::type;
template<Dimension D1, Dimension D2>
struct dimension_multiply_impl;
template<BaseDimension D1, BaseDimension D2>
struct dimension_multiply_impl<D1, D2> {
using type = downcast_dimension<merge_dimension<derived_dimension<exp<D1, 1>>, derived_dimension<exp<D2, 1>>>>;
using type = downcast_dimension<merge_dimension<derived_dimension_base<exp<D1, 1>>, derived_dimension_base<exp<D2, 1>>>>;
};
template<BaseDimension D1, DerivedDimension D2>
struct dimension_multiply_impl<D1, D2> {
using type = downcast_dimension<merge_dimension<derived_dimension<exp<D1, 1>>, typename D2::downcast_base_type>>;
using type = downcast_dimension<merge_dimension<derived_dimension_base<exp<D1, 1>>, typename D2::downcast_base_type>>;
};
template<DerivedDimension D1, BaseDimension D2>
@@ -200,11 +203,11 @@ struct dimension_sqrt_impl;
template<BaseDimension D>
struct dimension_sqrt_impl<D> {
using type = downcast_dimension<derived_dimension<exp<D, 1, 2>>>;
using type = downcast_dimension<derived_dimension_base<exp<D, 1, 2>>>;
};
template<BaseDimension D>
struct dimension_sqrt_impl<derived_dimension<exp<D, 2>>> {
struct dimension_sqrt_impl<derived_dimension_base<exp<D, 2>>> {
using type = D;
};
@@ -214,8 +217,8 @@ struct dimension_sqrt_impl<D> {
};
template<typename... Es>
struct dimension_sqrt_impl<derived_dimension<Es...>> {
using type = downcast_dimension<derived_dimension<exp_multiply<Es, 1, 2>...>>;
struct dimension_sqrt_impl<derived_dimension_base<Es...>> {
using type = downcast_dimension<derived_dimension_base<exp_multiply<Es, 1, 2>...>>;
};
} // namespace detail
@@ -231,7 +234,7 @@ struct dimension_pow_impl;
template<BaseDimension D, std::size_t N>
struct dimension_pow_impl<D, N> {
using type = downcast_dimension<derived_dimension<exp<D, N>>>;
using type = downcast_dimension<derived_dimension_base<exp<D, N>>>;
};
template<BaseDimension D>
@@ -240,7 +243,7 @@ struct dimension_pow_impl<D, 1> {
};
template<BaseDimension D, std::size_t N>
struct dimension_pow_impl<derived_dimension<exp<D, 1, N>>, N> {
struct dimension_pow_impl<derived_dimension_base<exp<D, 1, N>>, N> {
using type = D;
};
@@ -250,8 +253,8 @@ struct dimension_pow_impl<D, N> {
};
template<typename... Es, std::size_t N>
struct dimension_pow_impl<derived_dimension<Es...>, N> {
using type = downcast_dimension<derived_dimension<exp_multiply<Es, N, 1>...>>;
struct dimension_pow_impl<derived_dimension_base<Es...>, N> {
using type = downcast_dimension<derived_dimension_base<exp_multiply<Es, N, 1>...>>;
};
} // namespace detail
+5 -6
View File
@@ -22,7 +22,6 @@
#pragma once
#include <units/bits/deduced_symbol_text.h>
#include <units/derived_dimension.h>
namespace units::detail {
@@ -73,19 +72,19 @@ constexpr auto prefix_or_ratio_text()
}
template<typename... Es, std::size_t... Idxs>
constexpr auto derived_dimension_unit_text(derived_dimension<Es...>, std::index_sequence<Idxs...>)
constexpr auto derived_dimension_unit_text(exp_list<Es...>, std::index_sequence<Idxs...>)
{
return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, Idxs>() + ...);
}
template<typename... Es>
constexpr auto derived_dimension_unit_text(derived_dimension<Es...> d)
constexpr auto derived_dimension_unit_text(exp_list<Es...> list)
{
return derived_dimension_unit_text(d, std::index_sequence_for<Es...>());
return derived_dimension_unit_text(list, std::index_sequence_for<Es...>());
}
template<typename... Es>
constexpr bool all_named(derived_dimension<Es...>)
constexpr bool all_named(exp_list<Es...>)
{
return (dimension_unit<typename Es::dimension>::is_named && ...);
}
@@ -97,7 +96,7 @@ constexpr auto derived_dimension_unit_text()
if constexpr(all_named(recipe()))
return derived_dimension_unit_text(recipe());
else
return derived_dimension_unit_text(Dim());
return derived_dimension_unit_text(typename Dim::exponents());
}
// TODO Inline below concept when switched to gcc-10
+5 -4
View File
@@ -112,7 +112,7 @@ inline constexpr bool is_base_dimension<base_dimension<Name, Params...>> = true;
} // namespace detail
template<typename T>
concept BaseDimension = detail::is_base_dimension<typename T::base_type_workaround>; // TODO Replace with is_derived_from_instantiation when fixed
concept BaseDimension = detail::is_base_dimension<typename T::base_type_workaround>;
// Exponent
namespace detail {
@@ -126,11 +126,12 @@ template<typename T>
concept Exponent = detail::is_exp<T>;
// DerivedDimension
template<typename...>
struct derived_dimension;
template<Exponent E, Exponent... ERest>
requires (BaseDimension<typename E::dimension> && ... && BaseDimension<typename ERest::dimension>)
struct derived_dimension_base;
template<typename T>
concept DerivedDimension = std::is_empty_v<T> && is_instantiation<downcast_base_t<T>, derived_dimension>;
concept DerivedDimension = is_instantiation<downcast_base_t<T>, derived_dimension_base>;
// Dimension
template<typename T>
+62 -66
View File
@@ -31,41 +31,6 @@
namespace units {
/**
* @brief A derived dimension
*
* There are 2 partial specializations of this primary class template. One of them is used by the library engine
* and another one is the interface for the user to define a derived dimension.
*/
template<typename...>
struct derived_dimension;
/**
* @brief Dimensionless quantity
*/
template<>
struct derived_dimension<> : downcast_base<derived_dimension<>> {};
/**
* @brief A dimension of a derived quantity
*
* Expression of the dependence of a quantity on the base quantities (and their base dimensions) of a system of
* quantities as a product of powers of factors corresponding to the base quantities, omitting any numerical factors.
* A power of a factor is the factor raised to an exponent.
*
* A derived dimension can be formed from multiple exponents (i.e. velocity is represented as "exp<L, 1>, exp<T, -1>").
* It is also possible to form a derived dimension with only one exponent (i.e. frequency is represented as just
* "exp<T, -1>").
*
* @note This partial class template specialization is used by the library engine and should not be directly instantiated
* by the user (see the other partial specialization).
*
* @tparam E a first exponent of a derived dimension
* @tparam ERest zero or more following exponents of a derived dimension
*/
template<Exponent E, Exponent... ERest>
struct derived_dimension<E, ERest...> : downcast_base<derived_dimension<E, ERest...>> {}; // TODO rename to 'dimension'?
/**
* @brief A power of factor corresponding to the dimension of a quantity
*
@@ -119,6 +84,31 @@ struct exp_multiply_impl {
template<Exponent E, int Num, int Den>
using exp_multiply = detail::exp_multiply_impl<E, Num, Den>::type;
template<Exponent... Es>
struct exp_list {};
/**
* @brief A dimension of a derived quantity
*
* Expression of the dependence of a quantity on the base quantities (and their base dimensions) of a system of
* quantities as a product of powers of factors corresponding to the base quantities, omitting any numerical factors.
* A power of a factor is the factor raised to an exponent.
*
* A derived dimension can be formed from multiple exponents (i.e. velocity is represented as "exp<L, 1>, exp<T, -1>").
* It is also possible to form a derived dimension with only one exponent (i.e. frequency is represented as just
* "exp<T, -1>").
*
* @note This class template is used by the library engine and should not be directly instantiated by the user.
*
* @tparam E a first exponent of a derived dimension
* @tparam ERest zero or more following exponents of a derived dimension
*/
template<Exponent E, Exponent... ERest>
requires (BaseDimension<typename E::dimension> && ... && BaseDimension<typename ERest::dimension>)
struct derived_dimension_base : downcast_base<derived_dimension_base<E, ERest...>> {
using exponents = exp_list<E, ERest...>;
};
// make_dimension
namespace detail {
@@ -130,32 +120,32 @@ namespace detail {
*
* @tparam D derived dimension to consolidate
*/
template<DerivedDimension D>
template<typename ExpList>
struct dim_consolidate;
template<>
struct dim_consolidate<derived_dimension<>> {
using type = derived_dimension<>;
struct dim_consolidate<exp_list<>> {
using type = exp_list<>;
};
template<typename E>
struct dim_consolidate<derived_dimension<E>> {
using type = derived_dimension<E>;
struct dim_consolidate<exp_list<E>> {
using type = exp_list<E>;
};
template<typename E1, typename... ERest>
struct dim_consolidate<derived_dimension<E1, ERest...>> {
using type = type_list_push_front<typename dim_consolidate<derived_dimension<ERest...>>::type, E1>;
struct dim_consolidate<exp_list<E1, ERest...>> {
using type = type_list_push_front<typename dim_consolidate<exp_list<ERest...>>::type, E1>;
};
template<BaseDimension Dim, int Num1, int Den1, int Num2, int Den2, typename... ERest>
struct dim_consolidate<derived_dimension<exp<Dim, Num1, Den1>, exp<Dim, Num2, Den2>, ERest...>> {
struct dim_consolidate<exp_list<exp<Dim, Num1, Den1>, exp<Dim, Num2, Den2>, ERest...>> {
// TODO: provide custom implementation for ratio_add
using r1 = std::ratio<Num1, Den1>;
using r2 = std::ratio<Num2, Den2>;
using r = std::ratio_add<r1, r2>;
using type = conditional<r::num == 0, typename dim_consolidate<derived_dimension<ERest...>>::type,
typename dim_consolidate<derived_dimension<exp<Dim, r::num, r::den>, ERest...>>::type>;
using type = conditional<r::num == 0, typename dim_consolidate<exp_list<ERest...>>::type,
typename dim_consolidate<exp_list<exp<Dim, r::num, r::den>, ERest...>>::type>;
};
/**
@@ -168,7 +158,7 @@ struct extract;
template<>
struct extract<> {
using type = derived_dimension<>;
using type = exp_list<>;
};
template<BaseDimension Dim, int Num, int Den, Exponent... ERest>
@@ -182,12 +172,21 @@ struct extract<exp<Dim, Num, Den>, ERest...> {
};
template<Exponent... Es, int Num, int Den, Exponent... ERest>
struct extract<exp<derived_dimension<Es...>, Num, Den>, ERest...> {
struct extract<exp<derived_dimension_base<Es...>, Num, Den>, ERest...> {
using type = type_list_push_front<typename extract<ERest...>::type, exp_multiply<Es, Num, Den>...>;
};
template<typename T>
struct to_derived_dimension_base;
template<Exponent... Es>
struct to_derived_dimension_base<exp_list<Es...>> {
using type = derived_dimension_base<Es...>;
};
/**
* @brief Converts user provided derived dimension specification into a valid units::derived_dimension definition
* @brief Converts user provided derived dimension specification into a valid units::derived_dimension_base definition
*
* User provided definition of a derived dimension may contain the same base dimension repeated more than once on the
* list possibly hidden in other derived units provided by the user. The process here should:
@@ -197,7 +196,7 @@ struct extract<exp<derived_dimension<Es...>, Num, Den>, ERest...> {
* this base dimension.
*/
template<Exponent... Es>
using make_dimension = dim_consolidate<type_list_sort<typename extract<Es...>::type, exp_less>>::type;
using make_dimension = to_derived_dimension_base<typename dim_consolidate<type_list_sort<typename extract<Es...>::type, exp_less>>::type>::type;
template<Exponent E>
requires (E::den == 1 || E::den == 2) // TODO provide support for any den
@@ -209,31 +208,31 @@ struct exp_ratio {
using type = conditional<E::den == 2, ratio_sqrt<pow>, pow>;
};
template<DerivedDimension D>
template<typename ExpList>
struct base_units_ratio_impl;
template<typename E, typename... Es>
struct base_units_ratio_impl<derived_dimension<E, Es...>> {
using type = ratio_multiply<typename exp_ratio<E>::type, typename base_units_ratio_impl<derived_dimension<Es...>>::type>;
struct base_units_ratio_impl<exp_list<E, Es...>> {
using type = ratio_multiply<typename exp_ratio<E>::type, typename base_units_ratio_impl<exp_list<Es...>>::type>;
};
template<typename E>
struct base_units_ratio_impl<derived_dimension<E>> {
struct base_units_ratio_impl<exp_list<E>> {
using type = exp_ratio<E>::type;
};
/**
* @brief Calculates the common ratio of all the references of base units in the derived dimension
*/
template<DerivedDimension D>
using base_units_ratio = base_units_ratio_impl<D>::type;
template<typename D>
using base_units_ratio = base_units_ratio_impl<typename D::exponents>::type;
} // namespace detail
/**
* @brief The list of exponents of dimensions (both base and derived) provided by the user
*
* This is the primary interface to create derived dimensions. Exponents list can contain powers of factors of both
* This is the user's interface to create derived dimensions. Exponents list can contain powers of factors of both
* base and derived dimensions. This is called a "recipe" of the dimension and among others is used to print
* unnamed coherent units of this dimension.
*
@@ -241,21 +240,18 @@ using base_units_ratio = base_units_ratio_impl<D>::type;
* of powers of base units with no other proportionality factor than one.
*
* The implementation is responsible for unpacking all of the dimensions into a list containing only base dimensions
* and their factors and putting them to the other (private) units::derived_dimension class template partial
* specialization.
*
* @note User should always use only this partial specialization to create derived dimensions.
* and their factors and putting them to derived_dimension_base class template.
*
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
* @tparam E The list of exponents of ingredient dimensions
* @tparam ERest The list of exponents of ingredient dimensions
* @tparam U a coherent unit of a derived dimension
* @tparam E the list of exponents of ingredient dimensions
* @tparam ERest the list of exponents of ingredient dimensions
*/
template<typename Child, Unit U, Exponent E, Exponent... ERest>
requires (!Exponent<Child>)
struct derived_dimension<Child, U, E, ERest...> : downcast_child<Child, typename detail::make_dimension<E, ERest...>> {
using recipe = derived_dimension<E, ERest...>;
struct derived_dimension : downcast_child<Child, typename detail::make_dimension<E, ERest...>> {
using recipe = exp_list<E, ERest...>;
using coherent_unit = U;
using base_units_ratio = detail::base_units_ratio<typename downcast_child<Child, typename detail::make_dimension<E, ERest...>>::downcast_base_type>;
using base_units_ratio = detail::base_units_ratio<derived_dimension>;
};
} // namespace units
+1 -1
View File
@@ -164,7 +164,7 @@ template<typename Child, DerivedDimension Dim, Unit U, Unit... URest>
(U::is_named && (URest::is_named && ... && true))
struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> {
static constexpr bool is_named = false;
static constexpr auto symbol = detail::deduced_symbol_text<typename Dim::recipe, U, URest...>();
static constexpr auto symbol = detail::deduced_symbol_text<Dim, U, URest...>();
using prefix_type = no_prefix;
};