// 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 namespace units { template> requires (R::num * R::den > 0) struct unit : downcast_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 Unit = std::is_empty_v && detail::is_unit>; // make_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 = E::dimension; }; template struct get_ratio { using ratio = ::units::ratio<1>; }; template struct get_ratio { using unit_base_dim = get_unit_base_dim::dimension; using ratio = conditional, typename U::ratio, typename get_ratio::ratio>; }; template struct ratio_op; template struct ratio_op { using ratio = Result; }; template struct ratio_op { using calc_ratio = conditional<(UnitExpNum * UnitExpDen > 0), ratio_multiply, ratio_divide>; static constexpr int value = (UnitExpNum * UnitExpDen > 0) ? (UnitExpNum - UnitExpDen) : (UnitExpNum + UnitExpDen); using ratio = ratio_op::ratio; }; template struct derived_ratio; template struct derived_ratio, Us...> { using ratio = ::units::ratio<1>; }; template struct derived_ratio, Us...> { using rest_ratio = derived_ratio, Us...>::ratio; using e_ratio = get_ratio::ratio; using ratio = ratio_op::ratio; }; template using make_derived_unit = unit::ratio>; } // derived_unit template struct derived_unit; template struct derived_unit : downcast_helper>> {}; template struct derived_unit : downcast_helper> {}; template struct derived_unit : downcast_helper {}; template struct derived_unit : downcast_helper> {}; // SI 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