// 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 namespace units { // PrefixFamily struct prefix_family; /** * @brief A concept matching a prefix family * * Satisfied by all types derived from `prefix_family` */ template concept PrefixFamily = std::derived_from; // Prefix // TODO gcc:92150 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92150 // namespace detail { // template // inline constexpr bool is_prefix = false; // template // inline constexpr bool is_prefix> = true; // } // namespace detail /** * @brief A concept matching a symbol prefix * * Satisfied by all instantiations of `prefix`. */ template // concept Prefix = detail::is_prefix; concept Prefix = true; namespace detail { template inline constexpr bool is_ratio = false; } // namespace detail /** * @brief A concept matching a ratio * * Satisfied by all instantiations of `ratio`. */ template concept Ratio = detail::is_ratio; /** * @brief A concept matching unit's ratio * * Satisfied by all types that satisfy `Ratio` and for which `R::num > 0` and `R::den > 0` */ template concept UnitRatio = Ratio && R::num > 0 && R::den > 0; // double negatives not allowed // Unit template struct scaled_unit; /** * @brief A concept matching all unit types in the library * * Satisfied by all unit types derived from the instantiation of :class:`scaled_unit`. */ template concept Unit = is_derived_from_instantiation; template requires U::is_named struct base_dimension; namespace detail { #if __GNUC__ == 9 && __GNUC_MINOR__ < 2 template inline constexpr bool is_base_dimension = true; #else template inline constexpr bool is_base_dimension = false; template inline constexpr bool is_base_dimension> = true; #endif } // namespace detail /** * @brief A concept matching all base dimensions in the library. * * Satisfied by all dimension types derived from the instantiation of `base_dimension`. */ template concept BaseDimension = detail::is_base_dimension; // Exponent namespace detail { template inline constexpr bool is_exp = false; } // namespace detail /** * @brief A concept matching dimension's exponents. * * Satisfied by all instantiations of :class:`exp`. */ template concept Exponent = detail::is_exp; // DerivedDimension namespace detail { template requires (BaseDimension && ... && BaseDimension) struct derived_dimension_base; } // namespace detail /** * @brief A concept matching all derived dimensions in the library. * * Satisfied by all dimension types derived from the instantiation of `detail::derived_dimension_base`. */ template concept DerivedDimension = is_instantiation, detail::derived_dimension_base>; // Dimension /** * @brief A concept matching all dimensions in the library. * * Satisfied by all dimension types for which either `BaseDimension` or `DerivedDimension` is `true`. */ template concept Dimension = BaseDimension || DerivedDimension; // UnitOf namespace detail { template struct dimension_unit_impl; template struct dimension_unit_impl { using type = D::base_unit; }; template struct dimension_unit_impl { using type = D::coherent_unit; }; } // namespace detail /** * @brief Returns a 'default' unit of the dimension * * Depending on the dimension type it returns a base unit (for base dimensions) * or a coherent unit (in case of derived dimensions). * * @tparam D Dimension type to get the unit from. */ template using dimension_unit = detail::dimension_unit_impl::type; /** * @brief A concept matching only units of a specified dimension. * * Satisfied by all unit types that satisfy `Unit`, `Dimension`, and for which * `U::reference` and @c dimension_unit::reference denote the same unit type. * * @tparam U Type to verify. * @tparam D Dimension type to use for verification. */ template concept UnitOf = Unit && Dimension && std::same_as::reference>; // Quantity namespace detail { template inline constexpr bool is_quantity = false; template inline constexpr bool is_quantity_point = false; } // namespace detail /** * @brief A concept matching all quantities in the library. * * Satisfied by all instantiations of :class:`quantity`. */ template concept Quantity = detail::is_quantity; /** * @brief A concept matching all quantity points in the library. * * Satisfied by all instantiations of :class:`quantity_point`. */ template concept QuantityPoint = detail::is_quantity_point; // WrappedQuantity namespace detail { template inline constexpr bool is_wrapped_quantity = false; template requires requires { typename T::value_type; } inline constexpr bool is_wrapped_quantity = Quantity || is_wrapped_quantity; } // namespace detail /** * @brief A concept matching types that wrap quantity objects. * * Satisfied by all wrapper types that satisfy `Quantity` * recursively (i.e. `std::optional>`). */ template concept WrappedQuantity = detail::is_wrapped_quantity; // Scalar namespace detail { template concept constructible_from_integral = // construction from an integral type std::constructible_from && // unit scaling std::regular_invocable, T, T> && std::regular_invocable, T, T>; template concept not_constructible_from_integral = // not construction from an integral type (!std::constructible_from) && // scaling by the value from ratio std::regular_invocable, T, std::int64_t> && std::regular_invocable, std::int64_t, T>; // && // std::regular_invocable, T, std::int64_t>; // TODO Uncomment when a bug in LA is fixed } // namespace detail /** * @brief A concept matching non-Quantity types. * * Satisfied by types that satisfy `(!Quantity) && (!WrappedQuantity) && std::regular`. */ template concept Scalar = (!Quantity) && (!WrappedQuantity) && std::regular && (detail::constructible_from_integral || detail::not_constructible_from_integral); } // namespace units