diff --git a/docs/users_guide/framework_basics/concepts.md b/docs/users_guide/framework_basics/concepts.md index aba062e1..efeb847a 100644 --- a/docs/users_guide/framework_basics/concepts.md +++ b/docs/users_guide/framework_basics/concepts.md @@ -282,9 +282,9 @@ for which an instantiation of `quantity_like_traits` type trait yields a valid t static constexpr auto reference = si::second; using rep = std::chrono::seconds::rep; - [[nodiscard]] static constexpr convert_implicitly to_numerical_value(const std::chrono::seconds& q) + [[nodiscard]] static constexpr convert_implicitly to_numerical_value(const std::chrono::seconds& d) { - return q.count(); + return d.count(); } [[nodiscard]] static constexpr convert_implicitly from_numerical_value(const rep& v) @@ -307,12 +307,11 @@ for which an instantiation of `quantity_point_like_traits` type trait yields a v - Static data member `point_origin` that matches the [`PointOrigin`](#PointOrigin) concept. - `rep` type that matches [`RepresentationOf`](#RepresentationOf) concept with the character provided in `reference`. -- `to_quantity(T)` static member function returning the `quantity` being the offset of the point - from the origin packed in either `convert_explicitly` or `convert_implicitly` wrapper that enables - implicit conversion in the latter case. -- `from_quantity(quantity)` static member function returning `T` packed in either - `convert_explicitly` or `convert_implicitly` wrapper that enables implicit conversion in the latter - case. +- `to_numerical_value(T)` static member function returning a raw value of the quantity being the offset + of the point from the origin packed in either `convert_explicitly` or `convert_implicitly` wrapper that + enables implicit conversion in the latter case. +- `from_numerical_value(rep)` static member function returning `T` packed in either `convert_explicitly` + or `convert_implicitly` wrapper that enables implicit conversion in the latter case. ??? abstract "Examples" @@ -324,17 +323,17 @@ for which an instantiation of `quantity_point_like_traits` type trait yields a v struct mp_units::quantity_point_like_traits> { using T = std::chrono::time_point; static constexpr auto reference = si::second; - static constexpr struct point_origin final : absolute_point_origin {} point_origin{}; + static constexpr struct point_origin_ final : absolute_point_origin {} point_origin{}; using rep = std::chrono::seconds::rep; - [[nodiscard]] static constexpr convert_implicitly> to_quantity(const T& qp) + [[nodiscard]] static constexpr convert_implicitly to_numerical_value(const T& tp) { - return quantity{qp.time_since_epoch()}; + return tp.time_since_epoch().count(); } - [[nodiscard]] static constexpr convert_implicitly from_quantity(const quantity& q) + [[nodiscard]] static constexpr convert_implicitly from_numerical_value(const rep& v) { - return T(q); + return T(std::chrono::seconds(v)); } }; 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 4ffddfe3..07da96af 100644 --- a/docs/users_guide/use_cases/interoperability_with_other_libraries.md +++ b/docs/users_guide/use_cases/interoperability_with_other_libraries.md @@ -174,10 +174,11 @@ type trait: - static data member `point_origin` that specifies the absolute point, which is the beginning of our measurement scale for our points, - `rep` type that specifies the underlying storage type, -- `to_quantity(T)` static member function returning the `quantity` being the offset of the point - from the origin packed in either `convert_explicitly` or `convert_implicitly` wrapper, -- `from_quantity(quantity)` static member function returning `T` packed in either - `convert_explicitly` or `convert_implicitly` wrapper. +- `to_numerical_value(T)` static member function returning a raw value of the `quantity` being + the offset of the point from the origin packed in either `convert_explicitly` or `convert_implicitly` + wrapper. +- `from_numerical_value(rep)` static member function returning `T` packed in either `convert_explicitly` + or `convert_implicitly` wrapper. For example, for our `Timestamp` type, we could provide the following: @@ -187,16 +188,8 @@ struct mp_units::quantity_point_like_traits { static constexpr auto reference = si::second; static constexpr auto point_origin = default_point_origin(reference); using rep = decltype(Timestamp::seconds); - - static constexpr convert_implicitly> to_quantity(Timestamp ts) - { - return ts.seconds * si::second; - } - - static constexpr convert_explicitly from_quantity(quantity q) - { - return Timestamp(q.numerical_value_ref_in(si::second)); - } + static constexpr convert_implicitly to_numerical_value(Timestamp ts) { return ts.seconds; } + static constexpr convert_explicitly from_numerical_value(rep v) { return Timestamp(v); } }; ``` diff --git a/src/core/include/mp-units/framework/customization_points.h b/src/core/include/mp-units/framework/customization_points.h index b1865cb7..1396d229 100644 --- a/src/core/include/mp-units/framework/customization_points.h +++ b/src/core/include/mp-units/framework/customization_points.h @@ -174,9 +174,9 @@ struct quantity_like_traits; * @brief Provides support for external quantity point-like types * * The type trait should provide nested @c reference and @c origin objects, - * a type alias @c rep, and static member functions @c to_quantity(T) that returns - * the quantity being the offset of the point from the origin and - * @c from_quantity(quantity) that returns @c T form this quantity. + * a type alias @c rep, and static member functions @c to_numerical_value(T) that + * returns the raw value of the the quantity being the offset of the point from the + * origin and @c from_numerical_value(rep) that returns @c T formed this raw value. * Both return types should be encapsulated in either @c convert_explicitly or * @c convert_implicitly to specify if the conversion is allowed to happen implicitly. * diff --git a/src/core/include/mp-units/framework/quantity_point.h b/src/core/include/mp-units/framework/quantity_point.h index 452140d3..af075311 100644 --- a/src/core/include/mp-units/framework/quantity_point.h +++ b/src/core/include/mp-units/framework/quantity_point.h @@ -201,13 +201,14 @@ public: quantity::reference, typename quantity_point_like_traits::rep>, quantity_type> constexpr explicit( - is_specialization_of::to_quantity(std::declval())), + is_specialization_of::to_numerical_value(std::declval())), convert_explicitly> || !std::convertible_to< quantity::reference, typename quantity_point_like_traits::rep>, quantity_type>) // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) quantity_point(const QP& qp) : - quantity_from_origin_is_an_implementation_detail_(quantity_point_like_traits::to_quantity(qp).value) + quantity_from_origin_is_an_implementation_detail_(quantity_point_like_traits::to_numerical_value(qp).value, + get_unit(quantity_point_like_traits::reference)) { } @@ -297,16 +298,20 @@ public: typename quantity_point_like_traits::rep>> [[nodiscard]] explicit( is_specialization_of< - decltype(quantity_point_like_traits::from_quantity(quantity_from_origin_is_an_implementation_detail_)), + decltype(quantity_point_like_traits::from_numerical_value( + quantity_from_origin_is_an_implementation_detail_.numerical_value_is_an_implementation_detail_)), convert_explicitly> || !std::convertible_to::reference, typename quantity_point_like_traits::rep>>) constexpr // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) operator QP_() const& noexcept( - noexcept(quantity_point_like_traits::from_quantity(quantity_from_origin_is_an_implementation_detail_)) && + noexcept(quantity_point_like_traits::from_numerical_value( + quantity_from_origin_is_an_implementation_detail_.numerical_value_is_an_implementation_detail_)) && std::is_nothrow_copy_constructible_v) { - return quantity_point_like_traits::from_quantity(quantity_from_origin_is_an_implementation_detail_).value; + return quantity_point_like_traits::from_numerical_value( + quantity_from_origin_is_an_implementation_detail_.numerical_value_is_an_implementation_detail_) + .value; } template> @@ -315,16 +320,19 @@ public: typename quantity_point_like_traits::rep>> [[nodiscard]] explicit( is_specialization_of< - decltype(quantity_point_like_traits::from_quantity(quantity_from_origin_is_an_implementation_detail_)), + decltype(quantity_point_like_traits::from_numerical_value( + quantity_from_origin_is_an_implementation_detail_.numerical_value_is_an_implementation_detail_)), convert_explicitly> || !std::convertible_to::reference, typename quantity_point_like_traits::rep>>) constexpr // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) operator QP_() && noexcept( - noexcept(quantity_point_like_traits::from_quantity(quantity_from_origin_is_an_implementation_detail_)) && + noexcept(quantity_point_like_traits::from_numerical_value( + quantity_from_origin_is_an_implementation_detail_.numerical_value_is_an_implementation_detail_)) && std::is_nothrow_move_constructible_v) { - return quantity_point_like_traits::from_quantity(std::move(quantity_from_origin_is_an_implementation_detail_)) + return quantity_point_like_traits::from_numerical_value( + std::move(quantity_from_origin_is_an_implementation_detail_).numerical_value_is_an_implementation_detail_) .value; } @@ -387,11 +395,10 @@ template PO> quantity_point(Q q, PO) -> quantity_point; template -explicit( - is_specialization_of::to_quantity(std::declval())), convert_explicitly>) - quantity_point(QP) - -> quantity_point::reference, quantity_point_like_traits::point_origin, - typename quantity_point_like_traits::rep>; +explicit(is_specialization_of::to_numerical_value(std::declval())), + convert_explicitly>) quantity_point(QP) + -> quantity_point::reference, quantity_point_like_traits::point_origin, + typename quantity_point_like_traits::rep>; template // TODO simplify when gcc catches up diff --git a/src/core/include/mp-units/framework/quantity_point_concepts.h b/src/core/include/mp-units/framework/quantity_point_concepts.h index f5d8af83..212ff75d 100644 --- a/src/core/include/mp-units/framework/quantity_point_concepts.h +++ b/src/core/include/mp-units/framework/quantity_point_concepts.h @@ -159,14 +159,12 @@ concept QuantityPointLike = requires { typename quantity_point_like_traits::rep; requires RepresentationOf::rep, get_quantity_spec(quantity_point_like_traits::reference).character>; -} && requires(T qp, quantity::reference, typename quantity_point_like_traits::rep> q) { +} && requires(T qp, typename quantity_point_like_traits::rep v) { { - quantity_point_like_traits::to_quantity(qp) - } -> detail::ConversionSpecOf< - quantity::reference, typename quantity_point_like_traits::rep>>; - + quantity_point_like_traits::to_numerical_value(qp) + } -> detail::ConversionSpecOf::rep>; { - quantity_point_like_traits::from_quantity(q) + quantity_point_like_traits::from_numerical_value(v) } -> detail::ConversionSpecOf; }; diff --git a/src/systems/include/mp-units/systems/si/chrono.h b/src/systems/include/mp-units/systems/si/chrono.h index b52402be..d5b6c55d 100644 --- a/src/systems/include/mp-units/systems/si/chrono.h +++ b/src/systems/include/mp-units/systems/si/chrono.h @@ -100,16 +100,16 @@ struct quantity_point_like_traits; using rep = Rep; - [[nodiscard]] static constexpr convert_implicitly> to_quantity(const T& qp) noexcept( + [[nodiscard]] static constexpr convert_implicitly to_numerical_value(const T& tp) noexcept( std::is_nothrow_copy_constructible_v) { - return quantity{qp.time_since_epoch()}; + return tp.time_since_epoch().count(); } - [[nodiscard]] static constexpr convert_implicitly from_quantity(const quantity& q) noexcept( + [[nodiscard]] static constexpr convert_implicitly from_numerical_value(const rep& v) noexcept( std::is_nothrow_copy_constructible_v) { - return T(q); + return T(std::chrono::duration(v)); } };