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

View File

@@ -68,7 +68,7 @@ constexpr auto exp_text()
} }
template<typename... Us, typename... Es, std::size_t... Idxs> 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>() + ...); 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> template<DerivedDimension Dim, Unit... Us>
constexpr auto deduced_symbol_text() 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 } // namespace units::detail

View File

@@ -27,11 +27,11 @@
namespace units::detail { namespace units::detail {
// same_scaled_units // same_scaled_units
template<DerivedDimension D, Unit... Us> template<typename ExpList, Unit... Us>
inline constexpr bool same_scaled_units = false; inline constexpr bool same_scaled_units = false;
template<typename... Es, Unit... Us> 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 // deduced_unit
template<typename Result, int UnitExpNum, int UnitExpDen, typename UnitRatio> 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; using ratio = ratio_op<calc_ratio, value, UnitExpDen, UnitRatio>::ratio;
}; };
template<DerivedDimension D, Unit... Us> template<typename ExpList, Unit... Us>
struct derived_ratio; struct derived_ratio;
template<Unit... Us> template<Unit... Us>
struct derived_ratio<derived_dimension<>, Us...> { struct derived_ratio<exp_list<>, Us...> {
using ratio = ::units::ratio<1>; using ratio = ::units::ratio<1>;
}; };
template<typename E, typename... ERest, Unit U, Unit... URest> template<typename E, typename... ERest, Unit U, Unit... URest>
struct derived_ratio<derived_dimension<E, ERest...>, U, URest...> { struct derived_ratio<exp_list<E, ERest...>, U, URest...> {
using rest_ratio = derived_ratio<derived_dimension<ERest...>, URest...>::ratio; using rest_ratio = derived_ratio<exp_list<ERest...>, URest...>::ratio;
using ratio = ratio_op<rest_ratio, E::num, E::den, typename U::ratio>::ratio; using ratio = ratio_op<rest_ratio, E::num, E::den, typename U::ratio>::ratio;
}; };

View File

