Merge branch 'downcasting_2.0'

This commit is contained in:
Mateusz Pusz
2020-09-09 20:31:03 +02:00
22 changed files with 300 additions and 145 deletions

View File

@@ -48,6 +48,12 @@ class UnitsConan(ConanFile):
"fmt/7.0.3",
"ms-gsl/3.1.0"
)
options = {
"downcast": ["off", "on", "auto"]
}
default_options = {
"downcast": "auto"
}
exports = ["LICENSE.md"]
exports_sources = ["docs/*", "src/*", "test/*", "cmake/*", "example/*","CMakeLists.txt"]
# scm = {
@@ -77,6 +83,13 @@ class UnitsConan(ConanFile):
def _configure_cmake(self, folder="src"):
cmake = CMake(self)
if self.options.downcast_dispatch_mode == "off":
cmake.definitions["UNITS_DOWNCAST"] = 0
elif self.options.downcast_dispatch_mode == "on":
cmake.definitions["UNITS_DOWNCAST"] = 1
elif self.options.downcast_dispatch_mode == "auto":
cmake.definitions["UNITS_DOWNCAST"] = 2
if self._run_tests:
# developer's mode (unit tests, examples, documentation, restrictive compilation warnings, ...)
cmake.configure()

View File

@@ -23,12 +23,7 @@ a few additional member types and functions::
};
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
requires detail::basic_arithmetic<Rep1, Rep2> && equivalent_dim<D1, dim_invert<D2>>
[[nodiscard]] constexpr ScalableNumber auto operator*(const quantity<D1, U1, Rep1>& lhs,
const quantity<D2, U2, Rep2>& rhs);
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
requires detail::basic_arithmetic<Rep1, Rep2> && (!equivalent_dim<D1, dim_invert<D2>>)
requires detail::basic_arithmetic<Rep1, Rep2> && (!equivalent<D1, dim_invert<D2>>)
[[nodiscard]] constexpr Quantity auto operator*(const quantity<D1, U1, Rep1>& lhs,
const quantity<D2, U2, Rep2>& rhs);
@@ -38,17 +33,12 @@ a few additional member types and functions::
const quantity<D, U, Rep>& q);
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
requires detail::basic_arithmetic<Rep1, Rep2> && equivalent_dim<D1, D2>
[[nodiscard]] constexpr ScalableNumber auto operator/(const quantity<D1, U1, Rep1>& lhs,
const quantity<D2, U2, Rep2>& rhs);
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
requires detail::basic_arithmetic<Rep1, Rep2> && (!equivalent_dim<D1, D2>)
requires detail::basic_arithmetic<Rep1, Rep2> && (!equivalent<D1, D2>)
[[nodiscard]] constexpr Quantity auto operator/(const quantity<D1, U1, Rep1>& lhs,
const quantity<D2, U2, Rep2>& rhs);
Additional functions provide the support for operations that result in a
different dimension type than those of their arguments. ``equivalent_dim``
different dimension type than those of their arguments. ``equivalent``
constraint requires two dimensions to be either the same or have convertible
units of base dimension (with the same reference unit).

View File

