From be1705bc4a94ddd9b0eba5f7ce495b74256e62c3 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 22 Dec 2023 17:29:02 +0100 Subject: [PATCH] refactor: `zeroth_point_origin()` renamed to `default_point_origin()` and `implicit_zeroth_point_origin` renamed to `zeroth_point_origin` --- .../framework_basics/the_affine_space.md | 8 +- .../interoperability_with_other_libraries.md | 2 +- src/core/include/mp-units/quantity_point.h | 28 ++-- test/unit_test/static/quantity_point_test.cpp | 146 ++++++++---------- 4 files changed, 87 insertions(+), 97 deletions(-) diff --git a/docs/users_guide/framework_basics/the_affine_space.md b/docs/users_guide/framework_basics/the_affine_space.md index 8281f8af8..fdbeb7874 100644 --- a/docs/users_guide/framework_basics/the_affine_space.md +++ b/docs/users_guide/framework_basics/the_affine_space.md @@ -83,7 +83,7 @@ origin: ```cpp template auto PO = zeroth_point_origin(R), + PointOriginFor auto PO = default_point_origin(R), RepresentationOf Rep = double> class quantity_point; ``` @@ -95,7 +95,7 @@ zeroth point using the following rules: - if the measurement unit of a quantity specifies its point origin in its definition (e.g., degree Celsius), then this point is being used, -- otherwise, an instantiation of `implicit_zeroth_point_origin` is being used which +- otherwise, an instantiation of `zeroth_point_origin` is being used which provides a zeroth point for a specific quantity type. !!! tip @@ -385,10 +385,12 @@ The above can be used as an origin for subsequent _points_: constexpr quantity_point first_climb_alt = everest_base_camp + isq::altitude(std::uint8_t{42} * m); static_assert(first_climb_alt.quantity_from(everest_base_camp) == 42 * m); static_assert(first_climb_alt.quantity_from(mean_sea_level) == 5406 * m); +static_assert(first_climb_alt.quantity_from_zero() == 5406 * m); ``` As we can see above, the `quantity_from()` member function returns a relative distance from the -provided point origin. +provided point origin while the `quantity_from_zero()` returns the distance from the absolute point +origin. ### Converting between different representations of the same _point_ diff --git a/docs/users_guide/use_cases/interoperability_with_other_libraries.md b/docs/users_guide/use_cases/interoperability_with_other_libraries.md index 682743e24..143a205ef 100644 --- a/docs/users_guide/use_cases/interoperability_with_other_libraries.md +++ b/docs/users_guide/use_cases/interoperability_with_other_libraries.md @@ -277,7 +277,7 @@ The **mp-units** library comes with built-in interoperability with those types. ``` 1. `my_origin` is not defined in terms of `chrono_point_origin`. - 2. `implicit_zeroth_point_origin` is not defined in terms of `chrono_point_origin`. + 2. `zeroth_point_origin` is not defined in terms of `chrono_point_origin`. Here is an example of how interoperability described in this chapter can be used in practice: diff --git a/src/core/include/mp-units/quantity_point.h b/src/core/include/mp-units/quantity_point.h index d633d1f4b..73d1f66c8 100644 --- a/src/core/include/mp-units/quantity_point.h +++ b/src/core/include/mp-units/quantity_point.h @@ -64,23 +64,23 @@ struct relative_point_origin { }; template -struct implicit_zeroth_point_origin_ : absolute_point_origin, QS> {}; +struct zeroth_point_origin_ : absolute_point_origin, QS> {}; template -inline constexpr implicit_zeroth_point_origin_ implicit_zeroth_point_origin; +inline constexpr zeroth_point_origin_ zeroth_point_origin; namespace detail { template -inline constexpr bool is_specialization_of_implicit_zeroth_point_origin = false; +inline constexpr bool is_specialization_of_zeroth_point_origin = false; template -inline constexpr bool is_specialization_of_implicit_zeroth_point_origin> = true; +inline constexpr bool is_specialization_of_zeroth_point_origin> = true; template -[[nodiscard]] consteval bool is_implicit_zeroth_point_origin(PO) +[[nodiscard]] consteval bool is_zeroth_point_origin(PO) { - return is_specialization_of_implicit_zeroth_point_origin; + return is_specialization_of_zeroth_point_origin; } } // namespace detail @@ -90,7 +90,7 @@ template { if constexpr (detail::AbsolutePointOrigin && detail::AbsolutePointOrigin) return is_same_v || - (detail::is_implicit_zeroth_point_origin(po1) && detail::is_implicit_zeroth_point_origin(po2) && + (detail::is_zeroth_point_origin(po1) && detail::is_zeroth_point_origin(po2) && interconvertible(po1.quantity_spec, po2.quantity_spec)); else if constexpr (detail::RelativePointOrigin && detail::RelativePointOrigin) return PO1::quantity_point == PO2::quantity_point; @@ -103,12 +103,12 @@ template } template -[[nodiscard]] consteval PointOriginFor auto zeroth_point_origin(R) +[[nodiscard]] consteval PointOriginFor auto default_point_origin(R) { if constexpr (requires { get_unit(R{}).point_origin; }) return get_unit(R{}).point_origin; else - return implicit_zeroth_point_origin; + return zeroth_point_origin; } namespace detail { @@ -133,7 +133,7 @@ template * @tparam PO a type that represents the origin point from which the quantity point is measured from * @tparam Rep a type to be used to represent values of a quantity point */ -template auto PO = zeroth_point_origin(R), +template auto PO = default_point_origin(R), RepresentationOf Rep = double> class quantity_point { public: @@ -169,7 +169,7 @@ class quantity_point { template requires QuantityOf, get_quantity_spec(R)> && std::constructible_from && - (point_origin == zeroth_point_origin(R)) && (implicitly_convertible(Q::quantity_spec, quantity_spec)) + (point_origin == default_point_origin(R)) && (implicitly_convertible(Q::quantity_spec, quantity_spec)) constexpr explicit quantity_point(Q&& q) : quantity_from_origin_is_an_implementation_detail_(std::forward(q)) { } @@ -377,7 +377,7 @@ class quantity_point { // CTAD template -quantity_point(Q q) -> quantity_point; +quantity_point(Q q) -> quantity_point; template PO> quantity_point(Q q, PO) -> quantity_point; @@ -396,7 +396,7 @@ template const quantity& q) requires requires { qp.quantity_ref_from(PO1) + q; } { - if constexpr (detail::is_implicit_zeroth_point_origin(PO1)) + if constexpr (detail::is_zeroth_point_origin(PO1)) return quantity_point{qp.quantity_ref_from(PO1) + q}; else return quantity_point{qp.quantity_ref_from(PO1) + q, PO1}; @@ -433,7 +433,7 @@ template const quantity& q) requires requires { qp.quantity_ref_from(PO1) - q; } { - if constexpr (detail::is_implicit_zeroth_point_origin(PO1)) + if constexpr (detail::is_zeroth_point_origin(PO1)) return quantity_point{qp.quantity_ref_from(PO1) - q}; else return quantity_point{qp.quantity_ref_from(PO1) - q, PO1}; diff --git a/test/unit_test/static/quantity_point_test.cpp b/test/unit_test/static/quantity_point_test.cpp index bb3da04e5..4ebfb5adb 100644 --- a/test/unit_test/static/quantity_point_test.cpp +++ b/test/unit_test/static/quantity_point_test.cpp @@ -115,30 +115,28 @@ static_assert(relative_po + 42 * m>.quantity_spec == is inline constexpr struct my_kelvin : named_unit<"my_K", mag<10> * si::kelvin> { } my_kelvin; -static_assert(zeroth_point_origin(si::kelvin) == si::absolute_zero); -static_assert(zeroth_point_origin(si::milli) == si::absolute_zero); -static_assert(zeroth_point_origin(mag<10> * si::kelvin) == si::absolute_zero); -static_assert(zeroth_point_origin(my_kelvin) == si::absolute_zero); - -static_assert(zeroth_point_origin(si::degree_Celsius) == si::ice_point); -static_assert(zeroth_point_origin(mag<10> * si::degree_Celsius) == si::ice_point); - -static_assert(zeroth_point_origin(si::metre) == implicit_zeroth_point_origin>); -static_assert(zeroth_point_origin(si::kelvin / si::second) == - implicit_zeroth_point_origin>); -static_assert(zeroth_point_origin(si::degree_Celsius / si::second) == - implicit_zeroth_point_origin>); - -static_assert(implicit_zeroth_point_origin == implicit_zeroth_point_origin); -static_assert(implicit_zeroth_point_origin == - implicit_zeroth_point_origin); -static_assert(implicit_zeroth_point_origin> == implicit_zeroth_point_origin); -static_assert(implicit_zeroth_point_origin> == - implicit_zeroth_point_origin); - -static_assert(implicit_zeroth_point_origin != implicit_zeroth_point_origin); -static_assert(implicit_zeroth_point_origin != implicit_zeroth_point_origin); -static_assert(implicit_zeroth_point_origin != implicit_zeroth_point_origin); +static_assert(default_point_origin(si::kelvin) == si::absolute_zero); +static_assert(default_point_origin(si::milli) == si::absolute_zero); +static_assert(default_point_origin(mag<10> * si::kelvin) == si::absolute_zero); +static_assert(default_point_origin(my_kelvin) == si::absolute_zero); + +static_assert(default_point_origin(si::degree_Celsius) == si::ice_point); +static_assert(default_point_origin(mag<10> * si::degree_Celsius) == si::ice_point); + +static_assert(default_point_origin(si::metre) == zeroth_point_origin>); +static_assert(default_point_origin(si::kelvin / si::second) == + zeroth_point_origin>); +static_assert(default_point_origin(si::degree_Celsius / si::second) == + zeroth_point_origin>); + +static_assert(zeroth_point_origin == zeroth_point_origin); +static_assert(zeroth_point_origin == zeroth_point_origin); +static_assert(zeroth_point_origin> == zeroth_point_origin); +static_assert(zeroth_point_origin> == zeroth_point_origin); + +static_assert(zeroth_point_origin != zeroth_point_origin); +static_assert(zeroth_point_origin != zeroth_point_origin); +static_assert(zeroth_point_origin != zeroth_point_origin); ///////////////////// // class invariants @@ -159,8 +157,8 @@ concept invalid_types = requires { requires !requires { typename QP; }; requires !requires { typename QP; }; requires !requires { typename QP; }; - requires !requires { typename QP, int>; }; - requires !requires { typename QP, int>; }; + requires !requires { typename QP, int>; }; + requires !requires { typename QP, int>; }; // quantity used as Rep requires !requires { typename QP>; }; // quantity point used as Rep @@ -186,9 +184,9 @@ concept valid_types = requires { typename QP; typename QP; typename QP; - typename QP, int>; - typename QP>, int>; - typename QP, int>; + typename QP, int>; + typename QP>, int>; + typename QP, int>; }; static_assert(valid_types); @@ -227,17 +225,15 @@ static_assert(quantity_point::reference == si::metre); static_assert(quantity_point::quantity_spec == kind_of); static_assert(quantity_point::dimension == isq::dim_length); static_assert(quantity_point::unit == si::metre); -static_assert(is_of_type::point_origin, implicit_zeroth_point_origin_>>); -static_assert( - is_of_type::absolute_point_origin, implicit_zeroth_point_origin_>>); +static_assert(is_of_type::point_origin, zeroth_point_origin_>>); +static_assert(is_of_type::absolute_point_origin, zeroth_point_origin_>>); static_assert(quantity_point::reference == isq::height[m]); static_assert(quantity_point::quantity_spec == isq::height); static_assert(quantity_point::dimension == isq::dim_length); static_assert(quantity_point::unit == si::metre); -static_assert(is_of_type::point_origin, implicit_zeroth_point_origin_>); -static_assert( - is_of_type::absolute_point_origin, implicit_zeroth_point_origin_>); +static_assert(is_of_type::point_origin, zeroth_point_origin_>); +static_assert(is_of_type::absolute_point_origin, zeroth_point_origin_>); static_assert(quantity_point::reference == si::metre); static_assert(quantity_point::quantity_spec == kind_of); @@ -868,17 +864,17 @@ static_assert(invalid_unit_conversion); static_assert(std::is_same_v); static_assert(std::is_same_v, - implicit_zeroth_point_origin_>>); + zeroth_point_origin_>>); static_assert(std::is_same_v, - implicit_zeroth_point_origin_>>); + zeroth_point_origin_>>); static_assert(quantity_point{123 * m}.unit == si::metre); static_assert(quantity_point{123 * m}.quantity_spec == kind_of); static_assert(std::is_same_v); static_assert(std::is_same_v, - implicit_zeroth_point_origin_>); + zeroth_point_origin_>); static_assert(std::is_same_v, - implicit_zeroth_point_origin_>); + zeroth_point_origin_>); static_assert(quantity_point{isq::height(123 * m)}.unit == si::metre); static_assert(quantity_point{isq::height(123 * m)}.quantity_spec == isq::height); @@ -1031,37 +1027,30 @@ concept invalid_binary_operations = requires { requires !requires { isq::length(1 * m) + QP(1 * m); }; requires !requires { QP(1 * m) + isq::length(1 * m); }; requires !requires { isq::length(1 * m) + QP(1 * m); }; - requires !requires { QP, int>(1 * m) + isq::length(1 * m); }; - requires !requires { isq::length(1 * m) + QP, int>(1 * m); }; - requires !requires { - QP, int>(1 * m) + isq::length(1 * m); - }; - requires !requires { - isq::length(1 * m) + QP, int>(1 * m); - }; + requires !requires { QP, int>(1 * m) + isq::length(1 * m); }; + requires !requires { isq::length(1 * m) + QP, int>(1 * m); }; + requires !requires { QP, int>(1 * m) + isq::length(1 * m); }; + requires !requires { isq::length(1 * m) + QP, int>(1 * m); }; requires !requires { Origin + isq::length(1 * m); }; requires !requires { isq::length(1 * m) + Origin; }; // can't subtract more generic quantity (violates point_origin quantity_spec) requires !requires { QP(1 * m) - isq::length(1 * m); }; requires !requires { QP(1 * m) - isq::length(1 * m); }; - requires !requires { QP, int>(1 * m) - isq::length(1 * m); }; - requires !requires { - QP, int>(1 * m) - isq::length(1 * m); - }; + requires !requires { QP, int>(1 * m) - isq::length(1 * m); }; + requires !requires { QP, int>(1 * m) - isq::length(1 * m); }; requires !requires { - QP, int>(10 * isq::height[m] / - (2 * isq::time[s])) + + QP, int>(10 * isq::height[m] / + (2 * isq::time[s])) + 5 * isq::speed[m / s]; }; requires !requires { - 5 * isq::speed[m / s] + - QP, int>( - 10 * isq::height[m] / (2 * isq::time[s])); + 5 * isq::speed[m / s] + QP, int>( + 10 * isq::height[m] / (2 * isq::time[s])); }; requires !requires { - QP, int>(10 * isq::height[m] / - (2 * isq::time[s])) - + QP, int>(10 * isq::height[m] / + (2 * isq::time[s])) - 5 * isq::speed[m / s]; }; requires !requires { Origin - isq::length(1 * m); }; @@ -1088,23 +1077,23 @@ concept invalid_binary_operations = requires { requires !requires { QP(1 * m) - quantity_point{1 * m}; }; requires !requires { quantity_point{1 * m} - QP(1 * m); }; requires !requires { - QP, int>(1 * m) - quantity_point{isq::height(1 * m)}; + QP, int>(1 * m) - quantity_point{isq::height(1 * m)}; }; requires !requires { - quantity_point{isq::height(1 * m)} - QP, int>(1 * m); + quantity_point{isq::height(1 * m)} - QP, int>(1 * m); }; requires !requires { - QP, int>(1 * m) - quantity_point{isq::length(1 * m)}; + QP, int>(1 * m) - quantity_point{isq::length(1 * m)}; }; requires !requires { - quantity_point{isq::length(1 * m)} - QP, int>(1 * m); + quantity_point{isq::length(1 * m)} - QP, int>(1 * m); }; requires !requires { quantity_point{10 * isq::height[m] / (2 * isq::time[s])} - - QP, int>(5 * isq::speed[m / s]); + QP, int>(5 * isq::speed[m / s]); }; requires !requires { - QP, int>(5 * isq::speed[m / s]) - + QP, int>(5 * isq::speed[m / s]) - quantity_point{10 * isq::height[m] / (2 * isq::time[s])}; }; @@ -1554,17 +1543,17 @@ static_assert((quantity_point{5 * isq::speed[m / s]} - 10 * isq::height[m] / (2 0 * isq::speed[m / s]); static_assert(is_of_type, int>>); + quantity_point, int>>); static_assert(is_of_type<10 * isq::height[m] / (2 * isq::time[s]) + quantity_point{5 * isq::speed[m / s]}, - quantity_point, int>>); + quantity_point, int>>); static_assert(is_of_type, int>>); + quantity_point, int>>); static_assert(is_of_type<5 * isq::speed[m / s] + quantity_point{10 * isq::length[m] / (2 * isq::time[s])}, - quantity_point, int>>); + quantity_point, int>>); static_assert(is_of_type, int>>); + quantity_point, int>>); static_assert(is_of_type, int>>); + quantity_point, int>>); static_assert( is_of_type>); @@ -1573,9 +1562,8 @@ static_assert( quantity>); static_assert( - is_of_type< - quantity_point{10 * isq::height[m] / (2 * isq::time[s])} + (10 * isq::height[m] / (2 * isq::time[s])), - quantity_point<(isq::height / isq::time)[m / s], implicit_zeroth_point_origin, int>>); + is_of_type, int>>); inline constexpr struct zero_Hz : absolute_point_origin> { } zero_Hz; @@ -1632,17 +1620,17 @@ static_assert(quantity_point{5 * isq::frequency[Hz]} - quantity_point{10 / (2 * 0 * isq::frequency[Hz]); static_assert(is_of_type, int>>); + quantity_point, int>>); static_assert(is_of_type<10 / (2 * isq::period_duration[s]) + quantity_point{5 * isq::frequency[Hz]}, - quantity_point, int>>); + quantity_point, int>>); static_assert(is_of_type, int>>); + quantity_point, int>>); static_assert(is_of_type<5 * isq::frequency[Hz] + quantity_point{10 / (2 * isq::period_duration[s])}, - quantity_point, int>>); + quantity_point, int>>); static_assert(is_of_type, int>>); + quantity_point, int>>); static_assert(is_of_type, int>>); + quantity_point, int>>); static_assert(is_of_type>); static_assert(is_of_type