1 parent
7985ea2969
commit
07ce64d6e7
9 files changed
+149
-126
No files matched your search
@@ -22,6 +22,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mp-units/bits/quantity_concepts.h>
|
||||
#include <mp-units/bits/quantity_spec_concepts.h>
|
||||
#include <mp-units/bits/reference_concepts.h>
|
||||
#include <mp-units/bits/representation_concepts.h>
|
||||
@@ -163,7 +164,7 @@ concept QuantityPointOf =
|
||||
* all quantity_point-specific information.
|
||||
*/
|
||||
template<typename T>
|
||||
concept QuantityPointLike = requires(T q) {
|
||||
concept QuantityPointLike = requires(T qp) {
|
||||
quantity_point_like_traits<T>::reference;
|
||||
quantity_point_like_traits<T>::point_origin;
|
||||
typename quantity_point_like_traits<T>::rep;
|
||||
@@ -171,10 +172,11 @@ concept QuantityPointLike = requires(T q) {
|
||||
requires PointOrigin<std::remove_const_t<decltype(quantity_point_like_traits<T>::point_origin)>>;
|
||||
requires RepresentationOf<typename quantity_point_like_traits<T>::rep,
|
||||
get_quantity_spec(quantity_point_like_traits<T>::reference).character>;
|
||||
requires Quantity<std::remove_cvref_t<decltype(quantity_point_like_traits<T>::quantity_from_origin(qp))>>;
|
||||
requires std::constructible_from<
|
||||
typename quantity_point<quantity_point_like_traits<T>::reference, quantity_point_like_traits<T>::point_origin,
|
||||
typename quantity_point_like_traits<T>::rep>::quantity_type,
|
||||
decltype(quantity_point_like_traits<T>::relative(q))>;
|
||||
quantity_point<quantity_point_like_traits<T>::reference, quantity_point_like_traits<T>::point_origin,
|
||||
typename quantity_point_like_traits<T>::rep>,
|
||||
decltype(quantity_point_like_traits<T>::quantity_from_origin(qp))>;
|
||||
};
|
||||
|
||||
} // namespace mp_units
|
||||
@@ -149,8 +149,8 @@ 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 a static member function @c relative(T) that will return
|
||||
* the quantity-like value of the quantity point.
|
||||
* a type alias @c rep, and a static member function @c quantity_from_origin(T) that will
|
||||
* return the quantity being the offset of the point from the origin.
|
||||
*
|
||||
* Usage example can be found in @c units/chrono.h header file.
|
||||
*
|
||||
|
||||
@@ -121,7 +121,7 @@ public:
|
||||
q_([&] {
|
||||
if constexpr (is_same_v<std::remove_const_t<decltype(point_origin)>,
|
||||
std::remove_const_t<decltype(QP::point_origin)>>) {
|
||||
return qp.relative();
|
||||
return qp.quantity_from_origin();
|
||||
} else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin<
|
||||
std::remove_const_t<decltype(point_origin)>>) {
|
||||
return qp.absolute();
|
||||
@@ -137,7 +137,8 @@ public:
|
||||
std::remove_const_t<decltype(point_origin)>> &&
|
||||
std::convertible_to<typename detail::quantity_point_like_type<QP>::quantity_type, quantity_type>
|
||||
constexpr explicit quantity_point(const QP& qp) :
|
||||
q_(typename detail::quantity_point_like_type<QP>::quantity_type{quantity_point_like_traits<QP>::relative(qp)})
|
||||
q_(typename detail::quantity_point_like_type<QP>::quantity_type{
|
||||
quantity_point_like_traits<QP>::quantity_from_origin(qp)})
|
||||
{
|
||||
}
|
||||
|
||||
@@ -161,24 +162,24 @@ public:
|
||||
// data access
|
||||
#ifdef __cpp_explicit_this_parameter
|
||||
template<typename Self>
|
||||
[[nodiscard]] constexpr auto&& relative(this Self&& self) noexcept
|
||||
[[nodiscard]] constexpr auto&& quantity_from_origin(this Self&& self) noexcept
|
||||
{
|
||||
return std::forward<Self>(self).q_;
|
||||
}
|
||||
#else
|
||||
[[nodiscard]] constexpr quantity_type& relative() & noexcept { return q_; }
|
||||
[[nodiscard]] constexpr const quantity_type& relative() const& noexcept { return q_; }
|
||||
[[nodiscard]] constexpr quantity_type&& relative() && noexcept { return std::move(q_); }
|
||||
[[nodiscard]] constexpr const quantity_type&& relative() const&& noexcept { return std::move(q_); }
|
||||
[[nodiscard]] constexpr quantity_type& quantity_from_origin() & noexcept { return q_; }
|
||||
[[nodiscard]] constexpr const quantity_type& quantity_from_origin() const& noexcept { return q_; }
|
||||
[[nodiscard]] constexpr quantity_type&& quantity_from_origin() && noexcept { return std::move(q_); }
|
||||
[[nodiscard]] constexpr const quantity_type&& quantity_from_origin() const&& noexcept { return std::move(q_); }
|
||||
#endif
|
||||
|
||||
[[nodiscard]] constexpr Quantity auto absolute() const noexcept
|
||||
{
|
||||
if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin<
|
||||
std::remove_const_t<decltype(point_origin)>>)
|
||||
return relative();
|
||||
return quantity_from_origin();
|
||||
else
|
||||
return point_origin.quantity_point.absolute() + relative();
|
||||
return point_origin.quantity_point.absolute() + quantity_from_origin();
|
||||
}
|
||||
|
||||
template<Unit U>
|
||||
@@ -247,9 +248,9 @@ template<auto R1, auto PO1, typename Rep1, auto R2, typename Rep2>
|
||||
requires ReferenceOf<std::remove_const_t<decltype(R2)>, get_quantity_spec(R1)>
|
||||
[[nodiscard]] constexpr QuantityPoint auto operator+(const quantity_point<R1, PO1, Rep1>& qp,
|
||||
const quantity<R2, Rep2>& q)
|
||||
requires requires { qp.relative() + q; }
|
||||
requires requires { qp.quantity_from_origin() + q; }
|
||||
{
|
||||
auto temp = qp.relative() + q;
|
||||
auto temp = qp.quantity_from_origin() + q;
|
||||
using q_type = decltype(temp);
|
||||
return quantity_point<q_type::reference, PO1, typename q_type::rep>(std::move(temp));
|
||||
}
|
||||
@@ -259,7 +260,7 @@ template<auto R1, typename Rep1, auto R2, auto PO2, typename Rep2>
|
||||
requires ReferenceOf<std::remove_const_t<decltype(R1)>, get_quantity_spec(R2)>
|
||||
[[nodiscard]] constexpr QuantityPoint auto operator+(const quantity<R1, Rep1>& q,
|
||||
const quantity_point<R2, PO2, Rep2>& qp)
|
||||
requires requires { q + qp.relative(); }
|
||||
requires requires { q + qp.quantity_from_origin(); }
|
||||
{
|
||||
return qp + q;
|
||||
}
|
||||
@@ -283,9 +284,9 @@ template<auto R1, auto PO1, typename Rep1, auto R2, typename Rep2>
|
||||
requires ReferenceOf<std::remove_const_t<decltype(R2)>, get_quantity_spec(R1)>
|
||||
[[nodiscard]] constexpr QuantityPoint auto operator-(const quantity_point<R1, PO1, Rep1>& qp,
|
||||
const quantity<R2, Rep2>& q)
|
||||
requires requires { qp.relative() - q; }
|
||||
requires requires { qp.quantity_from_origin() - q; }
|
||||
{
|
||||
const auto temp = qp.relative() - q;
|
||||
const auto temp = qp.quantity_from_origin() - q;
|
||||
using q_type = decltype(temp);
|
||||
return quantity_point<q_type::reference, PO1, typename q_type::rep>(std::move(temp));
|
||||
}
|
||||
@@ -306,7 +307,7 @@ template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
|
||||
std::remove_const_t<decltype(QP2::point_origin)>>) {
|
||||
constexpr auto common_qs = common_quantity_spec(QP1::quantity_spec, QP1::point_origin.quantity_spec,
|
||||
QP2::quantity_spec, QP2::point_origin.quantity_spec);
|
||||
return quantity_cast<common_qs>(lhs.relative() - rhs.relative());
|
||||
return quantity_cast<common_qs>(lhs.quantity_from_origin() - rhs.quantity_from_origin());
|
||||
} else
|
||||
return lhs.absolute() - rhs.absolute();
|
||||
}
|
||||
@@ -318,12 +319,12 @@ template<PointOrigin PO, QuantityPointOf<PO{}> QP>
|
||||
constexpr auto common_qs = common_quantity_spec(PO::quantity_spec, QP::quantity_spec, QP::point_origin.quantity_spec);
|
||||
if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin<PO>) {
|
||||
if constexpr (is_same_v<std::remove_const_t<PO>, std::remove_const_t<decltype(QP::point_origin)>>)
|
||||
return quantity_cast<common_qs>(qp.relative());
|
||||
return quantity_cast<common_qs>(qp.quantity_from_origin());
|
||||
else
|
||||
return quantity_cast<common_qs>(qp.absolute());
|
||||
} else {
|
||||
if constexpr (is_same_v<std::remove_const_t<PO>, std::remove_const_t<decltype(QP::point_origin)>>)
|
||||
return quantity_cast<common_qs>(qp.relative());
|
||||
return quantity_cast<common_qs>(qp.quantity_from_origin());
|
||||
else
|
||||
return quantity_cast<common_qs>(qp.absolute() - PO::quantity_point.absolute());
|
||||
}
|
||||
@@ -358,7 +359,7 @@ template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
|
||||
{
|
||||
if constexpr (is_same_v<std::remove_const_t<decltype(QP1::point_origin)>,
|
||||
std::remove_const_t<decltype(QP2::point_origin)>>)
|
||||
return lhs.relative() <=> rhs.relative();
|
||||
return lhs.quantity_from_origin() <=> rhs.quantity_from_origin();
|
||||
else
|
||||
return lhs.absolute() <=> rhs.absolute();
|
||||
}
|
||||
@@ -369,7 +370,7 @@ template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
|
||||
{
|
||||
if constexpr (is_same_v<std::remove_const_t<decltype(QP1::point_origin)>,
|
||||
std::remove_const_t<decltype(QP2::point_origin)>>)
|
||||
return lhs.relative() == rhs.relative();
|
||||
return lhs.quantity_from_origin() == rhs.quantity_from_origin();
|
||||
else
|
||||
return lhs.absolute() == rhs.absolute();
|
||||
}
|
||||
|
||||
@@ -80,9 +80,10 @@ struct quantity_point_like_traits<std::chrono::time_point<C, std::chrono::durati
|
||||
static constexpr auto reference = detail::time_unit_from_chrono_period<Period>();
|
||||
static constexpr auto point_origin = chrono_point_origin<C>;
|
||||
using rep = Rep;
|
||||
[[nodiscard]] static constexpr auto relative(const std::chrono::time_point<C, std::chrono::duration<Rep, Period>>& qp)
|
||||
[[nodiscard]] static constexpr auto quantity_from_origin(
|
||||
const std::chrono::time_point<C, std::chrono::duration<Rep, Period>>& qp)
|
||||
{
|
||||
return qp.time_since_epoch();
|
||||
return quantity{qp.time_since_epoch()};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user