@@ -28,7 +28,7 @@
namespace {
template<units::Quantity Target, units::Quantity Source>
requires units::equivalent_dim<typename Source::dimension, typename Target::dimension>
requires units::equivalent<typename Source::dimension, typename Target::dimension>
inline constexpr std::common_type_t<typename Target::rep, typename Source::rep> conversion_factor(Target, Source)
{
// get quantities looking like inputs but with Q::rep that doesn't have narrowing conversion

View File

@@ -27,7 +27,7 @@
namespace {
template<units::Quantity Target, units::Quantity Source>
requires units::equivalent_dim<typename Source::dimension, typename Target::dimension>
requires units::equivalent<typename Source::dimension, typename Target::dimension>
inline constexpr std::common_type_t<typename Target::rep, typename Source::rep> conversion_factor(Target, Source)
{
// get quantities looking like inputs but with Q::rep that doesn't have narrowing conversion

View File

@@ -27,6 +27,9 @@ project(mp-units
LANGUAGES CXX
)
set(DOWNCAST_MODE AUTO CACHE STRING "Select downcasting mode")
set_property(CACHE DOWNCAST_MODE PROPERTY STRINGS AUTO ON OFF)
# set path to custom cmake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
@@ -63,6 +66,7 @@ target_include_directories(mp-units
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_link_libraries(mp-units
INTERFACE
@@ -80,6 +84,17 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
)
endif()
if(DOWNCAST_MODE STREQUAL "AUTO")
message(STATUS "Configuring DOWNCAST_MODE=AUTOMATIC")
target_compile_definitions(mp-units INTERFACE DOWNCAST_MODE=2)
elseif(DOWNCAST_MODE)
message(STATUS "Configuring DOWNCAST_MODE=ON")
target_compile_definitions(mp-units INTERFACE DOWNCAST_MODE=1)
else()
message(STATUS "Configuring DOWNCAST_MODE=OFF")
target_compile_definitions(mp-units INTERFACE DOWNCAST_MODE=0)
endif()
add_library(mp-units::mp-units ALIAS mp-units)
# installation info

View File

@@ -23,6 +23,8 @@
#pragma once
#include <units/bits/dimension_op.h>
#include <units/bits/equivalent.h>
#include <units/quantity_cast.h>
namespace units {
@@ -55,9 +57,12 @@ struct common_quantity_impl<quantity<D1, U1, Rep1>, quantity<D2, U2, Rep2>, 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 dimension = conditional<is_specialization_of<D1, unknown_dimension>, D2, D1>;
static constexpr ratio r1 = D1::base_units_ratio * U1::ratio;
static constexpr ratio r2 = D2::base_units_ratio * U2::ratio;
using type = quantity<D1, downcast_unit<D1, common_ratio(r1, r2)>, Rep>;
static constexpr ratio cr = common_ratio(r1, r2);
using unit = downcast_unit<dimension, cr / dimension::base_units_ratio>;
using type = quantity<dimension, unit, Rep>;
};
template<typename D, typename U, typename Rep>
@@ -66,7 +71,7 @@ quantity_point<D, U, Rep> common_quantity_point_impl(quantity<D, U, Rep>);
} // namespace detail
template<Quantity Q1, Quantity Q2, ScalableNumber Rep = std::common_type_t<typename Q1::rep, typename Q2::rep>>
requires equivalent_dim<typename Q1::dimension, typename Q2::dimension>
requires equivalent<typename Q1::dimension, typename Q2::dimension>
using common_quantity = TYPENAME detail::common_quantity_impl<Q1, Q2, Rep>::type;
template<QuantityPoint QP1, QuantityPoint QP2>
@@ -79,7 +84,7 @@ using common_quantity_point = decltype(
namespace std {
template<units::Quantity Q1, units::Quantity Q2>
requires units::equivalent_dim<typename Q1::dimension, typename Q2::dimension>
requires units::equivalent<typename Q1::dimension, typename Q2::dimension>
struct common_type<Q1, Q2> {
using type = units::common_quantity<Q1, Q2>;
};

View File

@@ -27,40 +27,6 @@
namespace units {
// equivalent_dim
namespace detail {
template<BaseDimension D1, BaseDimension D2>
using equivalent_base_dim = std::conjunction<std::bool_constant<D1::symbol == D2::symbol>,
same_unit_reference<typename D1::base_unit, typename D2::base_unit>>;
template<Dimension D1, Dimension D2>
struct equivalent_dim_impl : std::false_type {};
template<BaseDimension D1, BaseDimension D2>
struct equivalent_dim_impl<D1, D2> : std::disjunction<is_same<D1, D2>, equivalent_base_dim<D1, D2>> {};
template<Exponent E1, Exponent E2>
struct equivalent_exp : std::false_type {};
template<BaseDimension Dim1, std::intmax_t Num, std::intmax_t Den, BaseDimension Dim2>
struct equivalent_exp<exponent<Dim1, Num, Den>, exponent<Dim2, Num, Den>> : equivalent_dim_impl<Dim1, Dim2> {};
template<DerivedDimension D1, DerivedDimension D2>
struct equivalent_derived_dim : std::false_type {};
template<typename... Es1, typename... Es2>
requires (sizeof...(Es1) == sizeof...(Es2))
struct equivalent_derived_dim<derived_dimension_base<Es1...>, derived_dimension_base<Es2...>> : std::conjunction<equivalent_exp<Es1, Es2>...> {};
template<DerivedDimension D1, DerivedDimension D2>
struct equivalent_dim_impl<D1, D2> : std::disjunction<is_same<D1, D2>, equivalent_derived_dim<downcast_base_t<D1>, downcast_base_t<D2>>> {};
} // namespace detail
template<Dimension D1, Dimension D2>
inline constexpr bool equivalent_dim = detail::equivalent_dim_impl<D1, D2>::value;
/**
* @brief Unknown dimension
*

View File

@@ -0,0 +1,98 @@
// 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/unit.h>
namespace units {
namespace detail {
template<typename T, typename U>
struct equivalent_impl : std::false_type {
};
// units
template<Unit U1, Unit U2>
struct equivalent_impl<U1, U2> : std::disjunction<std::is_same<U1, U2>, std::is_base_of<U1, U2>, std::is_base_of<U2, U1>> {};
// dimensions
template<BaseDimension D1, BaseDimension D2>
struct equivalent_base_dim :
std::conjunction<std::bool_constant<D1::symbol == D2::symbol>,
same_unit_reference<typename D1::base_unit, typename D2::base_unit>> {
};
template<BaseDimension D1, BaseDimension D2>
struct equivalent_impl<D1, D2> : std::disjunction<std::is_same<D1, D2>, equivalent_base_dim<D1, D2>> {
};
template<Exponent E1, Exponent E2>
struct equivalent_exp : std::false_type {
};
template<BaseDimension Dim1, std::intmax_t Num, std::intmax_t Den, BaseDimension Dim2>
struct equivalent_exp<exponent<Dim1, Num, Den>, exponent<Dim2, Num, Den>> : equivalent_impl<Dim1, Dim2> {
};
template<DerivedDimension D1, DerivedDimension D2>
struct equivalent_derived_dim : std::false_type {
};
template<typename... Es1, typename... Es2>
requires(sizeof...(Es1) == sizeof...(Es2))
struct equivalent_derived_dim<derived_dimension_base<Es1...>, derived_dimension_base<Es2...>> :
std::conjunction<equivalent_exp<Es1, Es2>...> {
};
template<DerivedDimension D1, DerivedDimension D2>
struct equivalent_impl<D1, D2> :
std::disjunction<std::is_same<D1, D2>, std::is_base_of<D1, D2>, std::is_base_of<D2, D1>,
equivalent_derived_dim<downcast_base_t<D1>, downcast_base_t<D2>>> {
};
// additionally accounts for unknown dimensions
template<Unit U1, Dimension D1, Unit U2, Dimension D2>
struct equivalent_unit : std::disjunction<equivalent_impl<U1, U2>,
std::bool_constant<U1::ratio / dimension_unit<D1>::ratio == U2::ratio / dimension_unit<D2>::ratio>> {};
// quantities and quantity points
template<typename Q1, typename Q2>
requires (Quantity<Q1> && Quantity<Q2>) || (QuantityPoint<Q1> && QuantityPoint<Q2>)
struct equivalent_impl<Q1, Q2> : std::disjunction<std::is_same<Q1, Q2>,
std::conjunction<equivalent_impl<typename Q1::dimension, typename Q2::dimension>,
equivalent_unit<typename Q1::unit, typename Q1::dimension,
typename Q2::unit, typename Q2::dimension>>> {};
} // namespace detail
template<typename T, typename U>
inline constexpr bool equivalent = detail::equivalent_impl<T, U>::value;
} // namespace units

View File

@@ -25,44 +25,75 @@
#include <units/bits/external/hacks.h>
#include <type_traits>
#ifdef DOWNCAST_MODE
#if DOWNCAST_MODE < 0 || DOWNCAST_MODE > 2
#error "Invalid DOWNCAST_MODE value"
#endif
#else
#define DOWNCAST_MODE 2
#endif
namespace units {
template<typename BaseType>
struct downcast_base {
using downcast_base_type = BaseType;
friend auto downcast_guide(downcast_base);
friend auto downcast_poison_pill(downcast_base);
};
template<typename T>
concept Downcastable =
requires {
typename T::downcast_base_type;
} &&
requires { typename T::downcast_base_type; } &&
std::derived_from<T, downcast_base<typename T::downcast_base_type>>;
template<typename Target, Downcastable T>
struct downcast_child : T {
friend auto downcast_guide(typename downcast_child::downcast_base /* base */) { return Target(); }
};
namespace detail {
template<typename T>
concept has_downcast =
concept has_downcast_guide =
requires {
downcast_guide(std::declval<downcast_base<T>>());
};
template<typename T>
concept has_downcast_poison_pill =
requires {
downcast_poison_pill(std::declval<downcast_base<T>>());
};
template<typename Target, Downcastable T>
struct downcast_child : T {
friend auto downcast_guide(typename T::downcast_base)
{ return Target(); }
};
template<Downcastable T>
struct downcast_poison : T {
friend auto downcast_poison_pill(typename T::downcast_base)
{ return true; }
};
enum class downcast_mode {
off = 0, // no downcasting at all
on = 1, // downcasting always forced -> compile-time errors in case of duplicated definitions
automatic = 2 // downcasting automatically enabled if no collisions are present
};
template<typename Target, Downcastable T, downcast_mode mode = static_cast<downcast_mode>(DOWNCAST_MODE)>
struct downcast_dispatch : std::conditional_t<mode == downcast_mode::off, T,
std::conditional_t<mode == downcast_mode::automatic && has_downcast_guide<T>,
downcast_poison<T>, downcast_child<Target, T>>> {};
namespace detail {
template<typename T>
constexpr auto downcast_impl()
{
if constexpr (has_downcast<T>)
if constexpr(has_downcast_guide<T> && !has_downcast_poison_pill<T>)
return decltype(downcast_guide(std::declval<downcast_base<T>>()))();
else
return T();
}
} // namespace detail
}
template<Downcastable T>
using downcast = decltype(detail::downcast_impl<T>());

View File

@@ -78,7 +78,7 @@ using make_dimension = TYPENAME to_derived_dimension_base<typename dim_consolida
* @tparam Es the list of exponents of ingredient dimensions
*/
template<typename Child, Unit U, Exponent... Es>
struct derived_dimension : downcast_child<Child, typename detail::make_dimension<Es...>> {
struct derived_dimension : downcast_dispatch<Child, typename detail::make_dimension<Es...>> {
using recipe = exponent_list<Es...>;
using coherent_unit = U;
static constexpr ratio base_units_ratio = detail::base_units_ratio(typename derived_dimension::exponents());

View File

@@ -71,7 +71,7 @@ struct prefix_base : downcast_base<prefix_base<PF, R>> {
*/
template<typename Child, PrefixFamily PF, basic_symbol_text Symbol, ratio R>
requires (!std::same_as<PF, no_prefix>)
struct prefix : downcast_child<Child, detail::prefix_base<PF, R>> {
struct prefix : downcast_dispatch<Child, detail::prefix_base<PF, R>, downcast_mode::on> {
static constexpr auto symbol = Symbol;
};

View File

@@ -76,7 +76,7 @@ public:
constexpr explicit(!(std::is_same_v<dimension, dim_one> && std::is_same_v<unit, one>)) quantity(const Value& v) : value_{static_cast<rep>(v)} {}
template<Quantity Q2>
requires equivalent_dim<D, typename Q2::dimension> &&
requires equivalent<D, typename Q2::dimension> &&
detail::safe_convertible<typename Q2::rep, rep> &&
detail::safe_divisible<rep, Q2, quantity>
constexpr quantity(const Q2& q) : value_{quantity_cast<quantity>(q).count()} {}
@@ -314,7 +314,7 @@ public:
}
template<typename D2, typename U2, typename Rep2>
requires equivalent_dim<D, D2> &&
requires equivalent<D, D2> &&
std::three_way_comparable_with<Rep, Rep2>
[[nodiscard]] friend constexpr auto operator<=>(const quantity& lhs, const quantity<D2, U2, Rep2>& rhs)
{
@@ -323,7 +323,7 @@ public:
}
template<typename D2, typename U2, typename Rep2>
requires equivalent_dim<D, D2> &&
requires equivalent<D, D2> &&
std::equality_comparable_with<Rep, Rep2>
[[nodiscard]] friend constexpr bool operator==(const quantity& lhs, const quantity<D2, U2, Rep2>& rhs)
{

View File

@@ -59,7 +59,7 @@ constexpr auto quantity_ratio(const quantity<D, U, Rep>&)
// QuantityOf
template<typename T, typename Dim>
concept QuantityOf = Quantity<T> && Dimension<Dim> && equivalent_dim<typename T::dimension, Dim>;
concept QuantityOf = Quantity<T> && Dimension<Dim> && equivalent<typename T::dimension, Dim>;
// quantity_cast
namespace detail {
@@ -337,7 +337,7 @@ template<Quantity To, typename D, typename U, typename Rep>
* @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>
requires equivalent<ToD, D>
[[nodiscard]] constexpr auto quantity_cast(const quantity<D, U, Rep>& q)
{
return quantity_cast<quantity<ToD, dimension_unit<ToD>, Rep>>(q);

View File

@@ -74,7 +74,7 @@ struct same_unit_reference : is_same<typename U1::reference, typename U2::refere
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
*/
template<typename Child>
struct unit : downcast_child<Child, scaled_unit<ratio(1), Child>> {
struct unit : downcast_dispatch<Child, scaled_unit<ratio(1), Child>> {
static constexpr bool is_named = false;
using prefix_family = no_prefix;
};
@@ -92,7 +92,7 @@ struct unit : downcast_child<Child, scaled_unit<ratio(1), Child>> {
* @tparam PF no_prefix or a type of prefix family
*/
template<typename Child, basic_symbol_text Symbol, PrefixFamily PF>
struct named_unit : downcast_child<Child, scaled_unit<ratio(1), Child>> {
struct named_unit : downcast_dispatch<Child, scaled_unit<ratio(1), Child>> {
static constexpr bool is_named = true;
static constexpr auto symbol = Symbol;
using prefix_family = PF;
@@ -114,7 +114,7 @@ struct named_unit : downcast_child<Child, scaled_unit<ratio(1), Child>> {
*/
template<typename Child, basic_symbol_text Symbol, PrefixFamily PF, ratio R, Unit U>
requires UnitRatio<R>
struct named_scaled_unit : downcast_child<Child, scaled_unit<R * U::ratio, typename U::reference>> {
struct named_scaled_unit : downcast_dispatch<Child, scaled_unit<R * U::ratio, typename U::reference>> {
static constexpr bool is_named = true;
static constexpr auto symbol = Symbol;
using prefix_family = PF;
@@ -133,7 +133,7 @@ struct named_scaled_unit : downcast_child<Child, scaled_unit<R * U::ratio, typen
*/
template<typename Child, Prefix P, Unit U>
requires U::is_named && std::same_as<typename P::prefix_family, typename U::prefix_family>
struct prefixed_unit : downcast_child<Child, scaled_unit<P::ratio * U::ratio, typename U::reference>> {
struct prefixed_unit : downcast_dispatch<Child, scaled_unit<P::ratio * U::ratio, typename U::reference>> {
static constexpr bool is_named = true;
static constexpr auto symbol = P::symbol + U::symbol;
using prefix_family = no_prefix;
@@ -155,7 +155,7 @@ struct prefixed_unit : downcast_child<Child, scaled_unit<P::ratio * U::ratio, ty
template<typename Child, DerivedDimension Dim, Unit U, Unit... URest>
requires detail::same_scaled_units<typename Dim::recipe, U, URest...> &&
(U::is_named && (URest::is_named && ... && true))
struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> {
struct deduced_unit : downcast_dispatch<Child, detail::deduced_unit<Dim, U, URest...>> {
static constexpr bool is_named = false;
static constexpr auto symbol = detail::deduced_symbol_text<Dim, U, URest...>();
using prefix_family = no_prefix;
@@ -179,7 +179,7 @@ template<typename Child, DerivedDimension Dim, Unit U, Unit... URest>
requires detail::same_scaled_units<typename Dim::recipe, U, URest...> &&
(U::is_named && (URest::is_named && ... && true))
// TODO - 'noble' is placeholder to sort of mean can pass its name on to other deduced units
struct noble_deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> {
struct noble_deduced_unit : downcast_dispatch<Child, detail::deduced_unit<Dim, U, URest...>> {
static constexpr bool is_named = true;
static constexpr auto symbol = detail::deduced_symbol_text<Dim, U, URest...>();
using prefix_family = no_prefix;
@@ -203,7 +203,7 @@ struct noble_deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, U
*/
template<typename Child, DerivedDimension Dim, basic_symbol_text Symbol, PrefixFamily PF, Unit U, Unit... URest>
requires detail::same_scaled_units<typename Dim::recipe, U, URest...>
struct named_deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> {
struct named_deduced_unit : downcast_dispatch<Child, detail::deduced_unit<Dim, U, URest...>> {
static constexpr bool is_named = true;
static constexpr auto symbol = Symbol;
using prefix_family = PF;

View File

@@ -50,6 +50,8 @@ static_assert(centimetre::symbol == "cm");
// speed
static_assert((10_q_cm / 5_q_s).count() == 2);
static_assert((2_q_cm_per_s).count() == 2);
static_assert(10_q_cm / 5_q_s == 2_q_cm_per_s);
static_assert(10_q_cm / 2_q_cm_per_s == 5_q_s);
static_assert(10_q_cm == 2_q_cm_per_s * 5_q_s);
@@ -59,7 +61,11 @@ static_assert(detail::unit_text<dim_speed, centimetre_per_second>() == "cm/s");
// area
static_assert(centimetre::ratio / dimension_unit<dim_length>::ratio == ratio(1));
static_assert((1_q_cm * 1_q_cm).count() == 1);
static_assert((1_q_cm2).count() == 1);
static_assert(1_q_cm * 1_q_cm == 1_q_cm2);
static_assert(100_q_cm * 100_q_cm == area<physical::si::square_metre>(1));
static_assert(100_q_cm * 100_q_cm == length<physical::si::metre>(1) * length<physical::si::metre>(1));
static_assert(100_q_cm2 / 10_q_cm == 10_q_cm);
static_assert(detail::unit_text<dim_area, square_centimetre>() == basic_symbol_text("cm²", "cm^2"));

View File

@@ -49,11 +49,14 @@ using amplitude_spectral_density = quantity<dim_amplitude_spectral_density, U, R
namespace {
static_assert(is_same_v<dimension_sqrt<dim_power_spectral_density>, dim_amplitude_spectral_density>);
static_assert(is_same_v<dimension_pow<dim_amplitude_spectral_density, 2>, dim_power_spectral_density>);
template<typename T, typename U>
inline constexpr bool compare = DOWNCAST_MODE != 0 ? is_same_v<T, U> : (is_same_v<T, U> || units::equivalent<T, U>);
static_assert(is_same_v<decltype(pow<2>(amplitude_spectral_density<volt_per_sqrt_hertz>(4))), decltype(power_spectral_density<volt2_per_hertz>(16))>);
static_assert(is_same_v<decltype(sqrt(power_spectral_density<volt2_per_hertz>(16))), decltype(amplitude_spectral_density<volt_per_sqrt_hertz>(4))>);
static_assert(compare<dimension_sqrt<dim_power_spectral_density>, dim_amplitude_spectral_density>);
static_assert(compare<dimension_pow<dim_amplitude_spectral_density, 2>, dim_power_spectral_density>);
static_assert(compare<decltype(pow<2>(amplitude_spectral_density<volt_per_sqrt_hertz>(4))), decltype(power_spectral_density<volt2_per_hertz>(16))>);
static_assert(compare<decltype(sqrt(power_spectral_density<volt2_per_hertz>(16))), decltype(amplitude_spectral_density<volt_per_sqrt_hertz>(4))>);
}
@@ -63,6 +66,5 @@ struct kilogram_per_second : unit<kilogram_per_second> {};
struct dim_mass_rate : derived_dimension<dim_mass_rate, kilogram_per_second, units::exponent<dim_mass, 1>, units::exponent<dim_time, -1>> {};
struct kilogram_per_hour : deduced_unit<kilogram_per_hour, dim_mass_rate, kilogram, hour> {};
constexpr auto a = 1_q_kg / 1_q_h;
static_assert(is_same_v<decltype(a)::unit, kilogram_per_hour>);
}

View File

@@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "units/math.h"
#include "units/physical/international/area.h"
#include "units/physical/si/area.h"
#include "units/physical/si/speed.h"
#include "units/physical/international/area.h"
@@ -27,18 +29,20 @@
namespace {
using namespace units;
using namespace units::physical::si::literals;
using namespace units::physical::international::literals;
using namespace units;
using namespace units::physical::si::literals;
using namespace units::physical::international::literals;
static_assert(is_same_v<decltype(pow<0>(2_q_m)), std::int64_t>);
static_assert(is_same_v<decltype(pow<1>(2_q_m)), decltype(2_q_m)>);
static_assert(is_same_v<decltype(pow<2>(2_q_m)), decltype(4_q_m2)>);
static_assert(is_same_v<decltype(pow<2>(2_q_km)), decltype(4_q_km2)>);
static_assert(is_same_v<decltype(pow<2>(2_q_ft)), decltype(4_q_ft2)>);
static_assert(is_same_v<decltype(sqrt(4_q_m2)), decltype(2_q_m)>);
static_assert(is_same_v<decltype(sqrt(4_q_km2)), decltype(2_q_km)>);
static_assert(is_same_v<decltype(sqrt(4_q_ft2)), decltype(2_q_ft)>);
template<typename T, typename U>
inline constexpr bool compare = DOWNCAST_MODE != 0 ? is_same_v<T, U> : (is_same_v<T, U> || units::equivalent<T, U>);
static_assert(compare<decltype(pow<0>(2_q_m)), std::int64_t>);
static_assert(compare<decltype(pow<1>(2_q_m)), decltype(2_q_m)>);
static_assert(compare<decltype(pow<2>(2_q_m)), decltype(4_q_m2)>);
static_assert(compare<decltype(pow<2>(2_q_km)), decltype(4_q_km2)>);
static_assert(compare<decltype(pow<2>(2_q_ft)), decltype(4_q_ft2)>);
static_assert(compare<decltype(sqrt(4_q_m2)), decltype(2_q_m)>);
static_assert(compare<decltype(sqrt(4_q_km2)), decltype(2_q_km)>);
static_assert(compare<decltype(sqrt(4_q_ft2)), decltype(2_q_ft)>);
} // namespace

View File

@@ -35,6 +35,9 @@ namespace {
using namespace units;
using namespace units::physical::si;
template<typename T, typename U>
inline constexpr bool compare = DOWNCAST_MODE != 0 ? std::is_same_v<T, U> : (std::is_same_v<T, U> || units::equivalent<T, U>);
// class invariants
template<typename DimLength>
@@ -119,23 +122,23 @@ static_assert((quantity_point(2_q_m) -= 1_q_m).relative().count() == 1);
// non-member arithmetic operators
static_assert(is_same_v<decltype(quantity_point<dim_length, metre, int>() + length<metre, double>()),
static_assert(compare<decltype(quantity_point<dim_length, metre, int>() + length<metre, double>()),
quantity_point<dim_length, metre, double>>);
static_assert(is_same_v<decltype(length<metre, int>() + quantity_point<dim_length, metre, double>()),
static_assert(compare<decltype(length<metre, int>() + quantity_point<dim_length, metre, double>()),
quantity_point<dim_length, metre, double>>);
static_assert(is_same_v<decltype(quantity_point<dim_length, kilometre, int>() + length<metre, double>()),
static_assert(compare<decltype(quantity_point<dim_length, kilometre, int>() + length<metre, double>()),
quantity_point<dim_length, metre, double>>);
static_assert(is_same_v<decltype(length<kilometre, int>() + quantity_point<dim_length, metre, double>()),
static_assert(compare<decltype(length<kilometre, int>() + quantity_point<dim_length, metre, double>()),
quantity_point<dim_length, metre, double>>);
static_assert(is_same_v<decltype(quantity_point<dim_length, metre, double>() - length<metre, int>()),
static_assert(compare<decltype(quantity_point<dim_length, metre, double>() - length<metre, int>()),
quantity_point<dim_length, metre, double>>);
static_assert(is_same_v<decltype(quantity_point<dim_length, kilometre, double>() - length<metre, int>()),
static_assert(compare<decltype(quantity_point<dim_length, kilometre, double>() - length<metre, int>()),
quantity_point<dim_length, metre, double>>);
static_assert(
is_same_v<decltype(quantity_point<dim_length, metre, double>() - quantity_point<dim_length, metre, int>()),
compare<decltype(quantity_point<dim_length, metre, double>() - quantity_point<dim_length, metre, int>()),
length<metre, double>>);
static_assert(
is_same_v<decltype(quantity_point<dim_length, kilometre, double>() - quantity_point<dim_length, metre, int>()),
compare<decltype(quantity_point<dim_length, kilometre, double>() - quantity_point<dim_length, metre, int>()),
length<metre, double>>);
static_assert((1_q_m + km).relative().count() == 1001);
@@ -187,13 +190,13 @@ static_assert(QuantityPoint<quantity_point<dim_length, millimetre, int>>);
// common_quantity_point
static_assert(is_same_v<
static_assert(compare<
common_quantity_point<quantity_point<dim_length, metre, int>, quantity_point<dim_length, kilometre, int>>,
quantity_point<dim_length, metre, int>>);
static_assert(is_same_v<common_quantity_point<quantity_point<dim_length, kilometre, long long>,
static_assert(compare<common_quantity_point<quantity_point<dim_length, kilometre, long long>,
quantity_point<dim_length, metre, int>>,
quantity_point<dim_length, metre, long long>>);
static_assert(is_same_v<common_quantity_point<quantity_point<dim_length, kilometre, long long>,
static_assert(compare<common_quantity_point<quantity_point<dim_length, kilometre, long long>,
quantity_point<dim_length, millimetre, double>>,
quantity_point<dim_length, millimetre, double>>);
@@ -208,8 +211,7 @@ static_assert(std::equality_comparable_with<decltype(quantity_point(1_q_m)), dec
// quantity_cast
static_assert(
is_same_v<decltype(quantity_point_cast<scaled_unit<ratio(1), metre>>(quantity_point(2_q_km)))::unit, metre>);
static_assert(compare<decltype(quantity_point_cast<scaled_unit<ratio(1), metre>>(quantity_point(2_q_km)))::unit, metre>);
static_assert(quantity_point_cast<quantity_point<dim_length, metre, int>>(quantity_point(2_q_km)).relative().count() ==
2000);

View File

@@ -34,6 +34,9 @@ namespace {
using namespace units;
using namespace units::physical::si;
template<typename T, typename U>
inline constexpr bool compare = DOWNCAST_MODE != 0 ? std::is_same_v<T, U> : (std::is_same_v<T, U> || units::equivalent<T, U>);
// class invariants
template<typename DimLength>
@@ -147,37 +150,35 @@ static_assert(invalid_compound_assignments<metre>);
// non-member arithmetic operators
static_assert(is_same_v<decltype(length<metre, int>() + length<metre, double>()), length<metre, double>>);
static_assert(is_same_v<decltype(length<metre, int>() + length<metre, double>()), length<metre, double>>);
static_assert(compare<decltype(length<metre, int>() + length<metre, double>()), length<metre, double>>);
static_assert(compare<decltype(length<metre, int>() + length<metre, double>()), length<metre, double>>);
static_assert(compare<decltype(length<kilometre, int>() + length<metre, double>()), length<metre, double>>);
static_assert(compare<decltype(length<metre, double>() - length<metre, int>()), length<metre, double>>);
static_assert(compare<decltype(length<kilometre, double>() - length<metre, int>()), length<metre, double>>);
static_assert(compare<decltype(length<metre, int>() * 1.0), length<metre, double>>);
static_assert(compare<decltype(1.0 * length<metre, int>()), length<metre, double>>);
static_assert(
is_same_v<decltype(length<kilometre, int>() + length<metre, double>()), length<metre, double>>);
static_assert(is_same_v<decltype(length<metre, double>() - length<metre, int>()), length<metre, double>>);
compare<decltype(speed<metre_per_second, int>() * physical::si::time<second, int>()), length<metre, int>>);
static_assert(
is_same_v<decltype(length<kilometre, double>() - length<metre, int>()), length<metre, double>>);
static_assert(is_same_v<decltype(length<metre, int>() * 1.0), length<metre, double>>);
static_assert(is_same_v<decltype(1.0 * length<metre, int>()), length<metre, double>>);
static_assert(
is_same_v<decltype(speed<metre_per_second, int>() * physical::si::time<second, int>()), length<metre, int>>);
static_assert(
is_same_v<decltype(speed<metre_per_second, int>() * physical::si::time<hour, int>()), length<scaled_unit<ratio(36, 1, 2), metre>, int>>);
static_assert(is_same_v<decltype(length<metre>() * physical::si::time<minute>()),
compare<decltype(speed<metre_per_second, int>() * physical::si::time<hour, int>()), length<scaled_unit<ratio(36, 1, 2), metre>, int>>);
static_assert(compare<decltype(length<metre>() * physical::si::time<minute>()),
quantity<unknown_dimension<units::exponent<dim_length, 1>, units::exponent<dim_time, 1>>, scaled_unit<ratio(6, 1, 1), unknown_coherent_unit>>>);
static_assert(is_same_v<decltype(1 / physical::si::time<second, int>()), frequency<hertz, int>>);
static_assert(is_same_v<decltype(1 / physical::si::time<minute, int>()), frequency<scaled_unit<ratio(1, 6, -1), hertz>, int>>);
static_assert(is_same_v<decltype(1 / frequency<hertz, int>()), physical::si::time<second, int>>);
static_assert(is_same_v<decltype(1 / length<kilometre>()),
static_assert(compare<decltype(1 / physical::si::time<second, int>()), frequency<hertz, int>>);
static_assert(compare<decltype(1 / physical::si::time<minute, int>()), frequency<scaled_unit<ratio(1, 6, -1), hertz>, int>>);
static_assert(compare<decltype(1 / frequency<hertz, int>()), physical::si::time<second, int>>);
static_assert(compare<decltype(1 / length<kilometre>()),
quantity<unknown_dimension<units::exponent<dim_length, -1>>, scaled_unit<ratio(1, 1, -3), unknown_coherent_unit>>>);
static_assert(is_same_v<decltype(length<metre, int>() / 1.0), length<metre, double>>);
static_assert(is_same_v<decltype(length<metre, int>() / length<metre, double>()), dimensionless<one, double>>);
static_assert(is_same_v<decltype(length<kilometre, int>() / length<metre, double>()), dimensionless<scaled_unit<ratio(1, 1, 3), one>, double>>);
static_assert(compare<decltype(length<metre, int>() / 1.0), length<metre, double>>);
static_assert(compare<decltype(length<metre, int>() / length<metre, double>()), dimensionless<one, double>>);
static_assert(compare<decltype(length<kilometre, int>() / length<metre, double>()), dimensionless<scaled_unit<ratio(1, 1, 3), one>, double>>);
static_assert(
is_same_v<decltype(length<metre, int>() / physical::si::time<second, int>()), speed<metre_per_second, int>>);
compare<decltype(length<metre, int>() / physical::si::time<second, int>()), speed<metre_per_second, int>>);
static_assert(
is_same_v<decltype(length<metre>() / physical::si::time<minute>()), speed<scaled_unit<ratio(1, 6, -1), metre_per_second>>>);
static_assert(is_same_v<decltype(physical::si::time<minute>() / length<metre>()),
compare<decltype(length<metre>() / physical::si::time<minute>()), speed<scaled_unit<ratio(1, 6, -1), metre_per_second>>>);
static_assert(compare<decltype(physical::si::time<minute>() / length<metre>()),
quantity<unknown_dimension<units::exponent<dim_length, -1>, units::exponent<dim_time, 1>>, scaled_unit<ratio(6 ,1 , 1), unknown_coherent_unit>>>);
static_assert(is_same_v<decltype(length<metre, int>() % short(1)), length<metre, int>>);
static_assert(is_same_v<decltype(length<metre, int>() % length<metre, short>(1)), length<metre, int>>);
static_assert(compare<decltype(length<metre, int>() % short(1)), length<metre, int>>);
static_assert(compare<decltype(length<metre, int>() % length<metre, short>(1)), length<metre, int>>);
static_assert((1_q_m + km).count() == 1001);
static_assert((1_q_m + 1_q_km).count() == 1001);
@@ -251,11 +252,10 @@ static_assert(Quantity<length<millimetre, int>>);
// common_quantity
static_assert(is_same_v<common_quantity<length<metre, int>, length<kilometre, int>>, length<metre, int>>);
static_assert(compare<common_quantity<length<metre, int>, length<kilometre, int>>, length<metre, int>>);
static_assert(compare<common_quantity<length<kilometre, long long>, length<metre, int>>, length<metre, long long>>);
static_assert(
is_same_v<common_quantity<length<kilometre, long long>, length<metre, int>>, length<metre, long long>>);
static_assert(is_same_v<common_quantity<length<kilometre, long long>, length<millimetre, double>>,
length<millimetre, double>>);
compare<common_quantity<length<kilometre, long long>, length<millimetre, double>>, length<millimetre, double>>);
// common_type
@@ -272,7 +272,7 @@ static_assert(!std::equality_comparable_with<dimensionless<one, int>, double>);
// quantity_cast
static_assert(is_same_v<decltype(quantity_cast<scaled_unit<ratio(1), metre>>(2_q_km))::unit, metre>);
static_assert(compare<decltype(quantity_cast<scaled_unit<ratio(1), metre>>(2_q_km))::unit, metre>);
static_assert(quantity_cast<length<metre, int>>(2_q_km).count() == 2000);
static_assert(quantity_cast<length<kilometre, int>>(2000_q_m).count() == 2);
@@ -339,6 +339,20 @@ static_assert(1_q_km / 1_q_s == 1000_q_m_per_s);
static_assert(2_q_km_per_h * 2_q_h == 4_q_km);
static_assert(2_q_km / 2_q_km_per_h == 1_q_h);
static_assert(is_same_v<decltype(pow<2>(2_q_m)), decltype(4_q_m2)>);
static_assert(compare<decltype(pow<2>(2_q_m)), decltype(4_q_m2)>);
// downcasting
#if DOWNCAST_MODE == 0
static_assert(std::is_same_v<decltype(10_q_m / 5_q_s), quantity<unknown_dimension<units::exp<dim_length, 1>, units::exp<dim_time, -1>>, scaled_unit<ratio(1), unknown_coherent_unit>, std::int64_t>>);
static_assert(std::is_same_v<decltype(1_q_mm + 1_q_km), length<scaled_unit<ratio(1, 1, -3), metre>, std::int64_t>>);
#else
static_assert(std::is_same_v<decltype(10_q_m / 5_q_s), speed<metre_per_second, std::int64_t>>);
static_assert(std::is_same_v<decltype(1_q_mm + 1_q_km), length<millimetre, std::int64_t>>);
#endif
} // namespace

View File

@@ -46,6 +46,13 @@ namespace {
using namespace units::physical;
static_assert(units::detail::quantity_ratio(si::length<si::metre>(1)) == units::ratio(1));
static_assert(units::detail::quantity_ratio(cgs::length<cgs::centimetre>(1)) == units::ratio(1, 100));
static_assert(units::detail::quantity_ratio(si::speed<si::metre_per_second>(1)) == units::ratio(1));
static_assert(units::detail::quantity_ratio(cgs::speed<cgs::centimetre_per_second>(1)) == units::ratio(1, 100));
static_assert(units::detail::quantity_ratio(si::force<si::newton>(1)) == units::ratio(1000)); // defined in terms of kilogram that are 1000 * gram
static_assert(units::detail::quantity_ratio(cgs::force<cgs::dyne>(1)) == units::ratio(1, 100)); // defined in terms of gram so only centimetre ratio counts here
static_assert(cgs::length<cgs::centimetre>(100) == si::length<si::metre>(1));
static_assert(cgs::mass<cgs::gram>(1'000) == si::mass<si::kilogram>(1));
static_assert(cgs::time<cgs::second>(1) == si::time<si::second>(1));

View File

@@ -241,8 +241,6 @@ static_assert(kilogray::symbol == "kGy");
// speed
static_assert(is_same_v<decltype(1_q_km / 1_q_s), speed<scaled_unit<ratio(1, 1, 3), metre_per_second>, std::int64_t>>);
static_assert(10_q_m / 5_q_s == 2_q_m_per_s);
static_assert(10 / 5_q_s * 1_q_m == 2_q_m_per_s);
static_assert(1_q_km / 1_q_s == 1000_q_m_per_s);

View File

@@ -21,6 +21,7 @@
// SOFTWARE.
#include "units/unit.h"
#include "units/bits/equivalent.h"
#include "units/physical/si/prefixes.h"
namespace {
@@ -28,6 +29,9 @@ namespace {
using namespace units;
using namespace units::physical;
template<typename T, typename U>
inline constexpr bool compare = DOWNCAST_MODE != 0 ? std::is_same_v<T, U> : (std::is_same_v<T, U> || units::equivalent<T, U>);
struct metre : named_unit<metre, "m", si::prefix> {};
struct centimetre : prefixed_unit<centimetre, si::centi, metre> {};
struct kilometre : prefixed_unit<kilometre, si::kilo, metre> {};
@@ -49,11 +53,11 @@ struct metre_per_second : unit<metre_per_second> {};
struct dim_speed : derived_dimension<dim_speed, metre_per_second, units::exponent<dim_length, 1>, units::exponent<dim_time, -1>> {};
struct kilometre_per_hour : deduced_unit<kilometre_per_hour, dim_speed, kilometre, hour> {};
static_assert(is_same_v<downcast<scaled_unit<ratio(1), metre>>, metre>);
static_assert(is_same_v<downcast<scaled_unit<ratio(1, 1, -2), metre>>, centimetre>);
static_assert(is_same_v<downcast<scaled_unit<ratio(yard::ratio.num, yard::ratio.den, yard::ratio.exp), metre>>, yard>);
static_assert(is_same_v<downcast<scaled_unit<yard::ratio * ratio(1, 3), metre>>, foot>);
static_assert(is_same_v<downcast<scaled_unit<kilometre::ratio / hour::ratio, metre_per_second>>, kilometre_per_hour>);
static_assert(compare<downcast<scaled_unit<ratio(1), metre>>, metre>);
static_assert(compare<downcast<scaled_unit<ratio(1, 1, -2), metre>>, centimetre>);
static_assert(compare<downcast<scaled_unit<ratio(yard::ratio.num, yard::ratio.den, yard::ratio.exp), metre>>, yard>);
static_assert(compare<downcast<scaled_unit<yard::ratio * ratio(1, 3), metre>>, foot>);
static_assert(compare<downcast<scaled_unit<kilometre::ratio / hour::ratio, metre_per_second>>, kilometre_per_hour>);
#if COMP_GCC >= 10
static_assert([]<ratio R>() { return !requires { typename scaled_unit<R, metre>; }; }.template operator()<ratio(-1, 1)>()); // negative unit ratio