@@ -51,7 +51,7 @@ struct equivalent_derived_dim : std::false_type {};
template<typename... Es1, typename... Es2> template<typename... Es1, typename... Es2>
requires (sizeof...(Es1) == sizeof...(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> 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>>> {}; 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 // downcast did not find a user predefined type
template<typename... Es> template<typename... Es>
struct check_unknown<derived_dimension<Es...>> { struct check_unknown<derived_dimension_base<Es...>> {
using type = unknown_dimension<Es...>; using type = unknown_dimension<Es...>;
}; };
@@ -114,17 +114,17 @@ struct dim_invert_impl;
template<BaseDimension D> template<BaseDimension D>
struct dim_invert_impl<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> 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; using type = D;
}; };
template<typename... Es> template<typename... Es>
struct dim_invert_impl<derived_dimension<Es...>> { struct dim_invert_impl<derived_dimension_base<Es...>> {
using type = downcast_dimension<derived_dimension<exp_invert<Es>...>>; using type = downcast_dimension<derived_dimension_base<exp_invert<Es>...>>;
}; };
template<DerivedDimension D> template<DerivedDimension D>
@@ -139,18 +139,21 @@ using dim_invert = detail::dim_invert_impl<D>::type;
// dimension_multiply // dimension_multiply
namespace detail { namespace detail {
template<Dimension D> template<typename T>
struct dim_unpack { struct to_dimension;
using type = D;
template<Exponent... Es>
struct to_dimension<exp_list<Es...>> {
using type = derived_dimension_base<Es...>;
}; };
template<BaseDimension D> template<BaseDimension D>
struct dim_unpack<derived_dimension<exp<D, 1>>> { struct to_dimension<exp_list<exp<D, 1>>> {
using type = D; 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 * 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 * 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. * dimension itself.
*/ */
template<Dimension D1, Dimension D2> 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> template<Dimension D1, Dimension D2>
struct dimension_multiply_impl; struct dimension_multiply_impl;
template<BaseDimension D1, BaseDimension D2> template<BaseDimension D1, BaseDimension D2>
struct dimension_multiply_impl<D1, 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> template<BaseDimension D1, DerivedDimension D2>
struct dimension_multiply_impl<D1, 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> template<DerivedDimension D1, BaseDimension D2>
@@ -200,11 +203,11 @@ struct dimension_sqrt_impl;
template<BaseDimension D> template<BaseDimension D>
struct dimension_sqrt_impl<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> 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; using type = D;
}; };
@@ -214,8 +217,8 @@ struct dimension_sqrt_impl<D> {
}; };
template<typename... Es> template<typename... Es>
struct dimension_sqrt_impl<derived_dimension<Es...>> { struct dimension_sqrt_impl<derived_dimension_base<Es...>> {
using type = downcast_dimension<derived_dimension<exp_multiply<Es, 1, 2>...>>; using type = downcast_dimension<derived_dimension_base<exp_multiply<Es, 1, 2>...>>;
}; };
} // namespace detail } // namespace detail
@@ -231,7 +234,7 @@ struct dimension_pow_impl;
template<BaseDimension D, std::size_t N> template<BaseDimension D, std::size_t N>
struct dimension_pow_impl<D, 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> template<BaseDimension D>
@@ -240,7 +243,7 @@ struct dimension_pow_impl<D, 1> {
}; };
template<BaseDimension D, std::size_t N> 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; using type = D;
}; };
@@ -250,8 +253,8 @@ struct dimension_pow_impl<D, N> {
}; };
template<typename... Es, std::size_t N> template<typename... Es, std::size_t N>
struct dimension_pow_impl<derived_dimension<Es...>, N> { struct dimension_pow_impl<derived_dimension_base<Es...>, N> {
using type = downcast_dimension<derived_dimension<exp_multiply<Es, N, 1>...>>; using type = downcast_dimension<derived_dimension_base<exp_multiply<Es, N, 1>...>>;
}; };
} // namespace detail } // namespace detail

View File

@@ -22,7 +22,6 @@
#pragma once #pragma once
#include <units/bits/deduced_symbol_text.h>
#include <units/derived_dimension.h> #include <units/derived_dimension.h>
namespace units::detail { namespace units::detail {
@@ -73,19 +72,19 @@ constexpr auto prefix_or_ratio_text()
} }
template<typename... Es, std::size_t... Idxs> 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>() + ...); return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, Idxs>() + ...);
} }
template<typename... Es> 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> 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 && ...); return (dimension_unit<typename Es::dimension>::is_named && ...);
} }
@@ -97,7 +96,7 @@ constexpr auto derived_dimension_unit_text()
if constexpr(all_named(recipe())) if constexpr(all_named(recipe()))
return derived_dimension_unit_text(recipe()); return derived_dimension_unit_text(recipe());
else else
return derived_dimension_unit_text(Dim()); return derived_dimension_unit_text(typename Dim::exponents());
} }
// TODO Inline below concept when switched to gcc-10 // TODO Inline below concept when switched to gcc-10

View File

