Files
mp-units/src/include/units/quantity_cast.h
T

414 lines
15 KiB
C++
Raw Normal View History

// 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/concepts.h>
2020-03-26 16:37:47 +01:00
#include <units/customization_points.h>
#include <units/bits/dimension_op.h>
2020-05-30 21:07:11 -04:00
#include <units/bits/external/type_traits.h>
2020-09-05 18:20:19 -04:00
#include <units/bits/pow.h>
2020-02-23 15:36:07 +00:00
#include <cassert>
2020-09-04 23:00:57 +02:00
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable:4244) // warning C4244: 'argument': conversion from 'intmax_t' to 'T', possible loss of data with T=int
#endif //_MSC_VER
namespace units {
template<Dimension D, UnitOf<D> U, ScalableNumber Rep>
2020-09-08 11:02:16 +02:00
class quantity;
template<Dimension D, UnitOf<D> U, ScalableNumber Rep>
2020-09-08 11:02:16 +02:00
class quantity_point;
namespace detail {
template<typename D, typename U, typename Rep>
constexpr auto quantity_ratio(const quantity<D, U, Rep>&)
{
if constexpr(BaseDimension<D>) {
return U::ratio;
}
else {
return D::base_units_ratio * U::ratio / D::coherent_unit::ratio;
}
}
} // namespace detail
// QuantityOf
template<typename T, typename Dim>
concept QuantityOf = Quantity<T> && Dimension<Dim> && equivalent_dim<typename T::dimension, Dim>;
// quantity_cast
namespace detail {
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, typename CRep, bool NumIsOne, bool DenIsOne, bool ExpIsZero>
struct quantity_cast_impl;
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, true, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(q.count()));
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, true, false> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
if constexpr (treat_as_floating_point<CRep>) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(detail::fpow10<CRep>(CRatio.exp))));
} else {
2020-06-27 19:15:46 +02:00
if constexpr (CRatio.exp > 0) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(detail::ipow10(CRatio.exp))));
}
else {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(detail::ipow10(-CRatio.exp))));
}
}
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, false, true> {
template<typename Q>
static constexpr To cast(const Q& q)
{
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
2020-06-27 19:15:46 +02:00
(static_cast<CRep>(CRatio.num) /
static_cast<CRep>(CRatio.den))));
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, false, false> {
template<typename Q>
static constexpr To cast(const Q& q)
{
if constexpr (treat_as_floating_point<CRep>) {
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
2020-09-05 18:20:19 -04:00
static_cast<CRep>(detail::fpow10<CRep>(CRatio.exp)) *
2020-06-27 19:15:46 +02:00
(static_cast<CRep>(CRatio.num) /
static_cast<CRep>(CRatio.den))));
} else {
2020-06-27 19:15:46 +02:00
if constexpr (CRatio.exp > 0) {
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
2020-06-27 19:15:46 +02:00
static_cast<CRep>(CRatio.num) *
2020-09-05 18:20:19 -04:00
static_cast<CRep>(detail::ipow10(CRatio.exp)) /
2020-06-27 19:15:46 +02:00
static_cast<CRep>(CRatio.den)));
}
else {
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
2020-06-27 19:15:46 +02:00
static_cast<CRep>(CRatio.num) /
(static_cast<CRep>(CRatio.den) *
2020-09-05 18:20:19 -04:00
static_cast<CRep>(detail::ipow10(-CRatio.exp)))));
}
}
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, false, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(CRatio.den)));
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, false, false> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
if constexpr (treat_as_floating_point<CRep>) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(detail::fpow10<CRep>(CRatio.exp)) * (CRep{1} / static_cast<CRep>(CRatio.den))));
} else {
2020-06-27 19:15:46 +02:00
if constexpr (CRatio.exp > 0) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(detail::ipow10(CRatio.exp)) / static_cast<CRep>(CRatio.den)));
}
else {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) / (static_cast<CRep>(detail::ipow10(-CRatio.exp)) * static_cast<CRep>(CRatio.den))));
}
}
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, true, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num)));
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, true, false> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
if constexpr (treat_as_floating_point<CRep>) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) * static_cast<CRep>(detail::fpow10<CRep>(CRatio.exp))));
} else {
2020-06-27 19:15:46 +02:00
if constexpr (CRatio.exp > 0) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) * static_cast<CRep>(detail::ipow10(CRatio.exp))));
}
else {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) / static_cast<CRep>(detail::ipow10(-CRatio.exp))));
}
}
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, not_constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, true, false> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
if constexpr (treat_as_floating_point<CRep>) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * detail::fpow10<CRep>(CRatio.exp)));
} else {
2020-06-27 19:15:46 +02:00
if constexpr (CRatio.exp > 0) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * detail::ipow10(CRatio.exp)));
}
else {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() / detail::ipow10(-CRatio.exp)));
}
}
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, not_constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, false, true> {
template<typename Q>
static constexpr To cast(const Q& q)
{
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(q.count() * (CRatio.num / CRatio.den)));
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, not_constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, false, false> {
template<typename Q>
static constexpr To cast(const Q& q)
{
if constexpr (treat_as_floating_point<CRep>) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * detail::fpow10<CRep>(CRatio.exp) * (CRatio.num / CRatio.den)));
} else {
2020-06-27 19:15:46 +02:00
if constexpr (CRatio.exp > 0) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * detail::ipow10(CRatio.exp) / CRatio.den));
}
else {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count()) * CRatio.num / (CRatio.den * detail::ipow10(-CRatio.exp)));
}
}
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, not_constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, false, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(q.count() / CRatio.den));
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, not_constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, false, false> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
if constexpr (treat_as_floating_point<CRep>) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * detail::fpow10<CRep>(CRatio.exp) / CRatio.den));
} else {
2020-06-27 19:15:46 +02:00
if constexpr (CRatio.exp > 0) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * detail::ipow10(CRatio.exp) / CRatio.den));
}
else {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() / (detail::ipow10(-CRatio.exp) * CRatio.den)));
}
}
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, not_constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, true, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
2020-09-04 23:00:57 +02:00
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num));
}
};
2020-06-27 19:15:46 +02:00
template<typename To, ratio CRatio, not_constructible_from_integral CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, true, false> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
if constexpr (treat_as_floating_point<CRep>) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * detail::fpow10<CRep>(CRatio.exp)));
} else {
2020-06-27 19:15:46 +02:00
if constexpr (CRatio.exp > 0) {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * detail::ipow10(CRatio.exp)));
}
else {
2020-09-05 18:20:19 -04:00
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num / detail::ipow10(-CRatio.exp)));
}
}
}
};
template<typename Q1, typename Q2>
constexpr ratio cast_ratio(const Q1& from, const Q2& to)
2020-06-27 19:15:46 +02:00
{
2020-09-08 11:02:16 +02:00
using FromU = TYPENAME Q1::unit;
using ToU = TYPENAME Q2::unit;
if constexpr(same_unit_reference<FromU, ToU>::value) {
2020-06-27 19:15:46 +02:00
return FromU::ratio / ToU::ratio;
}
else {
return quantity_ratio(from) / quantity_ratio(to);
2020-06-27 19:15:46 +02:00
}
}
} // namespace detail
/**
* @brief Explcit cast of a quantity
*
* Implicit conversions between quantities of different types are allowed only for "safe"
* (i.e. non-truncating) conversion. In such cases an explicit cast have to be used.
*
* This cast gets the target quantity type to cast to. For example:
*
* auto q1 = units::quantity_cast<units::physical::si::time<units::physical::si::second>>(1q_ms);
*
* @tparam To a target quantity type to cast to
*/
template<Quantity To, typename D, typename U, typename Rep>
2020-03-25 15:04:33 +01:00
requires QuantityOf<To, D>
2020-09-08 13:09:34 +02:00
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
{
using c_ratio = std::integral_constant<ratio, detail::cast_ratio(quantity<D, U, Rep>(), To())>;
2020-03-25 15:04:33 +01:00
using c_rep = std::common_type_t<typename To::rep, Rep>;
2020-06-27 19:15:46 +02:00
using ret_unit = downcast_unit<typename To::dimension, To::unit::ratio>;
using ret = quantity<typename To::dimension, ret_unit, typename To::rep>;
2020-06-27 19:15:46 +02:00
using cast = detail::quantity_cast_impl<ret, c_ratio::value, c_rep, c_ratio::value.num == 1, c_ratio::value.den == 1, c_ratio::value.exp == 0>;
return cast::cast(q);
}
/**
* @brief Explcit cast of a quantity
*
* Implicit conversions between quantities of different types are allowed only for "safe"
* (i.e. non-truncating) conversion. In such cases an explicit cast have to be used.
*
* This cast gets only the target dimension to cast to. For example:
*
* auto q1 = units::quantity_cast<units::physical::si::acceleration>(200q_Gal);
*
* @tparam ToD a dimension type to use for a target quantity
*/
template<Dimension ToD, typename D, typename U, typename Rep>
requires equivalent_dim<ToD, D>
2020-09-08 13:09:34 +02:00
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
{
return quantity_cast<quantity<ToD, dimension_unit<ToD>, Rep>>(q);
}
/**
* @brief Explcit cast of a quantity
*
* Implicit conversions between quantities of different types are allowed only for "safe"
* (i.e. non-truncating) conversion. In such cases an explicit cast have to be used.
*
* This cast gets only the target unit to cast to. For example:
*
* auto q1 = units::quantity_cast<units::physical::si::second>(1q_ms);
*
* @tparam ToU a unit type to use for a target quantity
*/
template<Unit ToU, typename D, typename U, typename Rep>
requires UnitOf<ToU, D>
2020-09-08 13:09:34 +02:00
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
{
return quantity_cast<quantity<D, ToU, Rep>>(q);
}
/**
* @brief Explcit cast of a quantity
*
* Implicit conversions between quantities of different types are allowed only for "safe"
* (i.e. non-truncating) conversion. In such cases an explicit cast have to be used.
*
* This cast gets only representation to cast to. For example:
*
2020-02-17 15:56:06 +01:00
* auto q1 = units::quantity_cast<int>(1q_ms);
*
* @tparam ToRep a representation type to use for a target quantity
*/
template<ScalableNumber ToRep, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
{
return quantity_cast<quantity<D, U, ToRep>>(q);
}
2020-05-30 21:07:11 -04:00
/**
* @brief Explcit cast of a quantity point
*
* Implicit conversions between quantity points of different types are allowed only for "safe"
* (i.e. non-truncating) conversion. In other cases an explicit cast has to be used.
*
* This cast gets the target quantity point type to cast to or anything that works for quantity_cast. For example:
*
* auto q1 = units::quantity_point_cast<decltype(quantity_point{0q_s})>(quantity_point{1q_ms});
* auto q1 = units::quantity_point_cast<units::physical::si::time<units::physical::si::second>>(quantity_point{1q_ms});
* auto q1 = units::quantity_point_cast<units::physical::si::acceleration>(quantity_point{200q_Gal});
* auto q1 = units::quantity_point_cast<units::physical::si::second>(quantity_point{1q_ms});
* auto q1 = units::quantity_point_cast<int>(quantity_point{1q_ms});
*
* @tparam CastSpec a target quantity point type to cast to or anything that works for quantity_cast
*/
template<typename CastSpec, typename D, typename U, typename Rep>
requires is_specialization_of<CastSpec, quantity_point> ||
2020-05-30 21:07:11 -04:00
requires(quantity<D, U, Rep> q) { quantity_cast<CastSpec>(q); }
2020-09-08 13:09:34 +02:00
[[nodiscard]] constexpr auto quantity_point_cast(const quantity_point<D, U, Rep>& qp)
2020-05-30 21:07:11 -04:00
{
if constexpr (is_specialization_of<CastSpec, quantity_point>)
2020-05-30 21:07:11 -04:00
return quantity_point(quantity_cast<typename CastSpec::quantity_type>(qp.relative()));
else
return quantity_point(quantity_cast<CastSpec>(qp.relative()));
}
} // namespace units
2020-09-04 23:00:57 +02:00
#ifdef _MSC_VER
#pragma warning (pop)
#endif //_MSC_VER