Files
mp-units/src/core/include/mp_units/quantity.h
T

544 lines
18 KiB
C++
Raw Normal View History

2019-12-28 10:37:18 +00:00
2018-08-22 12:11:19 +02:00
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <mp_units/bits/dimension_concepts.h>
#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>
2023-02-03 11:08:13 +01:00
#include <mp_units/bits/sudo_cast.h>
#include <mp_units/bits/unit_concepts.h>
#include <mp_units/customization_points.h>
#include <mp_units/dimension.h>
#include <mp_units/quantity_spec.h>
#include <mp_units/reference.h>
#include <mp_units/unit.h>
2019-12-11 11:27:51 +01:00
#include <compare>
2022-10-09 21:32:38 +01:00
#include <utility>
2018-08-22 12:11:19 +02:00
namespace mp_units {
2018-08-22 12:11:19 +02:00
namespace detail {
2022-03-28 10:17:46 -04:00
template<typename T>
concept QuantityOne = QuantityOf<T, dimensionless[one]>;
2022-03-28 10:17:46 -04:00
template<QuantityLike Q>
2022-12-23 17:58:57 +01:00
using quantity_like_type = quantity<quantity_like_traits<Q>::reference, typename quantity_like_traits<Q>::rep>;
2022-11-30 17:54:13 +01:00
template<typename T, typename Arg>
concept RepSafeConstructibleFrom = // exposition only
std::constructible_from<T, Arg> && (treat_as_floating_point<T> || !treat_as_floating_point<Arg>);
2018-08-22 12:11:19 +02:00
// QFrom ratio is an exact multiple of QTo
template<typename QFrom, typename QTo>
concept Harmonic = // exposition only
2022-10-22 21:05:14 +02:00
Quantity<QFrom> && Quantity<QTo> &&
is_integral(get_canonical_unit(QFrom::unit).mag / get_canonical_unit(QTo::unit).mag);
template<typename QFrom, typename QTo>
concept QuantityConvertibleTo = // exposition only
2023-02-03 11:08:13 +01:00
Quantity<QFrom> && Quantity<QTo> && requires(QFrom q) { detail::sudo_cast<QTo>(q); } &&
(treat_as_floating_point<typename QTo::rep> ||
(!treat_as_floating_point<typename QFrom::rep> && Harmonic<QFrom, QTo>));
template<quantity_character Ch, typename Func, typename T, typename U>
concept InvokeResultOf = std::regular_invocable<Func, T, U> && RepresentationOf<std::invoke_result_t<Func, T, U>, Ch>;
} // namespace detail
/**
* @brief A quantity
*
* Property of a phenomenon, body, or substance, where the property has a magnitude that can be
2022-12-23 18:01:51 +01:00
* expressed by means of a number and a reference.
*
2022-12-23 18:01:51 +01:00
* @tparam R a reference of the quantity providing all information about quantity properties
* @tparam Rep a type to be used to represent values of a quantity
*/
template<Reference auto R, RepresentationOf<get_quantity_spec(R).character> Rep = double>
class quantity {
public:
2022-12-23 19:16:23 +01:00
Rep number_; // needs to be public for a structural type
2022-12-23 18:03:15 +01:00
// member types and values
2022-12-16 18:15:48 +01:00
static constexpr Reference auto reference = R;
static constexpr QuantitySpec auto quantity_spec = get_quantity_spec(reference);
static constexpr Dimension auto dimension = quantity_spec.dimension;
static constexpr Unit auto unit = get_unit(reference);
2022-12-23 18:01:51 +01:00
using rep = Rep;
// static member functions
[[nodiscard]] static constexpr quantity zero() noexcept
requires requires { quantity_values<rep>::zero(); }
{
return quantity(quantity_values<rep>::zero());
}
[[nodiscard]] static constexpr quantity one() noexcept
requires requires { quantity_values<rep>::one(); }
{
return quantity(quantity_values<rep>::one());
}
[[nodiscard]] static constexpr quantity min() noexcept
requires requires { quantity_values<rep>::min(); }
{
return quantity(quantity_values<rep>::min());
}
[[nodiscard]] static constexpr quantity max() noexcept
requires requires { quantity_values<rep>::max(); }
{
return quantity(quantity_values<rep>::max());
}
// construction, assignment, destruction
quantity() = default;
quantity(const quantity&) = default;
quantity(quantity&&) = default;
template<typename Value>
requires detail::RepSafeConstructibleFrom<rep, Value>
constexpr explicit(!detail::QuantityOne<quantity>) quantity(Value&& v) : number_(std::forward<Value>(v))
{
}
template<detail::QuantityConvertibleTo<quantity> Q>
2023-02-03 11:08:13 +01:00
constexpr explicit(false) quantity(const Q& q) : number_(detail::sudo_cast<quantity>(q).number())
{
}
template<QuantityLike Q>
requires detail::QuantityConvertibleTo<detail::quantity_like_type<Q>, quantity>
2022-12-23 19:16:23 +01:00
constexpr explicit quantity(const Q& q) : quantity(detail::quantity_like_type<Q>(quantity_like_traits<Q>::number(q)))
{
}
2021-01-04 13:05:26 +01:00
quantity& operator=(const quantity&) = default;
quantity& operator=(quantity&&) = default;
// data access
[[nodiscard]] constexpr const rep& number() const& noexcept { return number_; }
[[nodiscard]] constexpr rep&& number() && noexcept { return std::move(number_); }
[[nodiscard]] constexpr const rep&& number() const&& noexcept { return std::move(number_); }
template<Unit U>
requires requires(quantity q) { q[U{}]; }
[[nodiscard]] constexpr rep number_in(U) const noexcept
{
return (*this)[U{}].number();
}
2022-11-10 16:41:28 -10:00
template<Unit U>
requires detail::QuantityConvertibleTo<quantity, quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>>
[[nodiscard]] constexpr quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep> operator[](U) const
2022-11-10 16:41:28 -10:00
{
return quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>{*this};
2022-11-10 16:41:28 -10:00
}
// member unary operators
[[nodiscard]] constexpr Quantity auto operator+() const
requires requires(rep v) {
{
+v
} -> std::common_with<rep>;
}
{
2022-10-06 23:43:01 +01:00
using ret = quantity<R, decltype(+number())>;
return ret(+number());
2018-08-22 12:11:19 +02:00
}
[[nodiscard]] constexpr Quantity auto operator-() const
requires std::regular_invocable<std::negate<>, rep>
2019-08-05 18:22:53 +02:00
{
2022-10-06 23:43:01 +01:00
using ret = quantity<R, decltype(-number())>;
return ret(-number());
2019-08-05 18:22:53 +02:00
}
constexpr quantity& operator++()
requires requires(rep v) {
{
++v
} -> std::same_as<rep&>;
}
2018-08-22 12:11:19 +02:00
{
++number_;
return *this;
2018-08-22 12:11:19 +02:00
}
[[nodiscard]] constexpr quantity operator++(int)
requires requires(rep v) {
{
v++
} -> std::same_as<rep>;
}
2018-08-22 12:11:19 +02:00
{
return quantity(number_++);
2018-08-22 12:11:19 +02:00
}
constexpr quantity& operator--()
requires requires(rep v) {
{
--v
} -> std::same_as<rep&>;
}
2018-08-22 12:11:19 +02:00
{
--number_;
return *this;
2018-08-22 12:11:19 +02:00
}
[[nodiscard]] constexpr quantity operator--(int)
requires requires(rep v) {
{
v--
} -> std::same_as<rep>;
}
2018-08-22 12:11:19 +02:00
{
return quantity(number_--);
2018-08-22 12:11:19 +02:00
}
constexpr quantity& operator+=(const quantity& q)
requires requires(rep a, rep b) {
{
a += b
} -> std::same_as<rep&>;
}
2018-08-22 12:11:19 +02:00
{
number_ += q.number();
return *this;
2018-08-22 12:11:19 +02:00
}
constexpr quantity& operator-=(const quantity& q)
requires requires(rep a, rep b) {
{
a -= b
} -> std::same_as<rep&>;
}
2018-08-22 12:11:19 +02:00
{
number_ -= q.number();
return *this;
2018-08-22 12:11:19 +02:00
}
template<typename Rep2>
constexpr quantity& operator*=(const Rep2& rhs)
requires requires(rep a, const Rep2 b) {
{
a *= b
} -> std::same_as<rep&>;
}
2018-08-22 12:11:19 +02:00
{
number_ *= rhs;
return *this;
2018-08-22 12:11:19 +02:00
}
2022-12-23 18:01:51 +01:00
template<detail::QuantityOne Q>
2022-03-28 10:17:46 -04:00
constexpr quantity& operator*=(const Q& rhs)
requires requires(rep a, const typename Q::rep b) {
{
a *= b
} -> std::same_as<rep&>;
}
{
number_ *= rhs.number();
return *this;
}
2018-08-22 12:11:19 +02:00
template<typename Rep2>
constexpr quantity& operator/=(const Rep2& rhs)
requires requires(rep a, const Rep2 b) {
{
a /= b
} -> std::same_as<rep&>;
}
2018-08-22 12:11:19 +02:00
{
gsl_ExpectsAudit(rhs != quantity_values<Rep2>::zero());
number_ /= rhs;
return *this;
2018-08-22 12:11:19 +02:00
}
2022-12-23 18:01:51 +01:00
template<detail::QuantityOne Q>
2022-03-28 10:17:46 -04:00
constexpr quantity& operator/=(const Q& rhs)
requires requires(rep a, const typename Q::rep b) {
{
a /= b
} -> std::same_as<rep&>;
}
{
2022-03-28 10:17:46 -04:00
gsl_ExpectsAudit(rhs.number() != quantity_values<typename Q::rep>::zero());
number_ /= rhs.number();
return *this;
}
2018-08-22 12:11:19 +02:00
template<typename Rep2>
constexpr quantity& operator%=(const Rep2& rhs)
requires(!treat_as_floating_point<rep>) && (!treat_as_floating_point<Rep2>) && requires(rep a, const Rep2 b) {
{
a %= b
} -> std::same_as<rep&>;
}
2018-08-22 12:11:19 +02:00
{
gsl_ExpectsAudit(rhs != quantity_values<Rep2>::zero());
number_ %= rhs;
return *this;
2018-08-22 12:11:19 +02:00
}
template<detail::QuantityOne Q>
2022-03-28 10:17:46 -04:00
constexpr quantity& operator%=(const Q& rhs)
requires(!treat_as_floating_point<rep>) && (!treat_as_floating_point<typename Q::rep>) &&
requires(rep a, const typename Q::rep b) {
{
a %= b
} -> std::same_as<rep&>;
}
{
2022-03-28 10:17:46 -04:00
gsl_ExpectsAudit(rhs.number() != quantity_values<typename Q::rep>::zero());
number_ %= rhs.number();
return *this;
}
constexpr quantity& operator%=(const quantity& q)
requires(!treat_as_floating_point<rep>) && requires(rep a, rep b) {
{
a %= b
} -> std::same_as<rep&>;
}
2018-08-22 12:11:19 +02:00
{
gsl_ExpectsAudit(q.number() != quantity_values<rep>::zero());
number_ %= q.number();
return *this;
2018-08-22 12:11:19 +02:00
}
2019-12-17 20:15:38 +01:00
// Hidden Friends
// Below friend functions are to be found via argument-dependent lookup only
template<typename Value>
[[nodiscard]] friend constexpr Quantity auto operator+(const quantity& lhs, const Value& rhs)
2022-03-18 08:20:08 +01:00
requires requires { // TODO: Simplify when Clang catches up.
requires !Quantity<Value>;
requires unit == ::mp_units::one;
requires detail::InvokeResultOf<quantity_spec.character, std::plus<>, rep, Value>;
}
2020-09-08 11:02:16 +02:00
{
return ::mp_units::quantity(lhs.number() + rhs);
}
template<typename Value>
[[nodiscard]] friend constexpr Quantity auto operator+(const Value& lhs, const quantity& rhs)
2022-03-18 08:20:27 +01:00
requires requires { // TODO: Simplify when Clang catches up.
requires !Quantity<Value>;
requires unit == ::mp_units::one;
requires detail::InvokeResultOf<quantity_spec.character, std::plus<>, Value, rep>;
}
{
return ::mp_units::quantity(lhs + rhs.number());
2020-09-08 11:02:16 +02:00
}
template<typename Value>
[[nodiscard]] friend constexpr Quantity auto operator-(const quantity& lhs, const Value& rhs)
2022-03-18 08:20:36 +01:00
requires requires { // TODO: Simplify when Clang catches up.
requires !Quantity<Value>;
requires unit == ::mp_units::one;
requires detail::InvokeResultOf<quantity_spec.character, std::minus<>, rep, Value>;
}
2020-09-08 11:02:16 +02:00
{
return ::mp_units::quantity(lhs.number() - rhs);
}
template<typename Value>
[[nodiscard]] friend constexpr Quantity auto operator-(const Value& lhs, const quantity& rhs)
2022-03-18 08:20:43 +01:00
requires requires { // TODO: Simplify when Clang catches up.
requires !Quantity<Value>;
requires unit == ::mp_units::one;
requires detail::InvokeResultOf<quantity_spec.character, std::minus<>, Value, rep>;
}
{
return ::mp_units::quantity(lhs - rhs.number());
2020-09-08 11:02:16 +02:00
}
2021-09-17 08:58:15 -04:00
template<Representation Value>
requires detail::InvokeResultOf<quantity_spec.character, std::multiplies<>, rep, const Value&>
2020-09-08 13:09:34 +02:00
[[nodiscard]] friend constexpr Quantity auto operator*(const quantity& q, const Value& v)
2020-09-08 11:02:16 +02:00
{
2022-10-06 23:43:01 +01:00
using ret = quantity<R, std::invoke_result_t<std::multiplies<>, rep, Value>>;
return ret(q.number() * v);
2020-09-08 11:02:16 +02:00
}
2021-09-17 08:58:15 -04:00
template<Representation Value>
requires detail::InvokeResultOf<quantity_spec.character, std::multiplies<>, const Value&, rep>
2020-09-08 13:09:34 +02:00
[[nodiscard]] friend constexpr Quantity auto operator*(const Value& v, const quantity& q)
2020-09-08 11:02:16 +02:00
{
2022-10-06 23:43:01 +01:00
using ret = quantity<R, std::invoke_result_t<std::multiplies<>, Value, rep>>;
return ret(v * q.number());
2020-09-08 11:02:16 +02:00
}
template<typename Value>
requires(!Quantity<Value>) && detail::InvokeResultOf<quantity_spec.character, std::divides<>, rep, const Value&>
2020-09-08 13:09:34 +02:00
[[nodiscard]] friend constexpr Quantity auto operator/(const quantity& q, const Value& v)
2020-09-08 11:02:16 +02:00
{
2021-01-05 17:47:07 +01:00
gsl_ExpectsAudit(v != quantity_values<Value>::zero());
2022-10-06 23:43:01 +01:00
using ret = quantity<R, std::invoke_result_t<std::divides<>, rep, Value>>;
return ret(q.number() / v);
2020-09-08 11:02:16 +02:00
}
template<typename Value>
requires(!Quantity<Value>) && detail::InvokeResultOf<quantity_spec.character, std::divides<>, const Value&, rep>
[[nodiscard]] friend constexpr Quantity auto operator/(const Value& v, const quantity& q)
2020-09-08 11:02:16 +02:00
{
return (dimensionless[::mp_units::one] / reference)(v / q.number());
2020-09-08 11:02:16 +02:00
}
template<typename Value>
requires(!Quantity<Value>) && (!treat_as_floating_point<rep>) && (!treat_as_floating_point<Value>) &&
detail::InvokeResultOf<quantity_spec.character, std::modulus<>, rep, const Value&>
2020-09-08 13:09:34 +02:00
[[nodiscard]] friend constexpr Quantity auto operator%(const quantity& q, const Value& v)
2020-09-08 11:02:16 +02:00
{
gsl_ExpectsAudit(v != quantity_values<Value>::zero());
2022-10-06 23:43:01 +01:00
using ret = quantity<R, std::invoke_result_t<std::modulus<>, rep, Value>>;
return ret(q.number() % v);
2020-09-08 11:02:16 +02:00
}
[[nodiscard]] friend constexpr Quantity auto operator%(const quantity& lhs, const quantity& rhs)
requires(!treat_as_floating_point<rep>) && detail::InvokeResultOf<quantity_spec.character, std::modulus<>, rep, rep>
2020-09-08 11:02:16 +02:00
{
gsl_ExpectsAudit(rhs.number() != quantity_values<rep>::zero());
2022-10-06 23:43:01 +01:00
using ret = quantity<R, std::invoke_result_t<std::modulus<>, rep, rep>>;
return ret(lhs.number() % rhs.number());
2020-09-08 11:02:16 +02:00
}
[[nodiscard]] friend constexpr auto operator<=>(const quantity& lhs, const quantity& rhs)
requires std::three_way_comparable<rep>
#if __cpp_impl_three_way_comparison
= default;
#else
2019-12-11 11:27:51 +01:00
{
return lhs.number() <=> rhs.number();
2019-12-11 11:27:51 +01:00
}
#endif
2019-12-11 11:27:51 +01:00
[[nodiscard]] friend constexpr bool operator==(const quantity& lhs, const quantity& rhs) = default;
};
2018-08-22 12:11:19 +02:00
// CTAD
2022-10-06 23:43:01 +01:00
template<auto R, typename Rep>
explicit(false) quantity(quantity<R, Rep>) -> quantity<R, Rep>;
2022-05-22 19:47:11 +02:00
template<RepresentationOf<mp_units::quantity_character::scalar> Rep>
explicit(false) quantity(Rep) -> quantity<dimensionless[one], Rep>;
2020-09-23 16:34:13 +02:00
#if !UNITS_COMP_CLANG || UNITS_COMP_CLANG > 16
template<auto R, RepresentationOf<R.quantity_spec.character> Rep>
explicit(false) quantity(Rep&&) -> quantity<R, Rep>;
#endif
template<QuantityLike Q>
2022-12-23 17:58:57 +01:00
explicit quantity(Q) -> quantity<quantity_like_traits<Q>::reference, typename quantity_like_traits<Q>::rep>;
// non-member binary operators
template<Quantity Q1, Quantity Q2>
requires(get_kind(Q1::quantity_spec) == get_kind(Q2::quantity_spec)) &&
detail::InvokeResultOf<common_quantity_spec(Q1::quantity_spec, Q2::quantity_spec).character, std::plus<>,
typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr Quantity auto operator+(const Q1& lhs, const Q2& rhs)
{
constexpr auto ref = common_reference(Q1::reference, Q2::reference);
using ret = quantity<ref, decltype(lhs.number() + rhs.number())>;
return ret(ret(lhs).number() + ret(rhs).number());
}
template<Quantity Q1, Quantity Q2>
requires(get_kind(Q1::quantity_spec) == get_kind(Q2::quantity_spec)) &&
detail::InvokeResultOf<common_quantity_spec(Q1::quantity_spec, Q2::quantity_spec).character, std::minus<>,
typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr Quantity auto operator-(const Q1& lhs, const Q2& rhs)
{
constexpr auto ref = common_reference(Q1::reference, Q2::reference);
using ret = quantity<ref, decltype(lhs.number() - rhs.number())>;
return ret(ret(lhs).number() - ret(rhs).number());
}
template<Quantity Q1, Quantity Q2>
requires detail::InvokeResultOf<(Q1::quantity_spec * Q2::quantity_spec).character, std::multiplies<>,
typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr Quantity auto operator*(const Q1& lhs, const Q2& rhs)
{
return (Q1::reference * Q2::reference)(lhs.number() * rhs.number());
}
template<Quantity Q1, Quantity Q2>
requires detail::InvokeResultOf<(Q1::quantity_spec / Q2::quantity_spec).character, std::divides<>, typename Q1::rep,
typename Q2::rep>
[[nodiscard]] constexpr Quantity auto operator/(const Q1& lhs, const Q2& rhs)
{
gsl_ExpectsAudit(rhs.number() != quantity_values<typename Q2::rep>::zero());
return (Q1::reference / Q2::reference)(lhs.number() / rhs.number());
}
2021-05-11 15:03:04 +02:00
template<Quantity Q1, Quantity Q2>
requires(!treat_as_floating_point<typename Q1::rep>) && (!treat_as_floating_point<typename Q2::rep>) &&
(interconvertible(Q1::reference, Q2::reference) || QuantityOf<Q2, dimensionless>) &&
detail::InvokeResultOf<Q1::reference.character, std::modulus<>, typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr Quantity auto operator%(const Q1& lhs, const Q2& rhs)
{
gsl_ExpectsAudit(rhs.number() != quantity_values<typename Q2::rep>::zero());
using ret = quantity<Q1::reference, std::invoke_result_t<std::modulus<>, typename Q1::rep, typename Q2::rep>>;
2021-05-11 15:03:04 +02:00
return ret(lhs.number() % rhs.number());
}
template<Quantity Q1, Quantity Q2>
requires(get_kind(Q1::quantity_spec) == get_kind(Q2::quantity_spec)) &&
std::three_way_comparable_with<typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr auto operator<=>(const Q1& lhs, const Q2& rhs)
{
constexpr auto ref = common_reference(Q1::reference, Q2::reference);
2023-02-03 11:08:13 +01:00
return quantity<ref, typename Q1::rep>(lhs).number() <=> quantity<ref, typename Q2::rep>(rhs).number();
}
template<Quantity Q1, Quantity Q2>
requires(get_kind(Q1::quantity_spec) == get_kind(Q2::quantity_spec)) &&
std::equality_comparable_with<typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr bool operator==(const Q1& lhs, const Q2& rhs)
{
constexpr auto ref = common_reference(Q1::reference, Q2::reference);
2023-02-03 11:08:13 +01:00
return quantity<ref, typename Q1::rep>(lhs).number() == quantity<ref, typename Q2::rep>(rhs).number();
}
} // namespace mp_units
2022-10-09 21:32:38 +01:00
namespace std {
template<mp_units::Quantity Q1, mp_units::Quantity Q2>
2022-10-09 21:32:38 +01:00
requires requires {
{
mp_units::common_reference(Q1::reference, Q2::reference)
} -> mp_units::Reference;
typename common_type_t<typename Q1::rep, typename Q2::rep>;
}
2022-10-09 21:32:38 +01:00
struct common_type<Q1, Q2> {
public:
using type = mp_units::quantity<mp_units::common_reference(Q1::reference, Q2::reference),
common_type_t<typename Q1::rep, typename Q2::rep>>;
2022-10-09 21:32:38 +01:00
};
} // namespace std