refactor!: replace one_rep with reference<D, U>

This commit is contained in:
Johel Ernesto Guerrero Peña
2021-03-18 22:37:25 +01:00
committed by Mateusz Pusz
parent bf27b256b2
commit 9a520edfe9
65 changed files with 798 additions and 721 deletions
@@ -246,6 +246,22 @@ struct _point_kind_base;
template<typename T>
concept PointKind = kind_impl_<T, detail::_point_kind_base>;
// Reference
namespace detail {
template<typename T>
inline constexpr bool is_reference = false;
} // namespace detail
/**
* @brief A concept matching all references in the library.
*
* Satisfied by all specializations of @c reference.
*/
template<typename T>
concept Reference = detail::is_reference<T>;
// Quantity, QuantityPoint, QuantityKind, QuantityPointKind
namespace detail {
+4
View File
@@ -42,13 +42,17 @@
UNITS_PRAGMA(GCC diagnostic ignored "-Wunknown-pragmas") \
UNITS_PRAGMA(GCC diagnostic ignored "-Wunknown-warning-option") \
UNITS_PRAGMA(GCC diagnostic ignored X)
#define UNITS_DIAGNOSTIC_IGNORE_MISSING_BRACES UNITS_DIAGNOSTIC_IGNORE("-Wmissing-braces")
#define UNITS_DIAGNOSTIC_IGNORE_NON_TEMPLATE_FRIEND UNITS_DIAGNOSTIC_IGNORE("-Wnon-template-friend")
#define UNITS_DIAGNOSTIC_IGNORE_SHADOW UNITS_DIAGNOSTIC_IGNORE("-Wshadow")
#else
#define UNITS_DIAGNOSTIC_PUSH UNITS_PRAGMA(warning(push))
#define UNITS_DIAGNOSTIC_POP UNITS_PRAGMA(warning(pop))
#define UNITS_DIAGNOSTIC_IGNORE_PRAGMAS UNITS_PRAGMA(warning(disable : 4068))
#define UNITS_DIAGNOSTIC_IGNORE(X) UNITS_DIAGNOSTIC_IGNORE_PRAGMAS UNITS_PRAGMA(warning(disable : X))
#define UNITS_DIAGNOSTIC_IGNORE_MISSING_BRACES
#define UNITS_DIAGNOSTIC_IGNORE_NON_TEMPLATE_FRIEND
#define UNITS_DIAGNOSTIC_IGNORE_SHADOW
#endif
#if UNITS_COMP_CLANG
+6 -1
View File
@@ -44,7 +44,12 @@ inline namespace literals {
constexpr auto operator"" _q_rad(unsigned long long l) { gsl_ExpectsAudit(std::in_range<std::int64_t>(l)); return angle<radian, std::int64_t>(static_cast<std::int64_t>(l)); }
constexpr auto operator"" _q_rad(long double l) { return angle<radian, long double>(l); }
} // namespace literals
inline namespace unit_constants {
inline constexpr auto rad = reference<dim_angle<>, radian>{};
} // namespace unit_constants
} // namespace units
-124
View File
@@ -1,124 +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/basic_concepts.h>
#include <units/customization_points.h>
#include <type_traits>
namespace units {
struct invalid_one_rep {};
/**
* @brief A representation type to be used for unit constants
*
* This representation type is intended to be used in the unit constants definition:
*
* @code{.cpp}
* namespace unit_constants {
*
* inline constexpr auto m = length<metre, one_rep>{};
* inline constexpr auto km = length<kilometre, one_rep>{};
*
* }
* @endcode
*
* Unit constants simplify quantity creation:
*
* @code{.cpp}
* using namespace units::isq::si::unit_constants;
*
* auto d = 123 * m;
* auto v = 70 * km / h;
* @endcode
*
* Also, it is allowed to define custom unit constants from existing ones:
*
* @code{.cpp}
* constexpr auto Nm = N * m;
* constexpr auto mph = mi / h;
* @endcode
*
* `km * 3` or `s / 4` syntax is not allowed for quantity creation.
*/
struct one_rep {
[[nodiscard]] friend constexpr one_rep operator*(one_rep, one_rep) { return {}; }
[[nodiscard]] friend constexpr one_rep operator/(one_rep, one_rep) { return {}; }
template<QuantityValue Rep>
[[nodiscard]] friend constexpr Rep operator*(const Rep& lhs, one_rep)
{
return lhs;
}
template<QuantityValue Rep>
[[nodiscard]] friend constexpr Rep operator/(const Rep& lhs, one_rep)
{
return lhs;
}
template<QuantityValue Rep>
[[nodiscard]] friend constexpr Rep operator*(one_rep, const Rep&) = delete;
template<QuantityValue Rep>
[[nodiscard]] friend constexpr Rep operator/(one_rep, const Rep&) = delete;
template<typename Rep>
requires requires { quantity_values<Rep>::one(); } &&
(!Quantity<Rep> && !QuantityLike<Rep> && !wrapped_quantity_<Rep>) // TODO: Replace with `QuantityValue`
[[nodiscard]] constexpr operator Rep() const noexcept // when Clang catches up.
{
return quantity_values<Rep>::one();
}
[[nodiscard]] bool operator==(const one_rep&) const = default;
[[nodiscard]] auto operator<=>(const one_rep&) const = default;
[[nodiscard]] constexpr bool operator==(const invalid_one_rep&) const { return false; }
};
template<>
struct quantity_values<one_rep> {
static constexpr invalid_one_rep zero() noexcept { return invalid_one_rep{}; }
static constexpr one_rep one() noexcept { return one_rep{}; }
static constexpr one_rep min() noexcept { return one(); }
static constexpr one_rep max() noexcept { return one(); }
};
} // namespace units
namespace std {
template<>
struct common_type<units::one_rep, units::one_rep> {
using type = units::one_rep;
};
template<units::QuantityValue Rep>
struct common_type<units::one_rep, Rep> {
using type = Rep;
};
template<units::QuantityValue Rep>
struct common_type<Rep, units::one_rep> {
using type = Rep;
};
} // namespace std
+44 -16
View File
@@ -24,15 +24,26 @@
#pragma once
#include <units/bits/common_quantity.h>
#include <units/bits/dimension_op.h>
#include <units/bits/pow.h>
#include <units/bits/unit_text.h>
#include <units/generic/dimensionless.h>
#include <units/quantity_cast.h>
#include <units/reference.h>
#include <compare>
#include <utility>
namespace units {
namespace detail {
template<Reference R> // Replace with `v * R{}` pending https://github.com/BobSteagall/wg21/issues/58.
inline constexpr auto make_quantity = [](auto v) {
using Rep = decltype(v);
return quantity<typename R::dimension, typename R::unit, Rep>(std::move(v));
};
} // namespace detail
template<typename T>
concept floating_point_ = // exposition only
(Quantity<T> && treat_as_floating_point<typename T::rep>) ||
@@ -98,6 +109,7 @@ class quantity {
Rep value_;
public:
// member types
using reference = units::reference<D, U>;
using dimension = D;
using unit = U;
using rep = Rep;
@@ -392,25 +404,41 @@ template<Quantity Q1, QuantityEquivalentTo<Q1> Q2>
return ret(ret(lhs).count() - ret(rhs).count());
}
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
requires quantity_value_for_<std::multiplies<>, Rep1, Rep2>
[[nodiscard]] constexpr Quantity auto operator*(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
template<typename QuantityOrQuantityValue, typename D, typename U>
requires Quantity<QuantityOrQuantityValue> || QuantityValue<QuantityOrQuantityValue>
[[nodiscard]] constexpr Quantity auto operator*(const QuantityOrQuantityValue& lhs, reference<D, U>)
{
using dim = dimension_multiply<D1, D2>;
using unit = downcast_unit<dim, (U1::ratio / dimension_unit<D1>::ratio) * (U2::ratio / dimension_unit<D2>::ratio) * dimension_unit<dim>::ratio>;
using ret = quantity<dim, unit, std::invoke_result_t<std::multiplies<>, Rep1, Rep2>>;
return ret(lhs.count() * rhs.count());
if constexpr (Quantity<QuantityOrQuantityValue>)
return lhs * quantity<D, U, typename QuantityOrQuantityValue::rep>::one();
else
return quantity<D, U, QuantityOrQuantityValue>(lhs);
}
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
requires quantity_value_for_<std::divides<>, Rep1, Rep2>
[[nodiscard]] constexpr Quantity auto operator/(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
template<Quantity Q1, Quantity Q2>
requires quantity_value_for_<std::multiplies<>, typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr Quantity auto operator*(const Q1& lhs, const Q2& rhs)
{
gsl_ExpectsAudit(rhs.count() != quantity_values<Rep2>::zero());
using dim = dimension_divide<D1, D2>;
using unit = downcast_unit<dim, (U1::ratio / dimension_unit<D1>::ratio) / (U2::ratio / dimension_unit<D2>::ratio) * dimension_unit<dim>::ratio>;
using ret = quantity<dim, unit, std::invoke_result_t<std::divides<>, Rep1, Rep2>>;
return ret(lhs.count() / rhs.count());
return detail::make_quantity<reference_multiply<typename Q1::reference, typename Q2::reference>>(
lhs.count() * rhs.count());
}
template<typename QuantityOrQuantityValue, typename D, typename U>
requires Quantity<QuantityOrQuantityValue> || QuantityValue<QuantityOrQuantityValue>
[[nodiscard]] constexpr Quantity auto operator/(const QuantityOrQuantityValue& lhs, reference<D, U>)
{
if constexpr (Quantity<QuantityOrQuantityValue>)
return lhs / quantity<D, U, typename QuantityOrQuantityValue::rep>::one();
else
return lhs / quantity<D, U, QuantityOrQuantityValue>::one();
}
template<Quantity Q1, Quantity Q2>
requires quantity_value_for_<std::divides<>, typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr Quantity auto operator/(const Q1& lhs, const Q2& rhs)
{
gsl_ExpectsAudit(rhs.count() != quantity_values<typename Q2::rep>::zero());
return detail::make_quantity<reference_divide<typename Q1::reference, typename Q2::reference>>(
lhs.count() / rhs.count());
}
template<typename D1, typename U1, typename Rep1, typename U2, typename Rep2>
+8 -5
View File
@@ -300,8 +300,9 @@ template<QuantityKind QK1, QuantityKindEquivalentTo<QK1> QK2>
return detail::make_quantity_kind<QK1>(lhs.common() - rhs.common());
}
template<QuantityKind QK, Quantity Q>
[[nodiscard]] constexpr QuantityKind auto operator*(const QK& lhs, const Q& rhs)
template<QuantityKind QK, typename QuantityOrReference>
requires Quantity<QuantityOrReference> || Reference<QuantityOrReference>
[[nodiscard]] constexpr QuantityKind auto operator*(const QK& lhs, const QuantityOrReference& rhs)
requires requires { lhs.common() * rhs; }
{
return detail::downcasted_kind<QK>(lhs.common() * rhs);
@@ -321,11 +322,13 @@ template<QuantityKind QK1, QuantityKindRelatedTo<QK1> QK2>
return detail::downcasted_kind<QK1>(lhs.common() * rhs.common());
}
template<QuantityKind QK, Quantity Q>
[[nodiscard]] constexpr QuantityKind auto operator/(const QK& lhs, const Q& rhs)
template<QuantityKind QK, typename QuantityOrReference>
requires Quantity<QuantityOrReference> || Reference<QuantityOrReference>
[[nodiscard]] constexpr QuantityKind auto operator/(const QK& lhs, const QuantityOrReference& rhs)
requires requires { lhs.common() / rhs; }
{
gsl_ExpectsAudit(rhs.count() != quantity_values<typename Q::rep>::zero());
if constexpr (Quantity<QuantityOrReference>)
gsl_ExpectsAudit(rhs.count() != quantity_values<typename QuantityOrReference::rep>::zero());
return detail::downcasted_kind<QK>(lhs.common() / rhs);
}
+108
View File
@@ -0,0 +1,108 @@
// 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/basic_concepts.h>
#include <units/bits/dimension_op.h>
namespace units {
/**
* @brief The type for unit constants
*
* This type is intended to be used in the unit constants definition:
*
* @code{.cpp}
* namespace unit_constants {
*
* inline constexpr auto m = reference<dim_length, metre>{};
* inline constexpr auto km = reference<dim_length, kilometre>{};
*
* }
* @endcode
*
* Unit constants simplify quantity creation:
*
* @code{.cpp}
* using namespace units::isq::si::unit_constants;
*
* auto d = 123 * m;
* auto v = 70 * km / h;
* @endcode
*
* Also, it is allowed to define custom unit constants from existing ones:
*
* @code{.cpp}
* constexpr auto Nm = N * m;
* constexpr auto mph = mi / h;
* @endcode
*
* `km * 3` or `s / 4` syntax is not allowed for quantity creation.
*/
template<Dimension D, UnitOf<D> U>
struct reference {
using dimension = D;
using unit = U;
};
namespace detail {
template<typename D, typename D1, typename U1, typename D2, typename U2>
using reference_multiply_impl = reference<D, downcast_unit<D,
(U1::ratio / dimension_unit<D1>::ratio) * (U2::ratio / dimension_unit<D2>::ratio) * dimension_unit<D>::ratio>>;
} // namespace detail
template<Reference R1, Reference R2>
using reference_multiply = detail::reference_multiply_impl<
dimension_multiply<typename R1::dimension, typename R2::dimension>,
typename R1::dimension, typename R1::unit, typename R2::dimension, typename R2::unit>;
template<Reference R1, Reference R2>
[[nodiscard]] constexpr reference_multiply<R1, R2> operator*(R1, R2) { return {}; }
namespace detail {
template<typename D, typename D1, typename U1, typename D2, typename U2>
using reference_divide_impl = reference<D, downcast_unit<D,
(U1::ratio / dimension_unit<D1>::ratio) / (U2::ratio / dimension_unit<D2>::ratio) * dimension_unit<D>::ratio>>;
} // namespace detail
template<Reference R1, Reference R2>
using reference_divide = detail::reference_divide_impl<
dimension_divide<typename R1::dimension, typename R2::dimension>,
typename R1::dimension, typename R1::unit, typename R2::dimension, typename R2::unit>;
template<Reference R1, Reference R2>
[[nodiscard]] constexpr reference_divide<R1, R2> operator/(R1, R2) { return {}; }
// type traits
namespace detail {
template<typename D, typename U>
inline constexpr bool is_reference<reference<D, U>> = true;
} // namespace detail
} // namespace units