forked from mpusz/mp-units
Small quantity formatting cleanup with clang-format
This commit is contained in:
@@ -22,8 +22,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/unit.h>
|
||||
#include <units/bits/concepts.h>
|
||||
#include <units/unit.h>
|
||||
#include <limits>
|
||||
|
||||
namespace units {
|
||||
@@ -36,7 +36,7 @@ namespace units {
|
||||
|
||||
// partial specialization below after the first quantity forward declaration
|
||||
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept Quantity = detail::is_quantity<T>;
|
||||
@@ -71,19 +71,21 @@ namespace units {
|
||||
};
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2, typename Rep>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
struct common_quantity_impl<quantity<U1, Rep1>, quantity<U2, Rep2>, Rep> {
|
||||
using type = quantity<downcast_target<unit<typename U1::dimension, common_ratio<typename U1::ratio, typename U2::ratio>>>, Rep>;
|
||||
using type =
|
||||
quantity<downcast_target<unit<typename U1::dimension, common_ratio<typename U1::ratio, typename U2::ratio>>>,
|
||||
Rep>;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template<Quantity Q1, Quantity Q2, Scalar Rep = std::common_type_t<typename Q1::rep, typename Q2::rep>>
|
||||
using common_quantity = detail::common_quantity_impl<Q1, Q2, Rep>::type;
|
||||
|
||||
// treat_as_floating_point
|
||||
|
||||
template<typename Rep> // todo Conceptify that
|
||||
template<typename Rep> // todo Conceptify that
|
||||
inline constexpr bool treat_as_floating_point = std::is_floating_point_v<Rep>;
|
||||
|
||||
// quantity_cast
|
||||
@@ -96,12 +98,12 @@ namespace units {
|
||||
static constexpr To cast(const Q& q)
|
||||
{
|
||||
if constexpr(treat_as_floating_point<CRep>) {
|
||||
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) * (static_cast<CRep>(CR::num) /
|
||||
static_cast<CRep>(CR::den))));
|
||||
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) *
|
||||
(static_cast<CRep>(CR::num) / static_cast<CRep>(CR::den))));
|
||||
}
|
||||
else {
|
||||
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CR::num) /
|
||||
static_cast<CRep>(CR::den)));
|
||||
return To(
|
||||
static_cast<To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CR::num) / static_cast<CRep>(CR::den)));
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -142,7 +144,7 @@ namespace units {
|
||||
|
||||
template<Quantity To, typename U, typename Rep>
|
||||
[[nodiscard]] constexpr To quantity_cast(const quantity<U, Rep>& q)
|
||||
requires same_dim<typename To::dimension, typename U::dimension>
|
||||
requires same_dim<typename To::dimension, typename U::dimension>
|
||||
{
|
||||
using c_ratio = ratio_divide<typename U::ratio, typename To::unit::ratio>;
|
||||
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
|
||||
@@ -168,7 +170,6 @@ namespace units {
|
||||
return quantity_cast<quantity<U, ToRep>>(q);
|
||||
}
|
||||
|
||||
|
||||
// quantity_values
|
||||
|
||||
template<Scalar Rep>
|
||||
@@ -195,18 +196,18 @@ namespace units {
|
||||
|
||||
template<Scalar Rep2>
|
||||
requires std::convertible_to<Rep2, rep> &&
|
||||
(treat_as_floating_point<rep> || (!treat_as_floating_point<Rep2>))
|
||||
constexpr explicit quantity(const Rep2& r) : value_{static_cast<rep>(r)}
|
||||
(treat_as_floating_point<rep> || (!treat_as_floating_point<Rep2>))
|
||||
constexpr explicit quantity(const Rep2& r): value_{static_cast<rep>(r)}
|
||||
{
|
||||
}
|
||||
|
||||
template<Quantity Q2>
|
||||
requires same_dim<dimension, typename Q2::dimension> &&
|
||||
std::convertible_to<typename Q2::rep, rep> &&
|
||||
(treat_as_floating_point<rep> ||
|
||||
(std::ratio_divide<typename Q2::unit::ratio, typename unit::ratio>::den == 1 &&
|
||||
!treat_as_floating_point<typename Q2::rep>))
|
||||
constexpr quantity(const Q2& q) : value_{quantity_cast<quantity>(q).count()}
|
||||
std::convertible_to<typename Q2::rep, rep> &&
|
||||
(treat_as_floating_point<rep> ||
|
||||
(std::ratio_divide<typename Q2::unit::ratio, typename unit::ratio>::den == 1 &&
|
||||
!treat_as_floating_point<typename Q2::rep>))
|
||||
constexpr quantity(const Q2& q): value_{quantity_cast<quantity>(q).count()}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -262,7 +263,7 @@ namespace units {
|
||||
|
||||
template<Scalar Rep2>
|
||||
constexpr quantity& operator%=(const Rep2& rhs)
|
||||
requires (!treat_as_floating_point<rep> && !treat_as_floating_point<Rep2>)
|
||||
requires (!treat_as_floating_point<rep> && !treat_as_floating_point<Rep2>)
|
||||
{
|
||||
value_ %= rhs;
|
||||
return *this;
|
||||
@@ -276,11 +277,9 @@ namespace units {
|
||||
}
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator+(const quantity<U1, Rep1>& lhs,
|
||||
const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
[[nodiscard]] constexpr Quantity operator+(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
using common_rep = decltype(lhs.count() + rhs.count());
|
||||
using ret = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>, common_rep>;
|
||||
@@ -288,9 +287,8 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator-(const quantity<U1, Rep1>& lhs,
|
||||
const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
[[nodiscard]] constexpr Quantity operator-(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
using common_rep = decltype(lhs.count() - rhs.count());
|
||||
using ret = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>, common_rep>;
|
||||
@@ -298,26 +296,23 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U, typename Rep1, Scalar Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator*(const quantity<U, Rep1>& q,
|
||||
const Rep2& v)
|
||||
requires (!Quantity<Rep2>)
|
||||
[[nodiscard]] constexpr Quantity operator*(const quantity<U, Rep1>& q, const Rep2& v)
|
||||
requires (!Quantity<Rep2>)
|
||||
{
|
||||
using common_rep = decltype(q.count()* v);
|
||||
using common_rep = decltype(q.count() * v);
|
||||
using ret = quantity<U, common_rep>;
|
||||
return ret(ret(q).count() * v);
|
||||
}
|
||||
|
||||
template<Scalar Rep1, typename U, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator*(const Rep1& v,
|
||||
const quantity<U, Rep2>& q)
|
||||
requires (!Quantity<Rep1>)
|
||||
[[nodiscard]] constexpr Quantity operator*(const Rep1& v, const quantity<U, Rep2>& q)
|
||||
requires (!Quantity<Rep1>)
|
||||
{
|
||||
return q * v;
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Scalar operator*(const quantity<U1, Rep1>& lhs,
|
||||
const quantity<U2, Rep2>& rhs)
|
||||
[[nodiscard]] constexpr Scalar operator*(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, dim_invert<typename U2::dimension>>
|
||||
{
|
||||
using common_rep = decltype(lhs.count() * rhs.count());
|
||||
@@ -326,10 +321,9 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator*(const quantity<U1, Rep1>& lhs,
|
||||
const quantity<U2, Rep2>& rhs)
|
||||
[[nodiscard]] constexpr Quantity operator*(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires (!same_dim<typename U1::dimension, dim_invert<typename U2::dimension>>) &&
|
||||
(treat_as_floating_point<decltype(lhs.count() * rhs.count())> ||
|
||||
(treat_as_floating_point<decltype(lhs.count() * rhs.count())> ||
|
||||
(std::ratio_multiply<typename U1::ratio, typename U2::ratio>::den == 1))
|
||||
{
|
||||
using dim = dimension_multiply<typename U1::dimension, typename U2::dimension>;
|
||||
@@ -339,9 +333,8 @@ namespace units {
|
||||
}
|
||||
|
||||
template<Scalar Rep1, typename U, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator/(const Rep1& v,
|
||||
const quantity<U, Rep2>& q)
|
||||
requires (!Quantity<Rep1>)
|
||||
[[nodiscard]] constexpr Quantity operator/(const Rep1& v, const quantity<U, Rep2>& q)
|
||||
requires (!Quantity<Rep1>)
|
||||
{
|
||||
Expects(q != std::remove_cvref_t<decltype(q)>(0));
|
||||
|
||||
@@ -353,9 +346,8 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U, typename Rep1, Scalar Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator/(const quantity<U, Rep1>& q,
|
||||
const Rep2& v)
|
||||
requires (!Quantity<Rep2>)
|
||||
[[nodiscard]] constexpr Quantity operator/(const quantity<U, Rep1>& q, const Rep2& v)
|
||||
requires (!Quantity<Rep2>)
|
||||
{
|
||||
Expects(v != Rep2{0});
|
||||
|
||||
@@ -365,9 +357,8 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Scalar operator/(const quantity<U1, Rep1>& lhs,
|
||||
const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
[[nodiscard]] constexpr Scalar operator/(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
Expects(rhs != std::remove_cvref_t<decltype(rhs)>(0));
|
||||
|
||||
@@ -377,11 +368,10 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator/(const quantity<U1, Rep1>& lhs,
|
||||
const quantity<U2, Rep2>& rhs)
|
||||
requires (!same_dim<typename U1::dimension, typename U2::dimension>) &&
|
||||
(treat_as_floating_point<decltype(lhs.count() / rhs.count())> ||
|
||||
(ratio_divide<typename U1::ratio, typename U2::ratio>::den == 1))
|
||||
[[nodiscard]] constexpr Quantity operator/(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires (!same_dim<typename U1::dimension, typename U2::dimension>) &&
|
||||
(treat_as_floating_point<decltype(lhs.count() / rhs.count())> ||
|
||||
(ratio_divide<typename U1::ratio, typename U2::ratio>::den == 1))
|
||||
{
|
||||
Expects(rhs != std::remove_cvref_t<decltype(rhs)>(0));
|
||||
|
||||
@@ -392,8 +382,7 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U, typename Rep1, Scalar Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator%(const quantity<U, Rep1>& q,
|
||||
const Rep2& v)
|
||||
[[nodiscard]] constexpr Quantity operator%(const quantity<U, Rep1>& q, const Rep2& v)
|
||||
{
|
||||
using common_rep = decltype(q.count() % v);
|
||||
using ret = quantity<U, common_rep>;
|
||||
@@ -401,19 +390,16 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity operator%(const quantity<U1, Rep1>& lhs,
|
||||
const quantity<U2, Rep2>& rhs)
|
||||
[[nodiscard]] constexpr Quantity operator%(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
{
|
||||
using common_rep = decltype(lhs.count() % rhs.count());
|
||||
using ret = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>, common_rep>;
|
||||
return ret(ret(lhs).count() % ret(rhs).count());
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator==(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
using ct = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>>;
|
||||
return ct(lhs).count() == ct(rhs).count();
|
||||
@@ -421,14 +407,14 @@ namespace units {
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator!=(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator<(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
using ct = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>>;
|
||||
return ct(lhs).count() < ct(rhs).count();
|
||||
@@ -436,21 +422,21 @@ namespace units {
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator<=(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
return !(rhs < lhs);
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator>(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator>=(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
Reference in New Issue
Block a user