@@ -112,7 +112,7 @@ inline constexpr bool is_base_dimension<base_dimension<Name, Params...>> = true;
} // namespace detail } // namespace detail
template<typename T> 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 // Exponent
namespace detail { namespace detail {
@@ -126,11 +126,12 @@ template<typename T>
concept Exponent = detail::is_exp<T>; concept Exponent = detail::is_exp<T>;
// DerivedDimension // DerivedDimension
template<typename...> template<Exponent E, Exponent... ERest>
struct derived_dimension; requires (BaseDimension<typename E::dimension> && ... && BaseDimension<typename ERest::dimension>)
struct derived_dimension_base;
template<typename T> 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 // Dimension
template<typename T> template<typename T>

View File

@@ -31,41 +31,6 @@
namespace units { 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 * @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> template<Exponent E, int Num, int Den>
using exp_multiply = detail::exp_multiply_impl<E, Num, Den>::type; 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 // make_dimension
namespace detail { namespace detail {
@@ -130,32 +120,32 @@ namespace detail {
* *
* @tparam D derived dimension to consolidate * @tparam D derived dimension to consolidate
*/ */
template<DerivedDimension D> template<typename ExpList>
struct dim_consolidate; struct dim_consolidate;
template<> template<>
struct dim_consolidate<derived_dimension<>> { struct dim_consolidate<exp_list<>> {
using type = derived_dimension<>; using type = exp_list<>;
}; };
template<typename E> template<typename E>
struct dim_consolidate<derived_dimension<E>> { struct dim_consolidate<exp_list<E>> {
using type = derived_dimension<E>; using type = exp_list<E>;
}; };
template<typename E1, typename... ERest> template<typename E1, typename... ERest>
struct dim_consolidate<derived_dimension<E1, ERest...>> { struct dim_consolidate<exp_list<E1, ERest...>> {
using type = type_list_push_front<typename dim_consolidate<derived_dimension<ERest...>>::type, E1>; 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> 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 // TODO: provide custom implementation for ratio_add
using r1 = std::ratio<Num1, Den1>; using r1 = std::ratio<Num1, Den1>;
using r2 = std::ratio<Num2, Den2>; using r2 = std::ratio<Num2, Den2>;
using r = std::ratio_add<r1, r2>; using r = std::ratio_add<r1, r2>;
using type = conditional<r::num == 0, typename dim_consolidate<derived_dimension<ERest...>>::type, using type = conditional<r::num == 0, typename dim_consolidate<exp_list<ERest...>>::type,
typename dim_consolidate<derived_dimension<exp<Dim, r::num, r::den>, ERest...>>::type>; typename dim_consolidate<exp_list<exp<Dim, r::num, r::den>, ERest...>>::type>;
}; };
/** /**
@@ -168,7 +158,7 @@ struct extract;
template<> template<>
struct extract<> { struct extract<> {
using type = derived_dimension<>; using type = exp_list<>;
}; };
template<BaseDimension Dim, int Num, int Den, Exponent... ERest> 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> 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>...>; 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 * 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: * 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. * this base dimension.
*/ */
template<Exponent... Es> 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> template<Exponent E>
requires (E::den == 1 || E::den == 2) // TODO provide support for any den 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>; using type = conditional<E::den == 2, ratio_sqrt<pow>, pow>;
}; };
template<DerivedDimension D> template<typename ExpList>
struct base_units_ratio_impl; struct base_units_ratio_impl;
template<typename E, typename... Es> template<typename E, typename... Es>
struct base_units_ratio_impl<derived_dimension<E, Es...>> { struct base_units_ratio_impl<exp_list<E, Es...>> {
using type = ratio_multiply<typename exp_ratio<E>::type, typename base_units_ratio_impl<derived_dimension<Es...>>::type>; using type = ratio_multiply<typename exp_ratio<E>::type, typename base_units_ratio_impl<exp_list<Es...>>::type>;
}; };
template<typename E> 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; using type = exp_ratio<E>::type;
}; };
/** /**
* @brief Calculates the common ratio of all the references of base units in the derived dimension * @brief Calculates the common ratio of all the references of base units in the derived dimension
*/ */
template<DerivedDimension D> template<typename D>
using base_units_ratio = base_units_ratio_impl<D>::type; using base_units_ratio = base_units_ratio_impl<typename D::exponents>::type;
} // namespace detail } // namespace detail
/** /**
* @brief The list of exponents of dimensions (both base and derived) provided by the user * @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 * 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. * 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. * 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 * 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 * and their factors and putting them to derived_dimension_base class template.
* specialization.
*
* @note User should always use only this partial specialization to create derived dimensions.
* *
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom) * @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
* @tparam E The list of exponents of ingredient dimensions * @tparam U a coherent unit of a derived dimension
* @tparam ERest The list of exponents of ingredient dimensions * @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> template<typename Child, Unit U, Exponent E, Exponent... ERest>
requires (!Exponent<Child>) struct derived_dimension : downcast_child<Child, typename detail::make_dimension<E, ERest...>> {
struct derived_dimension<Child, U, E, ERest...> : downcast_child<Child, typename detail::make_dimension<E, ERest...>> { using recipe = exp_list<E, ERest...>;
using recipe = derived_dimension<E, ERest...>;
using coherent_unit = U; 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 } // namespace units

View File

@@ -164,7 +164,7 @@ template<typename Child, DerivedDimension Dim, Unit U, Unit... URest>
(U::is_named && (URest::is_named && ... && true)) (U::is_named && (URest::is_named && ... && true))
struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> { struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> {
static constexpr bool is_named = false; 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; using prefix_type = no_prefix;
}; };

View File

@@ -28,13 +28,13 @@ using namespace units;
namespace { namespace {
struct u0 : unit<u0> {}; struct u0 : named_unit<u0, "u0", no_prefix> {};
struct d0 : base_dimension<"d0", u0> {}; struct d0 : base_dimension<"d0", u0> {};
struct u1 : unit<u1> {}; struct u1 : named_unit<u1, "u1", no_prefix> {};
struct d1 : base_dimension<"d1", u1> {}; struct d1 : base_dimension<"d1", u1> {};
struct u2 : unit<u2> {}; struct u2 : named_unit<u2, "u2", no_prefix> {};
struct d2 : base_dimension<"d2", u2> {}; struct d2 : base_dimension<"d2", u2> {};
struct u3 : unit<u3> {}; struct u3 : named_unit<u3, "u3", no_prefix> {};
struct d3 : base_dimension<"d3", u3> {}; struct d3 : base_dimension<"d3", u3> {};
// exp_invert // exp_invert
@@ -50,77 +50,67 @@ struct typeinfo;
template<typename... Ts> template<typename... Ts>
using extract = detail::extract<Ts...>::type; using extract = detail::extract<Ts...>::type;
static_assert(std::is_same_v<extract<>, derived_dimension<>>); static_assert(std::is_same_v<extract<>, exp_list<>>);
static_assert(std::is_same_v<extract<exp<d0, 1>>, derived_dimension<exp<d0, 1>>>); static_assert(std::is_same_v<extract<exp<d0, 1>>, exp_list<exp<d0, 1>>>);
static_assert(std::is_same_v<extract<exp<d0, 1>, exp<d1, 2>>, derived_dimension<exp<d0, 1>, exp<d1, 2>>>); static_assert(std::is_same_v<extract<exp<d0, 1>, exp<d1, 2>>, exp_list<exp<d0, 1>, exp<d1, 2>>>);
using dim0 = derived_dimension<>; using dim1 = derived_dimension_base<exp<d0, 1>>;
using dim1 = derived_dimension<exp<d0, 1>>; using dim2 = derived_dimension_base<exp<d0, 1>, exp<d1, 2>>;
using dim2 = derived_dimension<exp<d0, 1>, exp<d1, 2>>; static_assert(std::is_same_v<extract<exp<dim1, 2>, exp<d0, 1>>, exp_list<exp<d0, 2>, exp<d0, 1>>>);
static_assert(std::is_same_v<extract<exp<dim0, 2>, exp<d0, 1>>, derived_dimension<exp<d0, 1>>>);
static_assert(std::is_same_v<extract<exp<dim1, 2>, exp<d0, 1>>, derived_dimension<exp<d0, 2>, exp<d0, 1>>>);
static_assert(std::is_same_v<extract<exp<dim2, -2>, exp<d0, 1>, exp<d1, 2>>, static_assert(std::is_same_v<extract<exp<dim2, -2>, exp<d0, 1>, exp<d1, 2>>,
derived_dimension<exp<d0, -2>, exp<d1, -4>, exp<d0, 1>, exp<d1, 2>>>); exp_list<exp<d0, -2>, exp<d1, -4>, exp<d0, 1>, exp<d1, 2>>>);
// dim_invert // dim_invert
static_assert(std::is_same_v<dim_invert<derived_dimension<exp<d0, -1>>>, d0>); static_assert(std::is_same_v<dim_invert<derived_dimension_base<exp<d0, -1>>>, d0>);
static_assert(std::is_same_v<dim_invert<derived_dimension<exp<d0, -2>>>, unknown_dimension<exp<d0, 2>>>); static_assert(std::is_same_v<dim_invert<derived_dimension_base<exp<d0, -2>>>, unknown_dimension<exp<d0, 2>>>);
static_assert( static_assert(
std::is_same_v<dim_invert<derived_dimension<exp<d0, 2>, exp<d1, -1>>>, unknown_dimension<exp<d0, -2>, exp<d1, 1>>>); std::is_same_v<dim_invert<derived_dimension_base<exp<d0, 2>, exp<d1, -1>>>, unknown_dimension<exp<d0, -2>, exp<d1, 1>>>);
// make_dimension // make_dimension
template<typename... Ts> template<typename... Ts>
using make_dimension = detail::make_dimension<Ts...>; using make_dimension = detail::make_dimension<Ts...>;
static_assert(std::is_same_v<make_dimension<exp<d0, 1>>, derived_dimension<exp<d0, 1>>>); static_assert(std::is_same_v<make_dimension<exp<d0, 1>>, derived_dimension_base<exp<d0, 1>>>);
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>>, derived_dimension<exp<d0, 1>, exp<d1, 1>>>); static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>>, derived_dimension_base<exp<d0, 1>, exp<d1, 1>>>);
static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d0, 1>>, derived_dimension<exp<d0, 1>, exp<d1, 1>>>); static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d0, 1>>, derived_dimension_base<exp<d0, 1>, exp<d1, 1>>>);
static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d1, 1>>, derived_dimension<exp<d1, 2>>>); static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d1, 1>>, derived_dimension_base<exp<d1, 2>>>);
static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d1, -1>>, derived_dimension<>>); static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d1, 1, 2>>, derived_dimension_base<exp<d1, 3, 2>>>);
static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d1, 1, 2>>, derived_dimension<exp<d1, 3, 2>>>); static_assert(std::is_same_v<make_dimension<exp<d1, 1, 2>, exp<d1, 1, 2>>, derived_dimension_base<exp<d1, 1>>>);
static_assert(std::is_same_v<make_dimension<exp<d1, 1, 2>, exp<d1, 1, 2>>, derived_dimension<exp<d1, 1>>>); static_assert(std::is_same_v<make_dimension<exp<d1, 2>, exp<d1, 1, 2>>, derived_dimension_base<exp<d1, 5, 2>>>);
static_assert(std::is_same_v<make_dimension<exp<d1, 2>, exp<d1, 1, 2>>, derived_dimension<exp<d1, 5, 2>>>);
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d0, 1>, exp<d1, 1>>, static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d0, 1>, exp<d1, 1>>,
derived_dimension<exp<d0, 2>, exp<d1, 2>>>); derived_dimension_base<exp<d0, 2>, exp<d1, 2>>>);
static_assert(std::is_same_v<make_dimension<exp<d0, -1>, exp<d1, -1>, exp<d0, -1>, exp<d1, -1>>, static_assert(std::is_same_v<make_dimension<exp<d0, -1>, exp<d1, -1>, exp<d0, -1>, exp<d1, -1>>,
derived_dimension<exp<d0, -2>, exp<d1, -2>>>); derived_dimension_base<exp<d0, -2>, exp<d1, -2>>>);
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d1, -1>>, derived_dimension<exp<d0, 1>>>); static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d1, -1>>, derived_dimension_base<exp<d0, 1>>>);
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d0, -1>, exp<d1, 1>>, derived_dimension<exp<d1, 1>>>); static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d0, -1>, exp<d1, 1>>, derived_dimension_base<exp<d1, 1>>>);
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d0, -1>>, derived_dimension<exp<d1, 1>>>); static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d0, -1>>, derived_dimension_base<exp<d1, 1>>>);
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d0, -1>, exp<d1, -1>>, derived_dimension<>>);
// dimension_multiply // dimension_multiply
static_assert(std::is_same_v<dimension_multiply<derived_dimension<exp<d0, 1>>, derived_dimension<exp<d1, 1>>>, static_assert(std::is_same_v<dimension_multiply<derived_dimension_base<exp<d0, 1>>, derived_dimension_base<exp<d1, 1>>>,
unknown_dimension<exp<d0, 1>, exp<d1, 1>>>); unknown_dimension<exp<d0, 1>, exp<d1, 1>>>);
static_assert( static_assert(
std::is_same_v<dimension_multiply<derived_dimension<exp<d0, 1>>, d1>, unknown_dimension<exp<d0, 1>, exp<d1, 1>>>); std::is_same_v<dimension_multiply<derived_dimension_base<exp<d0, 1>>, d1>, unknown_dimension<exp<d0, 1>, exp<d1, 1>>>);
static_assert( static_assert(
std::is_same_v<dimension_multiply<d0, derived_dimension<exp<d1, 1>>>, unknown_dimension<exp<d0, 1>, exp<d1, 1>>>); std::is_same_v<dimension_multiply<d0, derived_dimension_base<exp<d1, 1>>>, unknown_dimension<exp<d0, 1>, exp<d1, 1>>>);
static_assert(std::is_same_v<dimension_multiply<d0, d1>, unknown_dimension<exp<d0, 1>, exp<d1, 1>>>); static_assert(std::is_same_v<dimension_multiply<d0, d1>, unknown_dimension<exp<d0, 1>, exp<d1, 1>>>);
static_assert(std::is_same_v< static_assert(std::is_same_v<
dimension_multiply<derived_dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, derived_dimension<exp<d3, 1>>>, dimension_multiply<derived_dimension_base<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, derived_dimension_base<exp<d3, 1>>>,
unknown_dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>, exp<d3, 1>>>); unknown_dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>, exp<d3, 1>>>);
static_assert(std::is_same_v< static_assert(std::is_same_v<
dimension_multiply<derived_dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, derived_dimension<exp<d1, 1>>>, dimension_multiply<derived_dimension_base<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, derived_dimension_base<exp<d1, 1>>>,
unknown_dimension<exp<d0, 1>, exp<d1, 2>, exp<d2, 1>>>); unknown_dimension<exp<d0, 1>, exp<d1, 2>, exp<d2, 1>>>);
static_assert(std::is_same_v< static_assert(std::is_same_v<
dimension_multiply<derived_dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, derived_dimension<exp<d1, -1>>>, dimension_multiply<derived_dimension_base<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, derived_dimension_base<exp<d1, -1>>>,
unknown_dimension<exp<d0, 1>, exp<d2, 1>>>); unknown_dimension<exp<d0, 1>, exp<d2, 1>>>);
static_assert(std::is_same_v<dimension_multiply<derived_dimension<exp<d0, 1>>, derived_dimension<exp<d0, -1>>>, static_assert(std::is_same_v<dimension_multiply<derived_dimension_base<exp<d0, 2>>, derived_dimension_base<exp<d0, -1>>>, d0>);
unknown_dimension<>>);
static_assert(std::is_same_v<dimension_multiply<derived_dimension<exp<d0, 2>>, derived_dimension<exp<d0, -1>>>, d0>);
// dimension_divide // dimension_divide
static_assert(std::is_same_v<dimension_divide<derived_dimension<exp<d0, 1>>, derived_dimension<exp<d1, 1>>>, static_assert(std::is_same_v<dimension_divide<derived_dimension_base<exp<d0, 1>>, derived_dimension_base<exp<d1, 1>>>,
unknown_dimension<exp<d0, 1>, exp<d1, -1>>>); unknown_dimension<exp<d0, 1>, exp<d1, -1>>>);
static_assert(std::is_same_v<dimension_divide<derived_dimension<exp<d0, 1>>, derived_dimension<exp<d0, 1>>>, static_assert(std::is_same_v<dimension_divide<derived_dimension_base<exp<d0, 2>>, unknown_dimension<exp<d0, 1>>>, d0>);
unknown_dimension<>>);
static_assert(std::is_same_v<dimension_divide<derived_dimension<exp<d0, 1>>, derived_dimension<exp<d0, 1>>>,
unknown_dimension<>>);
static_assert(std::is_same_v<dimension_divide<derived_dimension<exp<d0, 2>>, unknown_dimension<exp<d0, 1>>>, d0>);
} // namespace } // namespace

View File

@@ -237,15 +237,13 @@ static_assert(
std::is_same_v<decltype(velocity<metre_per_second, int>() * si::time<second, int>()), length<metre, int>>); std::is_same_v<decltype(velocity<metre_per_second, int>() * si::time<second, int>()), length<metre, int>>);
static_assert( static_assert(
std::is_same_v<decltype(velocity<metre_per_second, int>() * si::time<hour, int>()), length<scaled_unit<metre, ratio<3600>>, int>>); std::is_same_v<decltype(velocity<metre_per_second, int>() * si::time<hour, int>()), length<scaled_unit<metre, ratio<3600>>, int>>);
// TODO uncomment below when fixed in gcc static_assert(std::is_same_v<decltype(length<metre>() * si::time<minute>()),
// static_assert(std::is_same_v<decltype(length<metre>() * si::time<minute>()), quantity<unknown_dimension<units::exp<dim_length, 1>, units::exp<dim_time, 1>>, scaled_unit<unknown_unit, ratio<60>>>>);
// quantity<derived_dimension<exp<dim_length, 1>, exp<dim_time, 1>>, scaled_unit<unknown_unit, ratio<60>>>>);
static_assert(std::is_same_v<decltype(1 / si::time<second, int>()), frequency<hertz, int>>); static_assert(std::is_same_v<decltype(1 / si::time<second, int>()), frequency<hertz, int>>);
static_assert(std::is_same_v<decltype(1 / si::time<minute, int>()), frequency<scaled_unit<hertz, ratio<1, 60>>, int>>); static_assert(std::is_same_v<decltype(1 / si::time<minute, int>()), frequency<scaled_unit<hertz, ratio<1, 60>>, int>>);
static_assert(std::is_same_v<decltype(1 / frequency<hertz, int>()), si::time<second, int>>); static_assert(std::is_same_v<decltype(1 / frequency<hertz, int>()), si::time<second, int>>);
// TODO uncomment below when fixed in gcc static_assert(std::is_same_v<decltype(1 / length<kilometre>()),
// static_assert(std::is_same_v<decltype(1 / length<kilometre>()), quantity<unknown_dimension<units::exp<dim_length, -1>>, scaled_unit<unknown_unit, ratio<1, 1000>>>>);
// quantity<derived_dimension<exp<dim_length, -1>>, scaled_unit<unknown_unit, ratio<1, 1000>>>>);
static_assert(std::is_same_v<decltype(length<metre, int>() / 1.0), length<metre, double>>); static_assert(std::is_same_v<decltype(length<metre, int>() / 1.0), length<metre, double>>);
static_assert(std::is_same_v<decltype(length<metre, int>() / length<metre, double>()), double>); static_assert(std::is_same_v<decltype(length<metre, int>() / length<metre, double>()), double>);
static_assert(std::is_same_v<decltype(length<kilometre, int>() / length<metre, double>()), double>); static_assert(std::is_same_v<decltype(length<kilometre, int>() / length<metre, double>()), double>);
@@ -253,9 +251,8 @@ static_assert(
std::is_same_v<decltype(length<metre, int>() / si::time<second, int>()), velocity<metre_per_second, int>>); std::is_same_v<decltype(length<metre, int>() / si::time<second, int>()), velocity<metre_per_second, int>>);
static_assert( static_assert(
std::is_same_v<decltype(length<metre>() / si::time<minute>()), velocity<scaled_unit<metre_per_second, ratio<1, 60>>>>); std::is_same_v<decltype(length<metre>() / si::time<minute>()), velocity<scaled_unit<metre_per_second, ratio<1, 60>>>>);
// TODO uncomment below when fixed in gcc static_assert(std::is_same_v<decltype(si::time<minute>() / length<metre>()),
// static_assert(std::is_same_v<decltype(si::time<minute>() / length<metre>()), quantity<unknown_dimension<units::exp<dim_length, -1>, units::exp<dim_time, 1>>, scaled_unit<unknown_unit, ratio<60>>>>);
// quantity<derived_dimension<exp<dim_length, -1>, exp<dim_time, 1>>, scaled_unit<unknown_unit, ratio<60>>>>);
static_assert(std::is_same_v<decltype(length<metre, int>() % short(1)), length<metre, int>>); static_assert(std::is_same_v<decltype(length<metre, int>() % short(1)), length<metre, int>>);
static_assert(std::is_same_v<decltype(length<metre, int>() % length<metre, short>(1)), length<metre, int>>); static_assert(std::is_same_v<decltype(length<metre, int>() % length<metre, short>(1)), length<metre, int>>);

View File

@@ -84,9 +84,9 @@ static_assert(
std::is_same_v<type_list_split_half<type_list<int, long, double, float>>::second_list, type_list<double, float>>); std::is_same_v<type_list_split_half<type_list<int, long, double, float>>::second_list, type_list<double, float>>);
// type_list_merge_sorted // type_list_merge_sorted
struct u0 : unit<u0> {}; struct u0 : named_unit<u0, "u0", no_prefix> {};
struct d0 : base_dimension<"d0", u0> {}; struct d0 : base_dimension<"d0", u0> {};
struct u1 : unit<u1> {}; struct u1 : named_unit<u1, "u1", no_prefix> {};
struct d1 : base_dimension<"d1", u1> {}; struct d1 : base_dimension<"d1", u1> {};
static_assert(std::is_same_v<type_list_merge_sorted<type_list<exp<d0, 1>>, type_list<exp<d1, 1>>, exp_less>, static_assert(std::is_same_v<type_list_merge_sorted<type_list<exp<d0, 1>>, type_list<exp<d1, 1>>, exp_less>,
@@ -99,10 +99,10 @@ static_assert(std::is_same_v<type_list_merge_sorted<type_list<exp<d1, 1>>, type_
template<TypeList List> template<TypeList List>
using exp_sort = type_list_sort<List, exp_less>; using exp_sort = type_list_sort<List, exp_less>;
static_assert(std::is_same_v<exp_sort<derived_dimension<exp<d0, 1>>>, derived_dimension<exp<d0, 1>>>); static_assert(std::is_same_v<exp_sort<exp_list<exp<d0, 1>>>, exp_list<exp<d0, 1>>>);
static_assert( static_assert(
std::is_same_v<exp_sort<derived_dimension<exp<d0, 1>, exp<d1, -1>>>, derived_dimension<exp<d0, 1>, exp<d1, -1>>>); std::is_same_v<exp_sort<exp_list<exp<d0, 1>, exp<d1, -1>>>, exp_list<exp<d0, 1>, exp<d1, -1>>>);
static_assert( static_assert(
std::is_same_v<exp_sort<derived_dimension<exp<d1, 1>, exp<d0, -1>>>, derived_dimension<exp<d0, -1>, exp<d1, 1>>>); std::is_same_v<exp_sort<exp_list<exp<d1, 1>, exp<d0, -1>>>, exp_list<exp<d0, -1>, exp<d1, 1>>>);
} // namespace } // namespace