// 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 #include // #include // IWYU pragma: begin_exports // #include // #include // IWYU pragma: end_exports namespace units { // namespace detail { // template // inline constexpr bool can_be_prefixed = false; // } // namespace detail /** * @brief A common point for a hierarchy of units * * A unit is an entity defined and adopted by convention, with which any other quantity of * the same kind can be compared to express the ratio of the second quantity to the first * one as a number. * * All units of the same dimension can be convereted between each other. To allow this all of * them are expressed as different ratios of the same one proprietary chosen reference unit * (i.e. all length units are expressed in terms of meter, all mass units are expressed in * terms of gram, ...) * * @tparam M a Magnitude representing the (relative) size of this unit * @tparam U a unit to use as a reference for this dimension * * @note U cannot be constrained with Unit as for some specializations (i.e. named_unit) * it gets the incomplete child's type with the CRTP idiom. */ template struct scaled_unit { static constexpr UNITS_MSVC_WORKAROUND(Magnitude) auto mag = M; using reference = U; }; // TODO: Remove when P1985 accepted namespace detail { template void to_base_scaled_unit(const volatile scaled_unit*); } // namespace detail /** * @brief A concept matching all unit types in the library * * Satisfied by all unit types derived from an specialization of :class:`scaled_unit`. */ template concept Unit = requires(T* t) { detail::to_base_scaled_unit(t); }; namespace detail { template inline constexpr bool is_named = false; } template concept NamedUnit = Unit && detail::is_named; template struct same_unit_reference : is_same {}; // TODO add checking for `per` and power elements as well template concept UnitSpec = Unit || is_specialization_of || detail::is_specialization_of_power; template struct derived_unit : detail::expr_fractions, Us...>, scaled_unit(), derived_unit> {}; // : detail::normalized_dimension, Ds...> {}; /** * @brief Unit one * * Unit of a dimensionless quantity. */ inline constexpr struct one : derived_unit<> { } one; /** * @brief A named unit * * Defines a named (in most cases coherent) unit that is then passed to a dimension definition. * A named unit may be composed with a prefix to create a prefixed_unit. * * @tparam Symbol a short text representation of the unit */ template struct named_unit; template struct named_unit : scaled_unit(), named_unit> { static constexpr auto symbol = Symbol; }; template requires is_specialization_of, derived_unit> struct named_unit : decltype(U) { static constexpr auto symbol = Symbol; }; /** * @brief A named scaled unit * * Defines a new named unit that is a scaled version of another unit. * A named unit may be composed with a prefix to create a prefixed_unit. * * @tparam Symbol a short text representation of the unit * @tparam M the Magnitude by which to scale U * @tparam U a reference unit to scale */ template struct named_scaled_unit : scaled_unit { static constexpr auto symbol = Symbol; }; /** * @brief A prefixed unit * * Defines a new unit that is a scaled version of another unit by the provided prefix. It is * only possible to create such a unit if the given prefix type matches the one defined in a * reference unit. * * @tparam Child inherited class type used by the downcasting facility (CRTP Idiom) * @tparam P prefix to be appied to the reference unit * @tparam U reference unit */ // template // requires detail::can_be_prefixed // struct prefixed_unit : downcast_dispatch> { // static constexpr auto symbol = P::symbol + U::symbol; // }; template struct prefixed_unit : scaled_unit {}; /** * @brief A coherent unit of a derived quantity * * Defines a new coherent unit of a derived quantity. It should be passed as a coherent unit * in the dimension's definition for such a quantity. * * @tparam Child inherited class type used by the downcasting facility (CRTP Idiom) */ // template // struct derived_unit : downcast_dispatch(), Child>> {}; namespace detail { template void is_named_impl(const volatile named_unit*); template void is_named_impl(const volatile named_scaled_unit*); template inline constexpr bool is_named = requires(U * u) { is_named_impl(u); }; // template // void can_be_prefixed_impl(const volatile named_unit*); // template // void can_be_prefixed_impl(const volatile named_scaled_unit*); // template // void can_be_prefixed_impl(const volatile alias_unit*); // template // inline constexpr bool can_be_prefixed = requires(U * u) { can_be_prefixed_impl(u); }; // template // inline constexpr bool can_be_prefixed> = can_be_prefixed; template struct unit_less : std::bool_constant() < type_name()> {}; template using type_list_of_unit_less = expr_less; } // namespace detail template constexpr Unit auto operator*(U1, U2) { return detail::expr_multiply(); } template constexpr Unit auto operator/(U1, U2) { return detail::expr_divide(); } template constexpr Unit auto operator/(int value, U) { gsl_Assert(value == 1); return detail::expr_invert(); } template constexpr bool operator==(U1, U2) { return false; } // template // constexpr bool operator==(D1, D2) // { // return D1::symbol == D2::symbol; // } // template // requires(type_list_size == 0) && (type_list_size == 1) && // BaseDimension> // constexpr bool operator==(D1, D2) // { // return D1::symbol == type_list_front::symbol; // } // template // requires(type_list_size == 0) && (type_list_size == 1) && // BaseDimension> // constexpr bool operator==(D1, D2) // { // return type_list_front::symbol == D2::symbol; // } // template // constexpr bool operator==(D1, D2) // { // return std::is_same_v && // std::is_same_v; // } } // namespace units