From 4eca0dbc3524af018794c91b7f8c025b05c3cba7 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 26 Oct 2024 09:30:17 +0200 Subject: [PATCH 1/8] refactor: `QuantitySpecWithNoSpecifiers` removed and `kind_of` definition simplified --- .../mp-units/framework/quantity_spec.h | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/core/include/mp-units/framework/quantity_spec.h b/src/core/include/mp-units/framework/quantity_spec.h index a3efa5687..e2fbefc01 100644 --- a/src/core/include/mp-units/framework/quantity_spec.h +++ b/src/core/include/mp-units/framework/quantity_spec.h @@ -520,38 +520,24 @@ struct is_dimensionless : std::true_type {}; */ namespace detail { -template -concept QuantitySpecWithNoSpecifiers = detail::NamedQuantitySpec || detail::DerivedQuantitySpec; - template [[nodiscard]] consteval QuantitySpec auto get_kind_tree_root(Q q); } // namespace detail -#if MP_UNITS_API_NO_CRTP template - requires detail::QuantitySpecWithNoSpecifiers && detail::SameQuantitySpec + requires(!detail::QuantityKindSpec) && detail::SameQuantitySpec +#if MP_UNITS_API_NO_CRTP struct kind_of_ final : Q::_base_type_ { - using _base_type_ = kind_of_; - static constexpr auto _quantity_spec_ = Q{}; -}; #else - -#if MP_UNITS_COMP_CLANG -template - requires detail::QuantitySpecWithNoSpecifiers && detail::SameQuantitySpec -#else -template - requires detail::SameQuantitySpec -#endif struct kind_of_ final : quantity_spec, Q{}>::_base_type_ { +#endif using _base_type_ = kind_of_; static constexpr auto _quantity_spec_ = Q{}; }; -#endif -MP_UNITS_EXPORT template - requires detail::SameQuantitySpec +MP_UNITS_EXPORT template + requires requires { typename kind_of_; } constexpr kind_of_ kind_of; namespace detail { From e72f3da6d9eb0e029959186c162b64a359f15f6a Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 26 Oct 2024 11:01:54 +0200 Subject: [PATCH 2/8] refactor: `magnitude` operators arguments renamed --- src/core/include/mp-units/framework/magnitude.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/core/include/mp-units/framework/magnitude.h b/src/core/include/mp-units/framework/magnitude.h index 5873da1ea..ff2981d81 100644 --- a/src/core/include/mp-units/framework/magnitude.h +++ b/src/core/include/mp-units/framework/magnitude.h @@ -428,22 +428,22 @@ constexpr Out magnitude_symbol_impl(Out out, const unit_symbol_formatting& fmt) template struct magnitude : detail::magnitude_base> { template - [[nodiscard]] friend consteval Magnitude auto operator*(magnitude m1, M m2) + [[nodiscard]] friend consteval Magnitude auto operator*(magnitude lhs, M rhs) { if constexpr (sizeof...(Ms) == 0) - return m2; + return rhs; else if constexpr (is_same_v>) - return m1; + return lhs; else - return _multiply_impl(m1, m2); + return _multiply_impl(lhs, rhs); } - [[nodiscard]] friend consteval auto operator/(magnitude l, Magnitude auto r) { return l * _pow<-1>(r); } + [[nodiscard]] friend consteval auto operator/(magnitude lhs, Magnitude auto rhs) { return lhs * _pow<-1>(rhs); } - template - [[nodiscard]] friend consteval bool operator==(magnitude, M2) + template + [[nodiscard]] friend consteval bool operator==(magnitude, Rhs) { - return is_same_v; + return is_same_v; } private: From 8d24b47948a3802d4f81d6b7bbcd25757cd9872d Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 26 Oct 2024 19:06:14 +0200 Subject: [PATCH 3/8] refactor: `treat_as_floating_point` simplified and extended to use `std::chrono::treat_as_floating_point_v` --- src/core/include/mp-units/bits/core_gmf.h | 1 + .../mp-units/framework/customization_points.h | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/core/include/mp-units/bits/core_gmf.h b/src/core/include/mp-units/bits/core_gmf.h index f5392c21c..5566bc923 100644 --- a/src/core/include/mp-units/bits/core_gmf.h +++ b/src/core/include/mp-units/bits/core_gmf.h @@ -55,6 +55,7 @@ #if MP_UNITS_HOSTED #include #ifndef MP_UNITS_IMPORT_STD +#include #include #include #include diff --git a/src/core/include/mp-units/framework/customization_points.h b/src/core/include/mp-units/framework/customization_points.h index 6b40e8333..43ca02014 100644 --- a/src/core/include/mp-units/framework/customization_points.h +++ b/src/core/include/mp-units/framework/customization_points.h @@ -33,6 +33,9 @@ import std; #include #include #include +#if MP_UNITS_HOSTED +#include +#endif #endif #endif @@ -50,11 +53,12 @@ MP_UNITS_EXPORT_BEGIN * @tparam Rep a representation type for which a type trait is defined */ template -constexpr bool treat_as_floating_point = std::is_floating_point_v; - -template - requires requires { typename wrapped_type_t; } -constexpr bool treat_as_floating_point = treat_as_floating_point>; +constexpr bool treat_as_floating_point = +#if MP_UNITS_HOSTED + std::chrono::treat_as_floating_point_v>; +#else + std::is_floating_point_v>; +#endif /** * @brief Specifies a type to have a scalar character From 8b46723fe28094c3eed2a604b426b5b108fecdfd Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 26 Oct 2024 19:09:30 +0200 Subject: [PATCH 4/8] refactor: `wrapped_type_t` reuses `std::indirectly_readable_traits` --- src/core/include/mp-units/ext/type_traits.h | 27 +++------------------ 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/src/core/include/mp-units/ext/type_traits.h b/src/core/include/mp-units/ext/type_traits.h index 965444f52..c93952c1a 100644 --- a/src/core/include/mp-units/ext/type_traits.h +++ b/src/core/include/mp-units/ext/type_traits.h @@ -29,6 +29,7 @@ #ifdef MP_UNITS_IMPORT_STD import std; #else +#include #include #include #endif @@ -100,30 +101,10 @@ constexpr bool is_derived_from_specialization_of = requires(T* t) { detail::to_b template typename Type> constexpr bool is_derived_from_specialization_of_v = requires(T* t) { detail::to_base_specialization_of_v(t); }; -namespace detail { - -template -struct get_value_type { - using type = T::value_type; -}; - -template -struct get_element_type { - using type = std::remove_reference_t; -}; - -} // namespace detail - -template - requires requires { typename T::value_type; } || requires { typename T::element_type; } -struct wrapped_type { - using type = - conditional, detail::get_element_type>::type; -}; - template - requires requires { typename T::value_type; } || requires { typename T::element_type; } -using wrapped_type_t = wrapped_type::type; + requires(!std::is_pointer_v && !std::is_array_v) && + requires { typename std::indirectly_readable_traits::value_type; } +using wrapped_type_t = std::indirectly_readable_traits::value_type; template struct value_type { From 2a1edbe47a56bf2e6f3fdaba5a0fcf68cb9e9d20 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 26 Oct 2024 19:11:10 +0200 Subject: [PATCH 5/8] fix: `ValuePreservingTo` fixed to apply `std::remove_cvref_t` on `FromRep` --- src/core/include/mp-units/framework/quantity.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/include/mp-units/framework/quantity.h b/src/core/include/mp-units/framework/quantity.h index 67ea73ff6..3c4dc0c9b 100644 --- a/src/core/include/mp-units/framework/quantity.h +++ b/src/core/include/mp-units/framework/quantity.h @@ -63,9 +63,11 @@ template concept IsFloatingPoint = treat_as_floating_point; template -concept ValuePreservingTo = requires(FromRep&& from, ToRep to) { - { to = std::forward(from) } -> std::same_as; -} && (IsFloatingPoint || (!IsFloatingPoint && (integral_conversion_factor(FromUnit, ToUnit)))); +concept ValuePreservingTo = + requires(FromRep&& from, ToRep to) { + { to = std::forward(from) } -> std::same_as; + } && (IsFloatingPoint || + (!IsFloatingPoint> && (integral_conversion_factor(FromUnit, ToUnit)))); template concept QuantityConvertibleTo = From 424f9665dbfe9c3df8dfd9f252b502c019959dbd Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 26 Oct 2024 19:12:10 +0200 Subject: [PATCH 6/8] feat: `std::is_object` constraint applied to `value_type_t` --- src/core/include/mp-units/ext/type_traits.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/include/mp-units/ext/type_traits.h b/src/core/include/mp-units/ext/type_traits.h index c93952c1a..01d3780d2 100644 --- a/src/core/include/mp-units/ext/type_traits.h +++ b/src/core/include/mp-units/ext/type_traits.h @@ -106,19 +106,24 @@ template requires { typename std::indirectly_readable_traits::value_type; } using wrapped_type_t = std::indirectly_readable_traits::value_type; +namespace detail { + template -struct value_type { +struct value_type_impl { using type = T; }; template requires requires { typename wrapped_type_t; } -struct value_type { +struct value_type_impl { using type = wrapped_type_t; }; +} // namespace detail + template -using value_type_t = value_type::type; + requires std::is_object_v +using value_type_t = detail::value_type_impl::type; template concept one_of = (false || ... || std::same_as); From 48fcbb8030108ddee4aadeaa1301a201975ea72e Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 26 Oct 2024 19:14:11 +0200 Subject: [PATCH 7/8] feat: `DerivedDimensionExpr` removed --- src/core/include/mp-units/framework/dimension.h | 13 ++++++++----- .../mp-units/framework/dimension_concepts.h | 17 ----------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/core/include/mp-units/framework/dimension.h b/src/core/include/mp-units/framework/dimension.h index 72b15dd65..24a1e7b84 100644 --- a/src/core/include/mp-units/framework/dimension.h +++ b/src/core/include/mp-units/framework/dimension.h @@ -52,20 +52,23 @@ import std; namespace mp_units { -template +template struct derived_dimension; MP_UNITS_EXPORT struct dimension_one; namespace detail { +template +struct is_dimension_one : std::false_type {}; + template struct base_dimension_less : std::bool_constant() < type_name()> {}; template using type_list_of_base_dimension_less = expr_less; -template +template struct derived_dimension_impl : expr_fractions {}; struct dimension_interface { @@ -165,7 +168,7 @@ struct base_dimension : detail::dimension_interface { * @note User should not instantiate this type! It is not exported from the C++ module. The library will * instantiate this type automatically based on the dimensional arithmetic equation provided by the user. */ -template +template struct derived_dimension final : detail::dimension_interface, detail::derived_dimension_impl {}; /** @@ -250,14 +253,14 @@ constexpr auto dimension_symbol_impl(Out out, const power&, cons return copy_symbol_exponent(fmt.encoding, negative_power, out); } -template Out, DerivedDimensionExpr... Ms> +template Out, typename... Ms> constexpr Out dimension_symbol_impl(Out out, const type_list&, const dimension_symbol_formatting& fmt, bool negative_power) { return (..., (out = dimension_symbol_impl(out, Ms{}, fmt, negative_power))); } -template Out, DerivedDimensionExpr... Nums, DerivedDimensionExpr... Dens> +template Out, typename... Nums, typename... Dens> constexpr Out dimension_symbol_impl(Out out, const type_list& nums, const type_list& dens, const dimension_symbol_formatting& fmt) { diff --git a/src/core/include/mp-units/framework/dimension_concepts.h b/src/core/include/mp-units/framework/dimension_concepts.h index 18bec37aa..6e3c653b6 100644 --- a/src/core/include/mp-units/framework/dimension_concepts.h +++ b/src/core/include/mp-units/framework/dimension_concepts.h @@ -57,23 +57,6 @@ namespace detail { template concept BaseDimension = Dimension && is_derived_from_specialization_of_v; -template -struct is_dimension_one : std::false_type {}; - -template -concept IsPowerOfDim = - is_specialization_of_power && (BaseDimension || is_dimension_one::value); - -template -constexpr bool is_per_of_dims = false; - -template -constexpr bool is_per_of_dims> = - (... && (BaseDimension || is_dimension_one::value || IsPowerOfDim)); - -template -concept DerivedDimensionExpr = BaseDimension || is_dimension_one::value || IsPowerOfDim || is_per_of_dims; - } // namespace detail /** From 76057eaf7c104f04b6d9959373b4ffa5042f917d Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 26 Oct 2024 19:15:34 +0200 Subject: [PATCH 8/8] feat(example): `treat_as_floating_point` specializations for examples' types removed --- example/include/ranged_representation.h | 4 ---- example/include/validated_type.h | 3 --- example/measurement.cpp | 7 ++----- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/example/include/ranged_representation.h b/example/include/ranged_representation.h index aa5eb7256..0ffd5d70d 100644 --- a/example/include/ranged_representation.h +++ b/example/include/ranged_representation.h @@ -65,10 +65,6 @@ class ranged_representation : public validated_type constexpr bool mp_units::is_scalar> = mp_units::is_scalar; -template -constexpr bool mp_units::treat_as_floating_point> = - mp_units::treat_as_floating_point; - template struct MP_UNITS_STD_FMT::formatter, Char> : formatter { template diff --git a/example/include/validated_type.h b/example/include/validated_type.h index ec42f8938..c8b5dc6a7 100644 --- a/example/include/validated_type.h +++ b/example/include/validated_type.h @@ -116,9 +116,6 @@ class validated_type { template constexpr bool mp_units::is_scalar> = mp_units::is_scalar; -template -constexpr bool mp_units::treat_as_floating_point> = mp_units::treat_as_floating_point; - template std::basic_ostream& operator<<(std::basic_ostream& os, diff --git a/example/measurement.cpp b/example/measurement.cpp index b0cf594b0..596361132 100644 --- a/example/measurement.cpp +++ b/example/measurement.cpp @@ -131,12 +131,9 @@ class measurement { } // namespace -template -constexpr bool mp_units::treat_as_floating_point> = mp_units::treat_as_floating_point; - -template +template constexpr bool mp_units::is_scalar> = true; -template +template constexpr bool mp_units::is_vector> = true; static_assert(mp_units::RepresentationOf, mp_units::quantity_character::scalar>);