mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-05 13:14:29 +02:00
Merge branch 'master' of github.com:mpusz/units
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
- **0.7.0 WIP**
|
- **0.7.0 WIP**
|
||||||
- (!) refactor: `ScalableNumber` renamed to `QuantityValue`
|
- (!) refactor: `ScalableNumber` renamed to `QuantityValue`
|
||||||
- (!) refactor: output stream operators moved to the `units/quantity_io.h` header file
|
- (!) refactor: output stream operators moved to the `units/quantity_io.h` header file
|
||||||
- refactor: `quantity_point` (partially) updated to reflect latest changes to `quantity`
|
- refactor: quantity (kind) point updated to reflect latest changes to `quantity`
|
||||||
- refactor: basic concepts, `quantity` and `quantity_cast` refactored
|
- refactor: basic concepts, `quantity` and `quantity_cast` refactored
|
||||||
- refactor: `abs()` definition refactored to be more explicit about the return type
|
- refactor: `abs()` definition refactored to be more explicit about the return type
|
||||||
- feat: quantity (point) kind support added (thanks [@johelegp](https://github.com/johelegp))
|
- feat: quantity (point) kind support added (thanks [@johelegp](https://github.com/johelegp))
|
||||||
|
@@ -8,3 +8,6 @@ Customization Points
|
|||||||
|
|
||||||
.. doxygenstruct:: units::quantity_like_traits
|
.. doxygenstruct:: units::quantity_like_traits
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
.. doxygenstruct:: units::quantity_point_like_traits
|
||||||
|
:members:
|
||||||
|
@@ -53,3 +53,14 @@ such an explicit conversion::
|
|||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
quantity q = 1s; // ERROR
|
quantity q = 1s; // ERROR
|
||||||
|
|
||||||
|
For external quantity point-like types, `quantity_point_like_traits` is also provided.
|
||||||
|
It works just like `quantity_like_traits`, except that
|
||||||
|
``count(T)`` is replaced with ``relative(T)`` that returns the `QuantityLike` value.
|
||||||
|
|
||||||
|
Similar to `quantity` and `quantity_kind`, `quantity_point` and `quantity_kind_point`
|
||||||
|
provide a deduction guide from `QuantityPointLike`::
|
||||||
|
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
|
static_assert(quantity_point{std::chrono::sys_seconds{1s}} + 1_q_s == quantity_point{2s});
|
||||||
|
@@ -264,6 +264,9 @@ inline constexpr bool is_quantity_point_kind = false;
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
inline constexpr bool is_quantity_like = false;
|
inline constexpr bool is_quantity_like = false;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr bool is_quantity_point_like = false;
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -298,7 +301,7 @@ concept QuantityKind = detail::is_quantity_kind<T>;
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
concept QuantityPointKind = detail::is_quantity_point_kind<T>;
|
concept QuantityPointKind = detail::is_quantity_point_kind<T>;
|
||||||
|
|
||||||
// QuantityLike
|
// QuantityLike, QuantityPointLike
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A concept matching all quantity-like types (other than specialization of @c quantity)
|
* @brief A concept matching all quantity-like types (other than specialization of @c quantity)
|
||||||
@@ -309,6 +312,15 @@ concept QuantityPointKind = detail::is_quantity_point_kind<T>;
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
concept QuantityLike = detail::is_quantity_like<T>;
|
concept QuantityLike = detail::is_quantity_like<T>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A concept matching all quantity point-like types (other than specialization of @c quantity_point)
|
||||||
|
*
|
||||||
|
* Satisfied by all types for which a correct specialization of `quantity_point_like_traits`
|
||||||
|
* type trait is provided.
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
concept QuantityPointLike = detail::is_quantity_point_like<T>;
|
||||||
|
|
||||||
// QuantityValue
|
// QuantityValue
|
||||||
|
|
||||||
template<typename T, typename U>
|
template<typename T, typename U>
|
||||||
@@ -390,6 +402,18 @@ template<typename T>
|
|||||||
}
|
}
|
||||||
inline constexpr bool is_quantity_like<T> = true;
|
inline constexpr bool is_quantity_like<T> = true;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
requires requires(T q) {
|
||||||
|
typename quantity_point_like_traits<T>::dimension;
|
||||||
|
typename quantity_point_like_traits<T>::unit;
|
||||||
|
typename quantity_point_like_traits<T>::rep;
|
||||||
|
requires Dimension<typename quantity_point_like_traits<T>::dimension>;
|
||||||
|
requires Unit<typename quantity_point_like_traits<T>::unit>;
|
||||||
|
requires QuantityValue<typename quantity_point_like_traits<T>::rep>;
|
||||||
|
{ quantity_point_like_traits<T>::relative(q) } -> QuantityLike;
|
||||||
|
}
|
||||||
|
inline constexpr bool is_quantity_point_like<T> = true;
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
} // namespace units
|
} // namespace units
|
||||||
|
@@ -36,4 +36,15 @@ struct quantity_like_traits<std::chrono::duration<Rep, Period>> {
|
|||||||
[[nodiscard]] static constexpr rep count(const std::chrono::duration<Rep, Period>& q) { return q.count(); }
|
[[nodiscard]] static constexpr rep count(const std::chrono::duration<Rep, Period>& q) { return q.count(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename C, typename Rep, typename Period>
|
||||||
|
struct quantity_point_like_traits<std::chrono::time_point<C, std::chrono::duration<Rep, Period>>> {
|
||||||
|
using dimension = physical::si::dim_time;
|
||||||
|
using unit = downcast_unit<dimension, ratio(Period::num, Period::den)>;
|
||||||
|
using rep = Rep;
|
||||||
|
[[nodiscard]] static constexpr auto relative(
|
||||||
|
const std::chrono::time_point<C, std::chrono::duration<Rep, Period>>& qp) {
|
||||||
|
return qp.time_since_epoch();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace units
|
} // namespace units
|
||||||
|
@@ -85,4 +85,17 @@ struct quantity_values {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct quantity_like_traits;
|
struct quantity_like_traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Provides support for external quantity point-like types
|
||||||
|
*
|
||||||
|
* The type trait should provide the following nested type aliases: @c dimension, @c unit, @c rep,
|
||||||
|
* and a static member function @c relative(T) that will return the quantity-like value of the quantity point.
|
||||||
|
*
|
||||||
|
* Usage example can be found in @c units/chrono.h header file.
|
||||||
|
*
|
||||||
|
* @tparam T the type to provide support for
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
struct quantity_point_like_traits;
|
||||||
|
|
||||||
} // namespace units
|
} // namespace units
|
||||||
|
@@ -28,6 +28,10 @@
|
|||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
|
|
||||||
|
template<QuantityPointLike QP>
|
||||||
|
using quantity_point_like_type = quantity_point<typename quantity_point_like_traits<QP>::dimension,
|
||||||
|
typename quantity_point_like_traits<QP>::unit, typename quantity_point_like_traits<QP>::rep>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A quantity point
|
* @brief A quantity point
|
||||||
*
|
*
|
||||||
@@ -63,6 +67,11 @@ public:
|
|||||||
requires std::is_constructible_v<quantity_type, Q>
|
requires std::is_constructible_v<quantity_type, Q>
|
||||||
constexpr explicit quantity_point(const Q& q) : q_{q} {}
|
constexpr explicit quantity_point(const Q& q) : q_{q} {}
|
||||||
|
|
||||||
|
template<QuantityPointLike QP>
|
||||||
|
constexpr explicit quantity_point(const QP& qp)
|
||||||
|
requires std::is_constructible_v<quantity_type, decltype(quantity_point_like_traits<QP>::relative(qp))>
|
||||||
|
: q_{quantity_point_like_traits<QP>::relative(qp)} {}
|
||||||
|
|
||||||
template<QuantityPoint QP2>
|
template<QuantityPoint QP2>
|
||||||
requires std::is_convertible_v<typename QP2::quantity_type, quantity_type>
|
requires std::is_convertible_v<typename QP2::quantity_type, quantity_type>
|
||||||
constexpr quantity_point(const QP2& qp) : q_{qp.relative()} {}
|
constexpr quantity_point(const QP2& qp) : q_{qp.relative()} {}
|
||||||
@@ -182,6 +191,9 @@ template<QuantityLike Q>
|
|||||||
quantity_point(Q) -> quantity_point<typename quantity_like_traits<Q>::dimension,
|
quantity_point(Q) -> quantity_point<typename quantity_like_traits<Q>::dimension,
|
||||||
typename quantity_like_traits<Q>::unit, typename quantity_like_traits<Q>::rep>;
|
typename quantity_like_traits<Q>::unit, typename quantity_like_traits<Q>::rep>;
|
||||||
|
|
||||||
|
template<QuantityPointLike QP>
|
||||||
|
explicit quantity_point(QP) -> quantity_point_like_type<QP>;
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template<typename D, typename U, typename Rep>
|
template<typename D, typename U, typename Rep>
|
||||||
|
@@ -67,6 +67,10 @@ public:
|
|||||||
requires std::is_constructible_v<quantity_type, Q>
|
requires std::is_constructible_v<quantity_type, Q>
|
||||||
constexpr explicit quantity_point_kind(const Q& q) : qk_{q} {}
|
constexpr explicit quantity_point_kind(const Q& q) : qk_{q} {}
|
||||||
|
|
||||||
|
template<QuantityPointLike QP>
|
||||||
|
requires std::is_constructible_v<quantity_point<dimension, U, Rep>, QP>
|
||||||
|
constexpr explicit quantity_point_kind(const QP& qp) : qk_{quantity_point_like_traits<QP>::relative(qp)} {}
|
||||||
|
|
||||||
constexpr explicit quantity_point_kind(const quantity_point<dimension, U, Rep>& qp) : qk_{qp.relative()} {}
|
constexpr explicit quantity_point_kind(const quantity_point<dimension, U, Rep>& qp) : qk_{qp.relative()} {}
|
||||||
|
|
||||||
constexpr explicit quantity_point_kind(const quantity_kind_type& qk) : qk_{qk} {}
|
constexpr explicit quantity_point_kind(const quantity_kind_type& qk) : qk_{qk} {}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
#include "test_tools.h"
|
#include "test_tools.h"
|
||||||
#include <units/chrono.h>
|
#include <units/chrono.h>
|
||||||
#include <units/physical/si/derived/speed.h>
|
#include <units/physical/si/derived/speed.h>
|
||||||
|
#include <units/quantity_point.h>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@@ -30,8 +31,13 @@ using namespace units;
|
|||||||
using namespace units::physical;
|
using namespace units::physical;
|
||||||
using namespace units::physical::si::literals;
|
using namespace units::physical::si::literals;
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
using sys_seconds = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
|
||||||
|
using sys_days = std::chrono::time_point<std::chrono::system_clock,
|
||||||
|
std::chrono::duration<long, std::ratio_multiply<std::ratio<24>, std::chrono::hours::period>>>;
|
||||||
|
template<typename U, typename Rep = double> using time_point = quantity_point<si::dim_time, U, Rep>;
|
||||||
|
|
||||||
static_assert(QuantityLike<std::chrono::seconds>);
|
static_assert(QuantityLike<std::chrono::seconds>);
|
||||||
|
static_assert(QuantityPointLike<sys_seconds>);
|
||||||
|
|
||||||
// construction - same rep type
|
// construction - same rep type
|
||||||
static_assert(std::constructible_from<si::time<si::second, std::chrono::seconds::rep>, std::chrono::seconds>);
|
static_assert(std::constructible_from<si::time<si::second, std::chrono::seconds::rep>, std::chrono::seconds>);
|
||||||
@@ -42,6 +48,14 @@ static_assert(std::constructible_from<si::time<si::second, std::chrono::hours::r
|
|||||||
static_assert(!std::convertible_to<std::chrono::hours, si::time<si::second, std::chrono::hours::rep>>);
|
static_assert(!std::convertible_to<std::chrono::hours, si::time<si::second, std::chrono::hours::rep>>);
|
||||||
static_assert(!std::constructible_from<si::time<si::hour, std::chrono::seconds::rep>, std::chrono::seconds>);
|
static_assert(!std::constructible_from<si::time<si::hour, std::chrono::seconds::rep>, std::chrono::seconds>);
|
||||||
static_assert(!std::convertible_to<std::chrono::seconds, si::time<si::hour, std::chrono::seconds::rep>>);
|
static_assert(!std::convertible_to<std::chrono::seconds, si::time<si::hour, std::chrono::seconds::rep>>);
|
||||||
|
static_assert(std::constructible_from<time_point<si::second, sys_seconds::rep>, sys_seconds>);
|
||||||
|
static_assert(!std::convertible_to<sys_seconds, time_point<si::second, sys_seconds::rep>>);
|
||||||
|
static_assert(std::constructible_from<time_point<si::day, sys_days::rep>, sys_days>);
|
||||||
|
static_assert(!std::convertible_to<sys_days, time_point<si::day, sys_days::rep>>);
|
||||||
|
static_assert(std::constructible_from<time_point<si::second, sys_days::rep>, sys_days>);
|
||||||
|
static_assert(!std::convertible_to<sys_days, time_point<si::second, sys_days::rep>>);
|
||||||
|
static_assert(!std::constructible_from<time_point<si::day, sys_seconds::rep>, sys_seconds>);
|
||||||
|
static_assert(!std::convertible_to<sys_seconds, time_point<si::day, sys_seconds::rep>>);
|
||||||
|
|
||||||
// construction - different rep type (integral to a floating-point)
|
// construction - different rep type (integral to a floating-point)
|
||||||
static_assert(std::constructible_from<si::time<si::second>, std::chrono::seconds>);
|
static_assert(std::constructible_from<si::time<si::second>, std::chrono::seconds>);
|
||||||
@@ -50,15 +64,25 @@ static_assert(std::constructible_from<si::time<si::second>, std::chrono::hours>)
|
|||||||
static_assert(!std::convertible_to<std::chrono::hours, si::time<si::second>>);
|
static_assert(!std::convertible_to<std::chrono::hours, si::time<si::second>>);
|
||||||
static_assert(std::constructible_from<si::time<si::hour>, std::chrono::seconds>);
|
static_assert(std::constructible_from<si::time<si::hour>, std::chrono::seconds>);
|
||||||
static_assert(!std::convertible_to<std::chrono::seconds, si::time<si::hour>>);
|
static_assert(!std::convertible_to<std::chrono::seconds, si::time<si::hour>>);
|
||||||
|
static_assert(std::constructible_from<time_point<si::second>, sys_seconds>);
|
||||||
|
static_assert(!std::convertible_to<sys_seconds, time_point<si::second>>);
|
||||||
|
static_assert(std::constructible_from<time_point<si::second>, sys_days>);
|
||||||
|
static_assert(!std::convertible_to<sys_days, time_point<si::second>>);
|
||||||
|
static_assert(std::constructible_from<time_point<si::day>, sys_seconds>);
|
||||||
|
static_assert(!std::convertible_to<sys_seconds, time_point<si::day>>);
|
||||||
|
|
||||||
|
|
||||||
// CTAD
|
// CTAD
|
||||||
static_assert(is_same_v<decltype(quantity{1s}), si::time<si::second, std::chrono::seconds::rep>>);
|
static_assert(is_same_v<decltype(quantity{1s}), si::time<si::second, std::chrono::seconds::rep>>);
|
||||||
static_assert(is_same_v<decltype(quantity{1h}), si::time<si::hour, std::chrono::hours::rep>>);
|
static_assert(is_same_v<decltype(quantity{1h}), si::time<si::hour, std::chrono::hours::rep>>);
|
||||||
|
static_assert(is_same_v<decltype(quantity_point{sys_seconds{1s}}), time_point<si::second, sys_seconds::rep>>);
|
||||||
|
static_assert(is_same_v<decltype(quantity_point{sys_days{sys_days::duration{1}}}), time_point<si::day, sys_days::rep>>);
|
||||||
|
|
||||||
// operators
|
// operators
|
||||||
static_assert(quantity{1s} + 1_q_s == 2_q_s);
|
static_assert(quantity{1s} + 1_q_s == 2_q_s);
|
||||||
static_assert(quantity{1s} + 1_q_min == 61_q_s);
|
static_assert(quantity{1s} + 1_q_min == 61_q_s);
|
||||||
static_assert(10_q_m / quantity{2s} == 5_q_m_per_s);
|
static_assert(10_q_m / quantity{2s} == 5_q_m_per_s);
|
||||||
|
static_assert(quantity_point{sys_seconds{1s}} + 1_q_s == quantity_point{2_q_s});
|
||||||
|
static_assert(quantity_point{sys_seconds{1s}} + 1_q_min == quantity_point{61_q_s});
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@@ -39,6 +39,7 @@ using namespace units;
|
|||||||
namespace si = physical::si;
|
namespace si = physical::si;
|
||||||
using namespace si;
|
using namespace si;
|
||||||
using namespace unit_constants;
|
using namespace unit_constants;
|
||||||
|
using sys_seconds = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
|
||||||
|
|
||||||
constexpr auto cgs_cm = cgs::unit_constants::cm;
|
constexpr auto cgs_cm = cgs::unit_constants::cm;
|
||||||
|
|
||||||
@@ -303,6 +304,8 @@ static_assert(!constructible_or_convertible_from<nth_apple<one, int>>(quantity_p
|
|||||||
static_assert(!constructible_or_convertible_from<nth_apple<one, int>>(quantity_point(dimensionless<percent, int>(1))));
|
static_assert(!constructible_or_convertible_from<nth_apple<one, int>>(quantity_point(dimensionless<percent, int>(1))));
|
||||||
static_assert(!constructible_or_convertible_from<nth_apple<one, int>>(quantity_point(dimensionless<percent, double>(1))));
|
static_assert(!constructible_or_convertible_from<nth_apple<one, int>>(quantity_point(dimensionless<percent, double>(1))));
|
||||||
static_assert(!constructible_or_convertible_from<nth_apple<one, double>>(quantity_point(1.0 * s)));
|
static_assert(!constructible_or_convertible_from<nth_apple<one, double>>(quantity_point(1.0 * s)));
|
||||||
|
|
||||||
|
static_assert(construct_from_only<quantity_point_kind<time_point_kind, second, int>>(sys_seconds{42s}).relative().common() == 42 * s);
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
|
|
||||||
|
@@ -36,6 +36,7 @@ using namespace units;
|
|||||||
using namespace physical::si;
|
using namespace physical::si;
|
||||||
using namespace unit_constants;
|
using namespace unit_constants;
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
using sys_seconds = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
|
||||||
|
|
||||||
// class invariants
|
// class invariants
|
||||||
|
|
||||||
@@ -66,7 +67,9 @@ static_assert(quantity_point(1).relative() == quantity(1));
|
|||||||
static_assert(!std::is_convertible_v<int, quantity_point<dim_one, one, int>>);
|
static_assert(!std::is_convertible_v<int, quantity_point<dim_one, one, int>>);
|
||||||
|
|
||||||
static_assert(quantity_point(42s).relative() == 42 * s);
|
static_assert(quantity_point(42s).relative() == 42 * s);
|
||||||
|
static_assert(quantity_point(sys_seconds{42s}).relative() == 42 * s);
|
||||||
static_assert(!std::is_convertible_v<std::chrono::seconds, quantity_point<dim_time, second, std::chrono::seconds::rep>>);
|
static_assert(!std::is_convertible_v<std::chrono::seconds, quantity_point<dim_time, second, std::chrono::seconds::rep>>);
|
||||||
|
static_assert(!std::is_convertible_v<sys_seconds, quantity_point<dim_time, second, sys_seconds::rep>>);
|
||||||
|
|
||||||
static_assert(quantity_point<dim_length, metre, int>().relative() == 0_q_m);
|
static_assert(quantity_point<dim_length, metre, int>().relative() == 0_q_m);
|
||||||
constexpr quantity_point<dim_length, metre, int> km{1000_q_m};
|
constexpr quantity_point<dim_length, metre, int> km{1000_q_m};
|
||||||
|
Reference in New Issue
Block a user