More refactoring

- unknown_unit and unknown_dimension added
- the support for remaining SI units added
This commit is contained in:
Mateusz Pusz committed 2019-12-07 16:30:40 +01:00
1 parent 33f14f39f3
commit 22eda11bea
40 files changed
+1094 -953

No files matched your search

+3 -177
View File
@@ -229,6 +229,9 @@ using make_dimension = dim_consolidate<type_list_sort<typename extract<Es...>::t
* 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.
*
* Coherent unit is a unit that, for a given system of quantities and for a chosen set of base units, is a product
* 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.
@@ -246,30 +249,6 @@ struct derived_dimension<Child, U, E, ERest...> : downcast_child<Child, typename
using coherent_unit = U;
};
// unknown_unit
struct unknown_unit {};
namespace detail {
template<typename T>
concept PredefinedDimension = Dimension<T> && requires { typename T::coherent_unit; };
template<Dimension D, Ratio R>
auto unit_for_dimension_impl()
{
if constexpr(PredefinedDimension<D>) {
return downcast<scaled_unit<typename D::coherent_unit::reference, R>>{};
}
else {
return scaled_unit<unknown_unit, R>{};
}
}
template<Dimension D, Ratio R>
using unit_for_dimension = decltype(unit_for_dimension_impl<D, R>());
}
// same_dim
template<Dimension D1, Dimension D2>
inline constexpr bool same_dim = false;
@@ -280,157 +259,4 @@ inline constexpr bool same_dim<D1, D2> = std::is_same_v<D1, D2>;
template<DerivedDimension D1, DerivedDimension D2>
inline constexpr bool same_dim<D1, D2> = std::is_same_v<typename D1::downcast_base_type, typename D2::downcast_base_type>;
// dim_invert
namespace detail {
template<Dimension D>
struct dim_invert_impl;
template<BaseDimension D>
struct dim_invert_impl<D> {
using type = downcast<derived_dimension<exp<D, -1>>>;
};
template<BaseDimension D>
struct dim_invert_impl<derived_dimension<exp<D, -1>>> {
using type = D;
};
template<typename... Es>
struct dim_invert_impl<derived_dimension<Es...>> {
using type = downcast<derived_dimension<exp_invert<Es>...>>;
};
template<DerivedDimension D>
struct dim_invert_impl<D> : dim_invert_impl<downcast_base_t<D>> {
};
} // namespace detail
template<Dimension D>
using dim_invert = detail::dim_invert_impl<D>::type;
// dimension_multiply
namespace detail {
template<Dimension D>
struct dim_unpack {
using type = D;
};
template<BaseDimension D>
struct dim_unpack<derived_dimension<exp<D, 1>>> {
using type = D;
};
/**
* @brief Merges 2 sorted derived dimensions into one units::derived_dimension
*
* 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
* into one list and consolidate duplicates. Also it is possible that final exponents list will contain only
* one element being a base dimension with exponent 1. In such a case the final dimension should be the base
* 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;
template<Dimension D1, Dimension D2>
struct dimension_multiply_impl;
template<BaseDimension D1, BaseDimension D2>
struct dimension_multiply_impl<D1, D2> {
using type = downcast<merge_dimension<derived_dimension<exp<D1, 1>>, derived_dimension<exp<D2, 1>>>>;
};
template<BaseDimension D1, DerivedDimension D2>
struct dimension_multiply_impl<D1, D2> {
using type = downcast<merge_dimension<derived_dimension<exp<D1, 1>>, typename D2::downcast_base_type>>;
};
template<DerivedDimension D1, BaseDimension D2>
struct dimension_multiply_impl<D1, D2> {
using type = dimension_multiply_impl<D2, D1>::type;
};
template<DerivedDimension D1, DerivedDimension D2>
struct dimension_multiply_impl<D1, D2> {
using type = downcast<merge_dimension<typename D1::downcast_base_type, typename D2::downcast_base_type>>;
};
} // namespace detail
template<Dimension D1, Dimension D2>
using dimension_multiply = detail::dimension_multiply_impl<D1, D2>::type;
template<Dimension D1, Dimension D2>
using dimension_divide = detail::dimension_multiply_impl<D1, dim_invert<D2>>::type;
// dimension_sqrt
namespace detail {
template<Dimension D>
struct dimension_sqrt_impl;
template<BaseDimension D>
struct dimension_sqrt_impl<D> {
using type = derived_dimension<exp<D, 1, 2>>;
};
template<BaseDimension D>
struct dimension_sqrt_impl<derived_dimension<exp<D, 2>>> {
using type = D;
};
template<DerivedDimension D>
struct dimension_sqrt_impl<D> {
using type = dimension_sqrt_impl<typename D::downcast_base_type>;
};
template<typename... Es>
struct dimension_sqrt_impl<derived_dimension<Es...>> {
using type = downcast<derived_dimension<exp_multiply<Es, 1, 2>...>>;
};
} // namespace detail
template<Dimension D>
using dimension_sqrt = detail::dimension_sqrt_impl<typename D::downcast_base_type>::type;
// dimension_pow
namespace detail {
template<Dimension D, std::size_t N>
struct dimension_pow_impl;
template<BaseDimension D, std::size_t N>
struct dimension_pow_impl<D, N> {
using type = downcast<derived_dimension<exp<D, N>>>;
};
template<BaseDimension D>
struct dimension_pow_impl<D, 1> {
using type = D;
};
template<BaseDimension D, std::size_t N>
struct dimension_pow_impl<derived_dimension<exp<D, 1, N>>, N> {
using type = D;
};
template<DerivedDimension D, std::size_t N>
struct dimension_pow_impl<D, N> {
using type = dimension_pow_impl<downcast_base<D>, N>;
};
template<typename... Es, std::size_t N>
struct dimension_pow_impl<derived_dimension<Es...>, N> {
using type = downcast<derived_dimension<exp_multiply<Es, N, 1>...>>;
};
} // namespace detail
template<Dimension D, std::size_t N>
using dimension_pow = detail::dimension_pow_impl<D, N>::type;
} // namespace units