Most of concepts moved to a new file + quantity.h split to smaller pieces

This commit is contained in:
Mateusz Pusz
2019-12-12 13:17:31 +01:00
parent 9d50ea6ab5
commit fbf3ef8c4f
15 changed files with 592 additions and 570 deletions

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include <units/physical/si/acceleration.h> #include <units/physical/si/acceleration.h>
#include <cmath>
#include <iostream> #include <iostream>
namespace { namespace {

View File

@@ -23,7 +23,7 @@
#pragma once #pragma once
#include <units/bits/external/fixed_string.h> #include <units/bits/external/fixed_string.h>
#include <units/unit_concept.h> #include <units/concepts.h>
#include <type_traits> #include <type_traits>
namespace units { namespace units {
@@ -54,29 +54,6 @@ struct base_dimension {
using base_unit = U; using base_unit = U;
}; };
// BaseDimension
namespace detail {
#if __GNUC__ == 9 && __GNUC_MINOR__ < 2
template<typename T>
inline constexpr bool is_base_dimension = true;
#else
template<typename T>
inline constexpr bool is_base_dimension = false;
template<basic_fixed_string Name, typename... Params>
inline constexpr bool is_base_dimension<base_dimension<Name, Params...>> = true;
#endif
} // namespace detail
template<typename T>
concept BaseDimension = detail::is_base_dimension<typename T::base_type_workaround>;
// base_dimension_less // base_dimension_less
// TODO Remove the below when https://bugs.llvm.org/show_bug.cgi?id=32208 is fixed // TODO Remove the below when https://bugs.llvm.org/show_bug.cgi?id=32208 is fixed
// clang-format off // clang-format off

View File

@@ -0,0 +1,66 @@
// 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/bits/dimension_op.h>
namespace units {
template<Dimension D, UnitOf<D> U, Scalar Rep>
class quantity;
namespace detail {
template<typename Q1, typename Q2, typename Rep>
struct common_quantity_impl;
template<typename D, typename U, typename Rep1, typename Rep2, typename Rep>
struct common_quantity_impl<quantity<D, U, Rep1>, quantity<D, U, Rep2>, Rep> {
using type = quantity<D, U, Rep>;
};
template<typename D, typename U1, typename Rep1, typename U2, typename Rep2, typename Rep>
struct common_quantity_impl<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, Rep> {
using type = quantity<D, downcast_unit<D, common_ratio<typename U1::ratio, typename U2::ratio>>, Rep>;
};
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2, typename Rep>
requires same_unit_reference<dimension_unit<D1>, dimension_unit<D2>>::value
struct common_quantity_impl<quantity<D1, U1, Rep1>, quantity<D2, U2, Rep2>, Rep> {
using type = quantity<D1, downcast_unit<D1, common_ratio<typename U1::ratio, typename U2::ratio>>, Rep>;
};
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2, typename Rep>
struct common_quantity_impl<quantity<D1, U1, Rep1>, quantity<D2, U2, Rep2>, Rep> {
using ratio1 = ratio_multiply<typename D1::base_units_ratio, typename U1::ratio>;
using ratio2 = ratio_multiply<typename D2::base_units_ratio, typename U2::ratio>;
using type = quantity<D1, downcast_unit<D1, common_ratio<ratio1, ratio2>>, Rep>;
};
} // namespace detail
template<Quantity Q1, Quantity Q2, Scalar Rep = std::common_type_t<typename Q1::rep, typename Q2::rep>>
requires equivalent_dim<typename Q1::dimension, typename Q2::dimension>
using common_quantity = detail::common_quantity_impl<Q1, Q2, Rep>::type;
} // namespace units

View File

@@ -1,53 +0,0 @@
// 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/bits/external/hacks.h>
#include <units/bits/external/numeric_concepts.h>
#include <units/customization_points.h>
#include <units/ratio.h>
namespace units {
namespace detail {
template<typename T, typename U = T>
concept basic_arithmetic = // exposition only
std::magma<std::ranges::plus, T, U> &&
std::magma<std::ranges::minus, T, U> &&
std::magma<std::ranges::times, T, U> &&
std::magma<std::ranges::divided_by, T, U>;
template<typename From, typename To>
concept safe_convertible = // exposition only
std::convertible_to<From, To> &&
(treat_as_floating_point<To> || (!treat_as_floating_point<From>));
template<typename Rep, typename unit_from, typename unit_to>
concept safe_divisible = // exposition only
treat_as_floating_point<Rep> ||
ratio_divide<typename unit_from::ratio, typename unit_to::ratio>::den == 1;
}
} // namespace units

View File

@@ -22,7 +22,6 @@
#pragma once #pragma once
#include <units/unit_of_concept.h>
#include <units/derived_dimension.h> #include <units/derived_dimension.h>
namespace units::detail { namespace units::detail {

View File

@@ -0,0 +1,181 @@
// 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/bits/external/downcasting.h>
#include <units/bits/external/fixed_string.h>
#include <units/bits/external/hacks.h>
#include <units/bits/external/numeric_concepts.h>
#include <units/bits/external/type_traits.h>
#include <units/customization_points.h>
namespace units {
namespace detail {
template<typename T, typename U = T>
concept basic_arithmetic = // exposition only
std::magma<std::ranges::plus, T, U> &&
std::magma<std::ranges::minus, T, U> &&
std::magma<std::ranges::times, T, U> &&
std::magma<std::ranges::divided_by, T, U>;
} // namespace detail
// PrefixType
struct prefix_type;
template<typename T>
concept PrefixType = std::derived_from<T, prefix_type>;
// Prefix
// TODO gcc:92150
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92150
// namespace detail {
// template<typename T>
// inline constexpr bool is_prefix = false;
// template<typename PrefixType, Ratio R, basic_fixed_string Symbol>
// inline constexpr bool is_prefix<prefix<PrefixType, R, Symbol>> = true;
// } // namespace detail
template<typename T>
// concept Prefix = detail::is_prefix<T>;
concept Prefix = true;
namespace detail {
template<typename T>
inline constexpr bool is_ratio = false;
} // namespace detail
template<typename T>
concept Ratio = detail::is_ratio<T>;
// UnitRatio
template<typename R>
concept UnitRatio = Ratio<R> && (R::num * R::den > 0);
// Unit
template<typename U, UnitRatio R>
struct scaled_unit;
template<typename T>
concept Unit = is_derived_from_instantiation<T, scaled_unit>;
// BaseDimension
template<basic_fixed_string Name, Unit U>
struct base_dimension;
namespace detail {
#if __GNUC__ == 9 && __GNUC_MINOR__ < 2
template<typename T>
inline constexpr bool is_base_dimension = true;
#else
template<typename T>
inline constexpr bool is_base_dimension = false;
template<basic_fixed_string Name, typename... Params>
inline constexpr bool is_base_dimension<base_dimension<Name, Params...>> = true;
#endif
} // namespace detail
template<typename T>
concept BaseDimension = detail::is_base_dimension<typename T::base_type_workaround>; // TODO Replace with is_derived_from_instantiation when fixed
// Exponent
namespace detail {
template<typename T>
inline constexpr bool is_exp = false;
} // namespace detail
template<typename T>
concept Exponent = detail::is_exp<T>;
// DerivedDimension
template<typename...>
struct derived_dimension;
template<typename T>
concept DerivedDimension = std::is_empty_v<T> && is_instantiation<downcast_base_t<T>, derived_dimension>;
// Dimension
template<typename T>
concept Dimension = BaseDimension<T> || DerivedDimension<T>;
// UnitOf
namespace detail {
template<Dimension D>
struct dimension_unit_impl;
template<BaseDimension D>
struct dimension_unit_impl<D> {
using type = D::base_unit;
};
template<DerivedDimension D>
struct dimension_unit_impl<D> {
using type = D::coherent_unit;
};
} // namespace detail
template<Dimension D>
using dimension_unit = detail::dimension_unit_impl<D>::type;
template<typename U, typename D>
concept UnitOf =
Unit<U> &&
Dimension<D> &&
std::same_as<typename U::reference, typename dimension_unit<D>::reference>;
// Quantity
namespace detail {
template<typename T>
inline constexpr bool is_quantity = false;
} // namespace detail
template<typename T>
concept Quantity = detail::is_quantity<T>;
// Scalar
template<typename T>
concept Scalar = (!Quantity<T>) && std::regular<T> && std::totally_ordered<T> && detail::basic_arithmetic<T>;
} // namespace units

View File

@@ -22,94 +22,120 @@
#pragma once #pragma once
#include <limits>
#include <type_traits> #include <type_traits>
#include <cmath>
namespace units { namespace units {
// treat_as_floating_point /**
* @brief Specifies if a value of a type should be treated as a floating-point value
*
* This type trait should be specialized for a custom representation type to specify
* that values fo this type should be treated by the library as a floating-point ones
* which will enable implicit conversions between quantities.
*
* @tparam Rep a representation type for which a type trait is defined
*/
template<typename Rep>
inline constexpr bool treat_as_floating_point = std::is_floating_point_v<Rep>;
template<typename Rep> // TODO Conceptify that /**
inline constexpr bool treat_as_floating_point = std::is_floating_point_v<Rep>; * @brief A type trait that defines zero, one, min, and max for a representation type
*
* The zero, one, min, and max member functions in units::quantity forward their work to
* these methods. This type can be specialized if the representation Rep requires a specific
* implementation to return these quantity objects.
*
* @tparam Rep a representation type for which a type trait is defined
*/
template<typename Rep>
struct quantity_values {
static constexpr Rep zero() noexcept { return Rep(0); }
static constexpr Rep one() noexcept { return Rep(1); }
static constexpr Rep min() noexcept { return std::numeric_limits<Rep>::lowest(); }
static constexpr Rep max() noexcept { return std::numeric_limits<Rep>::max(); }
};
// // isnan
// namespace isnan_impl {
// // non-ADL lookup block
// void isnan(); // undefined
// template<typename> // // isnan
// inline constexpr bool has_customization = false; // namespace isnan_impl {
// template<typename T> // // non-ADL lookup block
// requires requires(const T& t) { // void isnan(); // undefined
// { isnan(t) } -> bool;
// }
// inline constexpr bool has_customization<T> = true;
// struct fn { // template<typename>
// template<typename T> // inline constexpr bool has_customization = false;
// constexpr bool operator()(const T&) const
// {
// return false;
// }
// template<typename T> // template<typename T>
// requires treat_as_floating_point<T> // requires requires(const T& t) {
// constexpr bool operator()(const T& value) const // { isnan(t) } -> bool;
// { // }
// return std::isnan(value); // inline constexpr bool has_customization<T> = true;
// }
// template<typename T> // struct fn {
// requires treat_as_floating_point<T> && has_customization<T> // template<typename T>
// constexpr bool operator()(const T& value) const // constexpr bool operator()(const T&) const
// { // {
// return isnan(value); // uses ADL // return false;
// } // }
// };
// }
// inline constexpr isnan_impl::fn isnan{}; // template<typename T>
// requires treat_as_floating_point<T>
// constexpr bool operator()(const T& value) const
// {
// return std::isnan(value);
// }
// // isfinite // template<typename T>
// namespace isfinite_impl { // requires treat_as_floating_point<T> && has_customization<T>
// constexpr bool operator()(const T& value) const
// {
// return isnan(value); // uses ADL
// }
// };
// }
// // non-ADL lookup block // inline constexpr isnan_impl::fn isnan{};
// void isfinite(); // undefined
// template<typename> // // isfinite
// inline constexpr bool has_customization = false; // namespace isfinite_impl {
// template<typename T> // // non-ADL lookup block
// requires requires(const T& t) { // void isfinite(); // undefined
// { isfinite(t) } -> bool;
// }
// inline constexpr bool has_customization<T> = true;
// struct fn { // template<typename>
// template<typename T> // inline constexpr bool has_customization = false;
// constexpr bool operator()(const T&) const
// {
// return true;
// }
// template<typename T> // template<typename T>
// requires treat_as_floating_point<T> // requires requires(const T& t) {
// constexpr bool operator()(const T& value) const // { isfinite(t) } -> bool;
// { // }
// return std::isfinite(value); // inline constexpr bool has_customization<T> = true;
// }
// template<typename T> // struct fn {
// requires treat_as_floating_point<T> && has_customization<T> // template<typename T>
// constexpr bool operator()(const T& value) const // constexpr bool operator()(const T&) const
// { // {
// return isfinite(value); // uses ADL // return true;
// } // }
// };
// }
// inline constexpr isfinite_impl::fn isfinite{}; // template<typename T>
// requires treat_as_floating_point<T>
// constexpr bool operator()(const T& value) const
// {
// return std::isfinite(value);
// }
} // template<typename T>
// requires treat_as_floating_point<T> && has_customization<T>
// constexpr bool operator()(const T& value) const
// {
// return isfinite(value); // uses ADL
// }
// };
// }
// inline constexpr isfinite_impl::fn isfinite{};
} // namespace units

View File

@@ -31,19 +31,6 @@
namespace units { namespace units {
// Exponent
namespace detail {
template<typename T>
inline constexpr bool is_exp = false;
// partial specialization for an exp type provided below
} // namespace detail
template<typename T>
concept Exponent = detail::is_exp<T>;
/** /**
* @brief A derived dimension * @brief A derived dimension
* *
@@ -79,14 +66,6 @@ struct derived_dimension<> : downcast_base<derived_dimension<>> {};
template<Exponent E, Exponent... ERest> template<Exponent E, Exponent... ERest>
struct derived_dimension<E, ERest...> : downcast_base<derived_dimension<E, ERest...>> {}; // TODO rename to 'dimension'? struct derived_dimension<E, ERest...> : downcast_base<derived_dimension<E, ERest...>> {}; // TODO rename to 'dimension'?
// DerivedDimension
template<typename T>
concept DerivedDimension = std::is_empty_v<T> && is_instantiation<downcast_base_t<T>, derived_dimension>;
// Dimension
template<typename T>
concept Dimension = BaseDimension<T> || DerivedDimension<T>;
/** /**
* @brief A power of factor corresponding to the dimension of a quantity * @brief A power of factor corresponding to the dimension of a quantity
* *

View File

@@ -35,9 +35,6 @@ namespace units {
*/ */
struct prefix_type {}; struct prefix_type {};
template<typename T>
concept PrefixType = std::derived_from<T, prefix_type>;
/** /**
* @brief No prefix possible for the unit * @brief No prefix possible for the unit
* *
@@ -72,25 +69,9 @@ struct prefix_base : downcast_base<prefix_base<PT, R>> {
* @tparam R factor to be used to scale a unit * @tparam R factor to be used to scale a unit
*/ */
template<typename Child, PrefixType PT, basic_fixed_string Symbol, Ratio R> template<typename Child, PrefixType PT, basic_fixed_string Symbol, Ratio R>
requires(!std::same_as<PT, no_prefix>) requires (!std::same_as<PT, no_prefix>)
struct prefix : downcast_child<Child, detail::prefix_base<PT, ratio<R::num, R::den>>> { struct prefix : downcast_child<Child, detail::prefix_base<PT, ratio<R::num, R::den>>> {
static constexpr auto symbol = Symbol; static constexpr auto symbol = Symbol;
}; };
// TODO gcc:92150
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92150
// namespace detail {
// template<typename T>
// inline constexpr bool is_prefix = false;
// template<typename PrefixType, Ratio R, basic_fixed_string Symbol>
// inline constexpr bool is_prefix<prefix<PrefixType, R, Symbol>> = true;
// } // namespace detail
template<typename T>
// concept Prefix = detail::is_prefix<T>;
concept Prefix = true;
} // namespace units } // namespace units

View File

@@ -22,255 +22,32 @@
#pragma once #pragma once
#include <units/bits/concepts.h> #include <units/bits/common_quantity.h>
#include <units/bits/dimension_op.h> #include <units/bits/dimension_op.h>
#include <units/bits/unit_text.h> #include <units/bits/unit_text.h>
#include <units/quantity_cast.h>
#if __GNUC__ >= 10 #if __GNUC__ >= 10
#include <compare> #include <compare>
#endif #endif
#include <limits>
#include <ostream> #include <ostream>
namespace units { namespace units {
// Quantity
namespace detail { namespace detail {
template<typename T> template<typename From, typename To>
inline constexpr bool is_quantity = false; concept safe_convertible = // exposition only
std::convertible_to<From, To> &&
(treat_as_floating_point<To> || (!treat_as_floating_point<From>));
// partial specialization below after the first quantity forward declaration template<typename Rep, typename UnitFrom, typename UnitTo>
concept safe_divisible = // exposition only
treat_as_floating_point<Rep> ||
ratio_divide<typename UnitFrom::ratio, typename UnitTo::ratio>::den == 1;
} // namespace detail } // namespace detail
template<typename T>
concept Quantity = detail::is_quantity<T>;
// QuantityOf
template<typename T, typename Dim>
concept QuantityOf = Quantity<T> && Dimension<Dim> && equivalent_dim<typename T::dimension, Dim>;
// Scalar
template<typename T>
concept Scalar = (!Quantity<T>) && std::regular<T> && std::totally_ordered<T> && detail::basic_arithmetic<T>;
template<Dimension D, UnitOf<D> U, Scalar Rep>
class quantity;
namespace detail {
template<typename D, typename U, typename Rep>
inline constexpr bool is_quantity<quantity<D, U, Rep>> = true;
} // namespace detail
// common_quantity
namespace detail {
template<typename Q1, typename Q2, typename Rep>
struct common_quantity_impl;
template<typename D, typename U, typename Rep1, typename Rep2, typename Rep>
struct common_quantity_impl<quantity<D, U, Rep1>, quantity<D, U, Rep2>, Rep> {
using type = quantity<D, U, Rep>;
};
template<typename D, typename U1, typename Rep1, typename U2, typename Rep2, typename Rep>
struct common_quantity_impl<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, Rep> {
using type = quantity<D, downcast_unit<D, common_ratio<typename U1::ratio, typename U2::ratio>>, Rep>;
};
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2, typename Rep>
requires same_unit_reference<dimension_unit<D1>, dimension_unit<D2>>::value
struct common_quantity_impl<quantity<D1, U1, Rep1>, quantity<D2, U2, Rep2>, Rep> {
using type = quantity<D1, downcast_unit<D1, common_ratio<typename U1::ratio, typename U2::ratio>>, Rep>;
};
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2, typename Rep>
struct common_quantity_impl<quantity<D1, U1, Rep1>, quantity<D2, U2, Rep2>, Rep> {
using ratio1 = ratio_multiply<typename D1::base_units_ratio, typename U1::ratio>;
using ratio2 = ratio_multiply<typename D2::base_units_ratio, typename U2::ratio>;
using type = quantity<D1, downcast_unit<D1, common_ratio<ratio1, ratio2>>, Rep>;
};
} // namespace detail
template<Quantity Q1, Quantity Q2, Scalar Rep = std::common_type_t<typename Q1::rep, typename Q2::rep>>
requires equivalent_dim<typename Q1::dimension, typename Q2::dimension>
using common_quantity = detail::common_quantity_impl<Q1, Q2, Rep>::type;
// quantity_cast
namespace detail {
template<typename To, typename CRatio, typename CRep, bool NumIsOne = false, bool DenIsOne = false>
struct quantity_cast_impl {
template<typename Q>
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>(CRatio::num) / static_cast<CRep>(CRatio::den))));
} else {
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio::num) /
static_cast<CRep>(CRatio::den)));
}
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
return To(static_cast<To::rep>(q.count()));
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, false> {
template<Quantity Q>
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()) * (CRep{1} / static_cast<CRep>(CRatio::den))));
} else {
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(CRatio::den)));
}
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio::num)));
}
};
template<Dimension FromD, Unit FromU, Dimension ToD, Unit ToU>
struct cast_ratio;
template<BaseDimension FromD, Unit FromU, BaseDimension ToD, Unit ToU>
struct cast_ratio<FromD, FromU, ToD, ToU> {
using type = ratio_divide<typename FromU::ratio, typename ToU::ratio>;
};
template<DerivedDimension FromD, Unit FromU, DerivedDimension ToD, Unit ToU>
requires same_unit_reference<FromU, ToU>::value
struct cast_ratio<FromD, FromU, ToD, ToU> {
using type = ratio_divide<typename FromU::ratio, typename ToU::ratio>;
};
template<DerivedDimension FromD, Unit FromU, DerivedDimension ToD, Unit ToU>
struct cast_ratio<FromD, FromU, ToD, ToU> {
using from_ratio = ratio_multiply<typename FromD::base_units_ratio, typename FromU::ratio>;
using to_ratio = ratio_multiply<typename ToD::base_units_ratio, typename ToU::ratio>;
using type = ratio_divide<from_ratio, to_ratio>;
};
} // 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::si::time<units::si::second>>(1ms);
*
* @tparam To a target quantity type to cast to
*/
template<Quantity To, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
requires QuantityOf<To, D> &&
detail::basic_arithmetic<std::common_type_t<typename To::rep, Rep, intmax_t>>
{
using c_ratio = detail::cast_ratio<D, U, typename To::dimension, typename To::unit>::type;
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
using ret_unit = downcast_unit<typename To::dimension, typename To::unit::ratio>;
using ret = quantity<typename To::dimension, ret_unit, typename To::rep>;
using cast = detail::quantity_cast_impl<ret, c_ratio, c_rep, c_ratio::num == 1, c_ratio::den == 1>;
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::si::acceleration>(200Gal);
*
* @tparam ToD a dimension type to use for a target quantity
*/
template<Dimension ToD, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
requires equivalent_dim<ToD, D>
{
return quantity_cast<quantity<ToD, U, 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::si::second>(1ms);
*
* @tparam ToU a unit type to use for a target quantity
*/
template<Unit ToU, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
requires UnitOf<ToU, D>
{
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:
*
* auto q1 = units::quantity_cast<int>(1ms);
*
* @tparam ToRep a representation type to use for a target quantity
*/
template<Scalar ToRep, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
requires detail::basic_arithmetic<std::common_type_t<ToRep, Rep, intmax_t>>
{
return quantity_cast<quantity<D, U, ToRep>>(q);
}
/**
* @brief A type trait that defines zero, one, min, and max for a representation type
*
* The zero, one, min, and max member functions in units::quantity forward their work to
* these methods. This type can be specialized if the representation Rep requires a specific
* implementation to return these quantity objects.
*
* @tparam Rep a representation type for which a type trait is defined
*/
template<Scalar Rep>
struct quantity_values {
static constexpr Rep zero() noexcept { return Rep(0); }
static constexpr Rep one() noexcept { return Rep(1); }
static constexpr Rep min() noexcept { return std::numeric_limits<Rep>::lowest(); }
static constexpr Rep max() noexcept { return std::numeric_limits<Rep>::max(); }
};
/** /**
* @brief A quantity * @brief A quantity
@@ -443,46 +220,6 @@ public:
return *this; return *this;
} }
template<typename U2, typename Rep2>
[[nodiscard]] friend constexpr Quantity AUTO operator+(const quantity& lhs, const quantity<D, U2, Rep2>& rhs)
requires detail::basic_arithmetic<Rep, Rep2>
{
using common_rep = decltype(lhs.count() + rhs.count());
using ret = common_quantity<quantity, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() + ret(rhs).count());
}
template<typename U2, typename Rep2>
[[nodiscard]] friend constexpr Quantity AUTO operator-(const quantity& lhs, const quantity<D, U2, Rep2>& rhs)
requires detail::basic_arithmetic<Rep, Rep2>
{
using common_rep = decltype(lhs.count() - rhs.count());
using ret = common_quantity<quantity, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() - ret(rhs).count());
}
template<Scalar Value>
[[nodiscard]] friend constexpr Quantity AUTO operator%(const quantity& q, const Value& v)
requires (!treat_as_floating_point<Rep>) &&
(!treat_as_floating_point<Value>) &&
std::magma<std::ranges::modulus, Rep, Value>
{
using common_rep = decltype(q.count() % v);
using ret = quantity<D, U, common_rep>;
return ret(q.count() % v);
}
template<typename U2, typename Rep2>
[[nodiscard]] friend constexpr Quantity AUTO operator%(const quantity& lhs, const quantity<D, U2, Rep2>& rhs)
requires (!treat_as_floating_point<Rep>) &&
(!treat_as_floating_point<Rep2>) &&
std::magma<std::ranges::modulus, Rep, Rep2>
{
using common_rep = decltype(lhs.count() % rhs.count());
using ret = common_quantity<quantity, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() % ret(rhs).count());
}
#if __GNUC__ >= 10 #if __GNUC__ >= 10
template<typename D2, typename U2, typename Rep2> template<typename D2, typename U2, typename Rep2>
@@ -573,7 +310,23 @@ public:
} }
}; };
// TODO make hidden friends when moved to gcc-10 template<typename D, typename U1, typename Rep1, typename U2, typename Rep2>
[[nodiscard]] constexpr Quantity AUTO operator+(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
requires detail::basic_arithmetic<Rep1, Rep2>
{
using common_rep = decltype(lhs.count() + rhs.count());
using ret = common_quantity<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() + ret(rhs).count());
}
template<typename D, typename U1, typename Rep1, typename U2, typename Rep2>
[[nodiscard]] constexpr Quantity AUTO operator-(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
requires detail::basic_arithmetic<Rep1, Rep2>
{
using common_rep = decltype(lhs.count() - rhs.count());
using ret = common_quantity<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() - ret(rhs).count());
}
template<typename D, typename U, typename Rep, Scalar Value> template<typename D, typename U, typename Rep, Scalar Value>
[[nodiscard]] constexpr Quantity AUTO operator*(const quantity<D, U, Rep>& q, const Value& v) [[nodiscard]] constexpr Quantity AUTO operator*(const quantity<D, U, Rep>& q, const Value& v)
@@ -614,7 +367,7 @@ template<typename D1, typename U1, typename Rep1, typename D2, typename U2, type
return ret(lhs.count() * rhs.count()); return ret(lhs.count() * rhs.count());
} }
template<typename D, Scalar Value, typename U, typename Rep> template<Scalar Value, typename D, typename U, typename Rep>
[[nodiscard]] constexpr Quantity AUTO operator/(const Value& v, const quantity<D, U, Rep>& q) [[nodiscard]] constexpr Quantity AUTO operator/(const Value& v, const quantity<D, U, Rep>& q)
requires std::magma<std::ranges::divided_by, Value, Rep> requires std::magma<std::ranges::divided_by, Value, Rep>
{ {
@@ -666,4 +419,33 @@ template<typename D1, typename U1, typename Rep1, typename D2, typename U2, type
return ret(lhs.count() / rhs.count()); return ret(lhs.count() / rhs.count());
} }
template<typename D, typename U, typename Rep, Scalar Value>
[[nodiscard]] constexpr Quantity AUTO operator%(const quantity<D, U, Rep>& q, const Value& v)
requires (!treat_as_floating_point<Rep>) &&
(!treat_as_floating_point<Value>) &&
std::magma<std::ranges::modulus, Rep, Value>
{
using common_rep = decltype(q.count() % v);
using ret = quantity<D, U, common_rep>;
return ret(q.count() % v);
}
template<typename D, typename U1, typename Rep1, typename U2, typename Rep2>
[[nodiscard]] constexpr Quantity AUTO operator%(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
requires (!treat_as_floating_point<Rep1>) &&
(!treat_as_floating_point<Rep2>) &&
std::magma<std::ranges::modulus, Rep1, Rep2>
{
using common_rep = decltype(lhs.count() % rhs.count());
using ret = common_quantity<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() % ret(rhs).count());
}
namespace detail {
template<typename D, typename U, typename Rep>
inline constexpr bool is_quantity<quantity<D, U, Rep>> = true;
} // namespace detail
} // namespace units } // namespace units

View File

@@ -0,0 +1,188 @@
// 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>
#include <units/bits/dimension_op.h>
namespace units {
// QuantityOf
template<typename T, typename Dim>
concept QuantityOf = Quantity<T> && Dimension<Dim> && equivalent_dim<typename T::dimension, Dim>;
// quantity_cast
namespace detail {
template<typename To, typename CRatio, typename CRep, bool NumIsOne = false, bool DenIsOne = false>
struct quantity_cast_impl {
template<typename Q>
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>(CRatio::num) / static_cast<CRep>(CRatio::den))));
} else {
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio::num) /
static_cast<CRep>(CRatio::den)));
}
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
return To(static_cast<To::rep>(q.count()));
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, false> {
template<Quantity Q>
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()) * (CRep{1} / static_cast<CRep>(CRatio::den))));
} else {
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(CRatio::den)));
}
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, true> {
template<Quantity Q>
static constexpr To cast(const Q& q)
{
return To(static_cast<To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio::num)));
}
};
template<Dimension FromD, Unit FromU, Dimension ToD, Unit ToU>
struct cast_ratio;
template<BaseDimension FromD, Unit FromU, BaseDimension ToD, Unit ToU>
struct cast_ratio<FromD, FromU, ToD, ToU> {
using type = ratio_divide<typename FromU::ratio, typename ToU::ratio>;
};
template<DerivedDimension FromD, Unit FromU, DerivedDimension ToD, Unit ToU>
requires same_unit_reference<FromU, ToU>::value
struct cast_ratio<FromD, FromU, ToD, ToU> {
using type = ratio_divide<typename FromU::ratio, typename ToU::ratio>;
};
template<DerivedDimension FromD, Unit FromU, DerivedDimension ToD, Unit ToU>
struct cast_ratio<FromD, FromU, ToD, ToU> {
using from_ratio = ratio_multiply<typename FromD::base_units_ratio, typename FromU::ratio>;
using to_ratio = ratio_multiply<typename ToD::base_units_ratio, typename ToU::ratio>;
using type = ratio_divide<from_ratio, to_ratio>;
};
} // 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::si::time<units::si::second>>(1ms);
*
* @tparam To a target quantity type to cast to
*/
template<Quantity To, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
requires QuantityOf<To, D> &&
detail::basic_arithmetic<std::common_type_t<typename To::rep, Rep, intmax_t>>
{
using c_ratio = detail::cast_ratio<D, U, typename To::dimension, typename To::unit>::type;
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
using ret_unit = downcast_unit<typename To::dimension, typename To::unit::ratio>;
using ret = quantity<typename To::dimension, ret_unit, typename To::rep>;
using cast = detail::quantity_cast_impl<ret, c_ratio, c_rep, c_ratio::num == 1, c_ratio::den == 1>;
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::si::acceleration>(200Gal);
*
* @tparam ToD a dimension type to use for a target quantity
*/
template<Dimension ToD, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
requires equivalent_dim<ToD, D>
{
return quantity_cast<quantity<ToD, U, 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::si::second>(1ms);
*
* @tparam ToU a unit type to use for a target quantity
*/
template<Unit ToU, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
requires UnitOf<ToU, D>
{
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:
*
* auto q1 = units::quantity_cast<int>(1ms);
*
* @tparam ToRep a representation type to use for a target quantity
*/
template<Scalar ToRep, typename D, typename U, typename Rep>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
requires detail::basic_arithmetic<std::common_type_t<ToRep, Rep, intmax_t>>
{
return quantity_cast<quantity<D, U, ToRep>>(q);
}
} // namespace units

View File

@@ -23,6 +23,7 @@
#pragma once #pragma once
#include <units/bits/external/hacks.h> #include <units/bits/external/hacks.h>
#include <units/concepts.h>
#include <cstdint> #include <cstdint>
#include <numeric> #include <numeric>
#include <type_traits> #include <type_traits>
@@ -51,21 +52,13 @@ struct ratio {
using type = ratio<num, den>; using type = ratio<num, den>;
}; };
// is_ratio
namespace detail { namespace detail {
template<typename T>
inline constexpr bool is_ratio = false;
template<intmax_t Num, intmax_t Den> template<intmax_t Num, intmax_t Den>
inline constexpr bool is_ratio<ratio<Num, Den>> = true; inline constexpr bool is_ratio<ratio<Num, Den>> = true;
} // namespace detail } // namespace detail
template<typename T>
concept Ratio = detail::is_ratio<T>;
// ratio_add // ratio_add
// TODO implement ratio_add // TODO implement ratio_add

View File

@@ -28,7 +28,6 @@
#include <units/bits/external/fixed_string.h> #include <units/bits/external/fixed_string.h>
#include <units/bits/external/text_tools.h> #include <units/bits/external/text_tools.h>
#include <units/bits/external/type_traits.h> #include <units/bits/external/type_traits.h>
#include <units/unit_of_concept.h>
#include <units/derived_dimension.h> #include <units/derived_dimension.h>
#include <units/prefix.h> #include <units/prefix.h>
#include <units/ratio.h> #include <units/ratio.h>

View File

@@ -1,41 +0,0 @@
// 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/bits/external/type_traits.h>
#include <units/ratio.h>
namespace units {
// UnitRatio
template<typename R>
concept UnitRatio = Ratio<R> && (R::num * R::den > 0);
template<typename U, UnitRatio R>
struct scaled_unit;
// Unit
template<typename T>
concept Unit = is_derived_from_instantiation<T, scaled_unit>;
} // namespace units

View File

@@ -1,56 +0,0 @@
// 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/derived_dimension.h>
namespace units {
namespace detail {
template<Dimension D>
struct dimension_unit_impl;
template<BaseDimension D>
struct dimension_unit_impl<D> {
using type = D::base_unit;
};
template<DerivedDimension D>
struct dimension_unit_impl<D> {
using type = D::coherent_unit;
};
} // namespace detail
template<Dimension D>
using dimension_unit = detail::dimension_unit_impl<D>::type;
// UnitOf
template<typename U, typename D>
concept UnitOf =
Unit<U> &&
Dimension<D> &&
std::same_as<typename U::reference, typename dimension_unit<D>::reference>;
} // namespace units