// 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 namespace units { template> requires (R::num > 0) struct unit : upcast_base> { using dimension = D; using ratio = R; }; // is_unit namespace detail { template inline constexpr bool is_unit = false; template inline constexpr bool is_unit> = true; } template concept bool Unit = std::is_empty_v && detail::is_unit>; // derived_unit namespace detail { template struct get_unit_base_dim; template struct get_unit_base_dim> { static_assert(sizeof...(Rest) == 0, "Base unit expected"); using dimension = typename E::dimension; }; template struct get_ratio { using ratio = std::ratio<1>; }; template struct get_ratio { using unit_base_dim = typename get_unit_base_dim::dimension; using ratio = std::conditional_t::ratio>; }; template struct ratio_op; template struct ratio_op { using ratio = Result; }; template struct ratio_op { using calc_ratio = std::conditional_t<(UnitExpValue > 0), std::ratio_multiply, std::ratio_divide>; static constexpr int value = UnitExpValue > 0 ? UnitExpValue - 1 : UnitExpValue + 1; using ratio = typename ratio_op::ratio; }; template struct derived_ratio; template struct derived_ratio, Us...> { using ratio = std::ratio<1>; }; template struct derived_ratio, Us...> { using rest_ratio = typename derived_ratio, Us...>::ratio; using e_ratio = typename get_ratio::ratio; using ratio = typename ratio_op::ratio; }; } template using derived_unit = unit::ratio>; // prefixes template using atto = unit>; template using femto = unit>; template using pico = unit>; template using nano = unit>; template using micro = unit>; template using milli = unit>; template using centi = unit>; template using deca = unit>; template using hecto = unit>; template using kilo = unit>; template using mega = unit>; template using giga = unit>; template using tera = unit>; template using peta = unit>; template using exa = unit>; } // namespace units