2020-05-30 21:07:11 -04: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 <units/quantity.h>
|
2020-09-04 23:00:57 +02:00
|
|
|
#include <compare>
|
|
|
|
|
|
2020-05-30 21:07:11 -04:00
|
|
|
namespace units {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief A quantity point
|
|
|
|
|
*
|
2020-08-17 19:28:47 -04:00
|
|
|
* An absolute quantity with respect to zero (which represents some origin).
|
2020-05-30 21:07:11 -04:00
|
|
|
*
|
|
|
|
|
* @tparam D a dimension of the quantity point (can be either a BaseDimension or a DerivedDimension)
|
|
|
|
|
* @tparam U a measurement unit of the quantity point
|
|
|
|
|
* @tparam Rep a type to be used to represent values of a quantity point
|
|
|
|
|
*/
|
2021-03-19 07:53:18 +01:00
|
|
|
template<Dimension D, UnitOf<D> U, Representation Rep = double>
|
2020-08-17 22:27:26 -04:00
|
|
|
class quantity_point {
|
2020-05-30 21:07:11 -04:00
|
|
|
public:
|
|
|
|
|
using quantity_type = quantity<D, U, Rep>;
|
|
|
|
|
using dimension = typename quantity_type::dimension;
|
|
|
|
|
using unit = typename quantity_type::unit;
|
|
|
|
|
using rep = typename quantity_type::rep;
|
|
|
|
|
|
|
|
|
|
private:
|
2020-05-31 15:05:17 -04:00
|
|
|
quantity_type q_{};
|
2020-05-30 21:07:11 -04:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
quantity_point() = default;
|
|
|
|
|
quantity_point(const quantity_point&) = default;
|
|
|
|
|
quantity_point(quantity_point&&) = default;
|
|
|
|
|
|
2021-01-04 18:36:26 -04:00
|
|
|
template<safe_convertible_to_<rep> Value>
|
|
|
|
|
requires is_same_v<dimension, dim_one> && std::is_constructible_v<quantity_type, Value>
|
|
|
|
|
constexpr explicit quantity_point(const Value& v) : q_(v) {}
|
|
|
|
|
|
|
|
|
|
constexpr explicit quantity_point(const quantity_type& q) : q_{q} {}
|
|
|
|
|
|
|
|
|
|
template<QuantityLike Q>
|
|
|
|
|
requires std::is_constructible_v<quantity_type, Q>
|
2020-05-30 21:07:11 -04:00
|
|
|
constexpr explicit quantity_point(const Q& q) : q_{q} {}
|
|
|
|
|
|
2021-02-17 02:57:59 -04:00
|
|
|
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)} {}
|
|
|
|
|
|
2020-05-30 21:07:11 -04:00
|
|
|
template<QuantityPoint QP2>
|
|
|
|
|
requires std::is_convertible_v<typename QP2::quantity_type, quantity_type>
|
2021-03-13 19:55:18 +01:00
|
|
|
constexpr explicit(false) quantity_point(const QP2& qp) : q_{qp.relative()} {}
|
2020-05-30 21:07:11 -04:00
|
|
|
|
|
|
|
|
quantity_point& operator=(const quantity_point&) = default;
|
|
|
|
|
quantity_point& operator=(quantity_point&&) = default;
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr quantity_type relative() const noexcept { return q_; }
|
|
|
|
|
|
2020-08-17 22:27:26 -04:00
|
|
|
[[nodiscard]] static constexpr quantity_point min() noexcept
|
2020-09-08 13:09:34 +02:00
|
|
|
requires requires { quantity_type::min(); }
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
|
|
|
|
return quantity_point(quantity_type::min());
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 22:27:26 -04:00
|
|
|
[[nodiscard]] static constexpr quantity_point max() noexcept
|
2020-09-08 13:09:34 +02:00
|
|
|
requires requires { quantity_type::max(); }
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
|
|
|
|
return quantity_point(quantity_type::max());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr quantity_point& operator++()
|
2020-09-08 13:09:34 +02:00
|
|
|
requires requires(quantity_type q) { ++q; }
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
|
|
|
|
++q_;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 22:27:26 -04:00
|
|
|
[[nodiscard]] constexpr quantity_point operator++(int)
|
2020-09-08 13:09:34 +02:00
|
|
|
requires requires(quantity_type q) { q++; }
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
|
|
|
|
return quantity_point(q_++);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr quantity_point& operator--()
|
2020-09-08 13:09:34 +02:00
|
|
|
requires requires(quantity_type q) { --q; }
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
|
|
|
|
--q_;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 22:27:26 -04:00
|
|
|
[[nodiscard]] constexpr quantity_point operator--(int)
|
2020-09-08 13:09:34 +02:00
|
|
|
requires requires(quantity_type q) { q--; }
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
|
|
|
|
return quantity_point(q_--);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr quantity_point& operator+=(const quantity_type& q)
|
2021-02-21 15:41:22 -04:00
|
|
|
requires requires { q_ += q; }
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
|
|
|
|
q_ += q;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr quantity_point& operator-=(const quantity_type& q)
|
2020-09-08 13:09:34 +02:00
|
|
|
requires requires(quantity_type q1, quantity_type q2) { q1 -= q2; }
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
|
|
|
|
q_ -= q;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hidden Friends
|
|
|
|
|
// Below friend functions are to be found via argument-dependent lookup only
|
2020-09-08 13:09:34 +02:00
|
|
|
|
|
|
|
|
template<Quantity Q>
|
|
|
|
|
[[nodiscard]] friend constexpr QuantityPoint auto operator+(const quantity_point& lhs, const Q& rhs)
|
2021-02-21 15:59:00 -04:00
|
|
|
requires requires(quantity_type q) { q + rhs; }
|
2020-09-08 13:09:34 +02:00
|
|
|
{
|
|
|
|
|
const auto q = lhs.relative() + rhs;
|
|
|
|
|
using q_type = decltype(q);
|
|
|
|
|
return quantity_point<typename q_type::dimension, typename q_type::unit, typename q_type::rep>(q);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Quantity Q>
|
|
|
|
|
[[nodiscard]] friend constexpr QuantityPoint auto operator+(const Q& lhs, const quantity_point& rhs)
|
|
|
|
|
requires requires { rhs + lhs; }
|
|
|
|
|
{
|
|
|
|
|
return rhs + lhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Quantity Q>
|
|
|
|
|
[[nodiscard]] friend constexpr QuantityPoint auto operator-(const quantity_point& lhs, const Q& rhs)
|
2021-02-21 15:59:00 -04:00
|
|
|
requires requires(quantity_type q) { q - rhs; }
|
2020-09-08 13:09:34 +02:00
|
|
|
{
|
|
|
|
|
const auto q = lhs.relative() - rhs;
|
|
|
|
|
using q_type = decltype(q);
|
|
|
|
|
return quantity_point<typename q_type::dimension, typename q_type::unit, typename q_type::rep>(q);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<QuantityPoint QP>
|
|
|
|
|
[[nodiscard]] friend constexpr Quantity auto operator-(const quantity_point& lhs, const QP& rhs)
|
2021-02-21 15:59:00 -04:00
|
|
|
requires requires(quantity_type q) { q - rhs.relative(); }
|
2020-09-08 13:09:34 +02:00
|
|
|
{
|
|
|
|
|
return lhs.relative() - rhs.relative();
|
|
|
|
|
}
|
2020-05-30 21:07:11 -04:00
|
|
|
|
2020-05-30 21:53:24 -04:00
|
|
|
template<QuantityPoint QP>
|
|
|
|
|
requires std::three_way_comparable_with<quantity_type, typename QP::quantity_type>
|
2020-09-08 13:09:34 +02:00
|
|
|
[[nodiscard]] friend constexpr auto operator<=>(const quantity_point& lhs, const QP& rhs)
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
2020-06-19 19:37:52 +02:00
|
|
|
return lhs.relative() <=> rhs.relative();
|
2020-05-30 21:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
2020-05-30 21:53:24 -04:00
|
|
|
template<QuantityPoint QP>
|
|
|
|
|
requires std::equality_comparable_with<quantity_type, typename QP::quantity_type>
|
2020-09-08 13:09:34 +02:00
|
|
|
[[nodiscard]] friend constexpr bool operator==(const quantity_point& lhs, const QP& rhs)
|
2020-05-30 21:07:11 -04:00
|
|
|
{
|
2020-06-19 19:37:52 +02:00
|
|
|
return lhs.relative() == rhs.relative();
|
2020-05-30 21:07:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-19 07:53:18 +01:00
|
|
|
template<Representation Rep>
|
|
|
|
|
explicit(false) quantity_point(Rep) -> quantity_point<dim_one, one, Rep>;
|
2021-01-04 18:36:26 -04:00
|
|
|
|
|
|
|
|
template<QuantityLike Q>
|
|
|
|
|
quantity_point(Q) -> quantity_point<typename quantity_like_traits<Q>::dimension,
|
|
|
|
|
typename quantity_like_traits<Q>::unit, typename quantity_like_traits<Q>::rep>;
|
2020-05-30 21:07:11 -04:00
|
|
|
|
2021-02-17 02:57:59 -04:00
|
|
|
template<QuantityPointLike QP>
|
2021-02-20 17:19:43 +01:00
|
|
|
explicit quantity_point(QP) -> quantity_point<typename quantity_point_like_traits<QP>::dimension,
|
|
|
|
|
typename quantity_point_like_traits<QP>::unit,
|
|
|
|
|
typename quantity_point_like_traits<QP>::rep>;
|
2021-02-17 02:57:59 -04:00
|
|
|
|
2020-05-30 21:07:11 -04:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
|
|
template<typename D, typename U, typename Rep>
|
|
|
|
|
inline constexpr bool is_quantity_point<quantity_point<D, U, Rep>> = true;
|
|
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
|
|
} // namespace units
|