// The MIT License (MIT) // // Copyright (c) 2018 Mateusz Pusz // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #pragma once #include #include #include #include #include namespace units { template struct base_dimension { static constexpr auto name = Name; static constexpr auto symbol = Symbol; }; template concept BaseDimension = std::is_empty_v && requires { T::name; T::symbol; };// && // TODO file a bug for this gcc issue // std::derived_from>; // base_dimension_less template struct base_dimension_less : std::bool_constant { }; // is_exp namespace detail { template inline constexpr bool is_exp = false; // partial specialization for an exp type provided below } // namespace detail template concept Exponent = detail::is_exp; template struct dimension; // is_dimension namespace detail { template inline constexpr bool is_dimension = false; template inline constexpr bool is_dimension> = true; } // namespace detail template concept Dimension = std::is_empty_v && detail::is_dimension>; // exp template requires BaseDimension || Dimension struct exp { using dimension = BaseOrDim; static constexpr int num = Num; static constexpr int den = Den; }; // is_exp namespace detail { template inline constexpr bool is_exp> = true; } // namespace detail // exp_less template struct exp_less : base_dimension_less { }; // exp_invert namespace detail { template struct exp_invert_impl; template struct exp_invert_impl> { using type = exp; }; } template using exp_invert = detail::exp_invert_impl::type; // exp_multiply namespace detail { template struct exp_multiply_impl { using r1 = ratio; using r2 = ratio; using r = ratio_multiply; using type = exp; }; } template using exp_multiply = detail::exp_multiply_impl::type; // dimension template struct dimension : downcast_base> {}; // same_dim template inline constexpr bool same_dim = std::is_same_v; // dim_invert namespace detail { template struct dim_invert_impl; template struct dim_invert_impl> : std::type_identity...>>> {}; } template using dim_invert = detail::dim_invert_impl>::type; // make_dimension namespace detail { template struct dim_consolidate; template<> struct dim_consolidate> { using type = dimension<>; }; template struct dim_consolidate> { using type = dimension; }; template struct dim_consolidate> { using type = type_list_push_front>::type, E1>; }; template struct dim_consolidate, exp, ERest...>> { // TODO: provide custom implementation for ratio_add using r1 = std::ratio; using r2 = std::ratio; using r = std::ratio_add; using type = conditional>::type, typename dim_consolidate, ERest...>>::type>; }; template struct extract; template<> struct extract<> { using type = dimension<>; }; template struct extract, ERest...> { using type = type_list_push_front::type, exp>; }; template struct extract, Num, Den>, ERest...> { using type = type_list_push_front::type, exp_multiply...>; }; template struct extract, ERest...> { using type = extract, Num, Den>, ERest...>::type; }; template struct make_dimension { using type = detail::dim_consolidate::type, exp_less>>::type; }; } // namespace detail // derived_dimension template struct derived_dimension : downcast_helper::type> {}; // merge_dimension namespace detail { template struct merge_dimension_impl { using type = detail::dim_consolidate>::type; }; } template using merge_dimension = detail::merge_dimension_impl::type; // dimension_multiply namespace detail { template struct dimension_multiply_impl; template struct dimension_multiply_impl, dimension> : std::type_identity, dimension>>> {}; } template using dimension_multiply = detail::dimension_multiply_impl::type; // dimension_divide namespace detail { template struct dimension_divide_impl; template struct dimension_divide_impl, dimension> : dimension_multiply_impl, dimension...>> { }; } template using dimension_divide = detail::dimension_divide_impl::type; // dimension_sqrt namespace detail { template struct dimension_sqrt_impl; template struct dimension_sqrt_impl> : std::type_identity...>>> {}; } template using dimension_sqrt = detail::dimension_sqrt_impl::type; // dimension_pow namespace detail { template struct dimension_pow_impl; template struct dimension_pow_impl, N> : std::type_identity...>>> {}; } template using dimension_pow = detail::dimension_pow_impl::type; } // namespace units