// 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 { /** * @brief A dimension of a base quantity * * Base quantity is a quantity in a conventionally chosen subset of a given system of quantities, where no quantity * in the subset can be expressed in terms of the other quantities within that subset. They are referred to as * being mutually independent since a base quantity cannot be expressed as a product of powers of the other base * quantities. * * Symbol template parameters is an unique identifier of the base dimension. The same identifiers can be multiplied * and divided which will result with an adjustment of its factor in an Exponent of a DerivedDimension * (in case of zero the dimension will be simplified and removed from further analysis of current expresion). * * @tparam Symbol an unique identifier of the base dimension used to provide dimensional analysis support */ template struct base_dimension { static constexpr auto symbol = Symbol; ///< Unique base dimension identifier }; namespace detail { template void to_base_base_dimension(const volatile base_dimension*); } // namespace detail /** * @brief A concept matching all base dimensions in the library. * * Satisfied by all dimension types derived from an specialization of `base_dimension`. */ template concept BaseDimension = requires(T* t) { detail::to_base_base_dimension(t); }; template struct base_dimension_less : std::bool_constant<(D1::symbol < D2::symbol)> {}; // TODO Can we provide a smarter implementation? std::false_type is_derived_dimension(...); /** * @brief A concept matching all derived dimensions in the library. * * Satisfied by all dimension types derived from an specialization of `derived_dimension`. */ template concept DerivedDimension = decltype(is_derived_dimension(std::declval()))::value; /** * @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; namespace detail { template using type_list_of_base_dimension_less = expr_less; template inline constexpr bool is_dim_one = false; } // namespace detail // TODO add checking for `per` and power elements as well template concept DimensionSpec = BaseDimension || detail::is_dim_one || is_specialization_of || detail::is_specialization_of_power; // User should not instantiate this type!!! // It should not be exported from the module template struct derived_dimension : detail::expr_fractions, Ds...> { using type = derived_dimension; }; template std::true_type is_derived_dimension(const volatile derived_dimension*); /** * @brief Dimension one * * Dimension for which all the exponents of the factors corresponding to the base * dimensions are zero. Also commonly named as "dimensionless". */ inline constexpr struct dim_one : derived_dimension<> { } dim_one; namespace detail { template<> inline constexpr bool is_dim_one = true; template struct dim_type_impl { using type = T; }; template struct dim_type_impl { using type = T::type; }; template using dim_type = dim_type_impl::type; } // namespace detail template [[nodiscard]] constexpr Dimension auto operator*(D1, D2) { return detail::expr_multiply, detail::dim_type, struct dim_one, detail::type_list_of_base_dimension_less, derived_dimension>(); } template [[nodiscard]] constexpr Dimension auto operator/(D1, D2) { return detail::expr_divide, detail::dim_type, struct dim_one, detail::type_list_of_base_dimension_less, derived_dimension>(); } template [[nodiscard]] constexpr Dimension auto operator/(int value, D) { gsl_Assert(value == 1); return detail::expr_invert, struct dim_one, derived_dimension>(); } template [[nodiscard]] constexpr bool operator==(D1, D2) { return is_same_v, detail::dim_type>; } // TODO consider adding the support for text output of the dimensional equation } // namespace units