Merge branch 'master' into feature/more-value-casts

This commit is contained in:
Mateusz Pusz authored and GitHub committed 2024-06-14 21:44:19 +09:00
commit b113f6ad0e
102 files changed
+1293 -1021

No files matched your search

+7 -20
View File
@@ -23,8 +23,7 @@
cmake_minimum_required(VERSION 3.23)
function(set_feature_flag name)
set(val_list "TRUE" "FALSE")
if(${projectPrefix}${name} IN_LIST val_list)
if(NOT ${projectPrefix}${name} STREQUAL "AUTO")
target_compile_definitions(
mp-units-core ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}${name}=$<BOOL:${${projectPrefix}${name}}>
)
@@ -36,15 +35,16 @@ add_mp_units_module(
core mp-units-core
HEADERS include/mp-units/bits/core_gmf.h
include/mp-units/bits/get_associated_quantity.h
include/mp-units/bits/get_common_base.h
include/mp-units/bits/hacks.h
include/mp-units/bits/math_concepts.h
include/mp-units/bits/module_macros.h
include/mp-units/bits/quantity_spec_hierarchy.h
include/mp-units/bits/ratio.h
include/mp-units/bits/sudo_cast.h
include/mp-units/bits/text_tools.h
include/mp-units/bits/type_list.h
include/mp-units/ext/algorithm.h
include/mp-units/ext/contracts.h
include/mp-units/ext/fixed_string.h
include/mp-units/ext/prime.h
include/mp-units/ext/type_name.h
@@ -86,6 +86,7 @@ if(NOT ${projectPrefix}API_FREESTANDING)
FILES
include/mp-units/bits/fmt.h
include/mp-units/bits/requires_hosted.h
include/mp-units/ext/format.h
include/mp-units/math.h
include/mp-units/ostream.h
include/mp-units/format.h
@@ -99,8 +100,8 @@ set_feature_flag(API_NO_CRTP)
# Text formatting
if(NOT ${projectPrefix}API_FREESTANDING
AND (${projectPrefix}API_STD_FORMAT STREQUAL "FALSE" OR (${projectPrefix}API_STD_FORMAT STREQUAL "AUTO"
AND NOT ${projectPrefix}LIB_FORMAT_SUPPORTED))
AND (NOT ${projectPrefix}API_STD_FORMAT OR (${projectPrefix}API_STD_FORMAT STREQUAL "AUTO"
AND NOT ${projectPrefix}LIB_FORMAT_SUPPORTED))
)
if(NOT TARGET fmt::fmt)
find_package(fmt REQUIRED)
@@ -109,21 +110,7 @@ if(NOT ${projectPrefix}API_FREESTANDING
endif()
# Contracts checking
if(${projectPrefix}API_CONTRACTS STREQUAL "NONE")
target_compile_definitions(mp-units-core ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}API_CONTRACTS=0)
elseif(${projectPrefix}API_CONTRACTS STREQUAL "GSL-LITE")
if(NOT TARGET gsl::gsl-lite)
find_package(gsl-lite REQUIRED)
endif()
target_link_libraries(mp-units-core ${${projectPrefix}TARGET_SCOPE} gsl::gsl-lite)
target_compile_definitions(mp-units-core ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}API_CONTRACTS=2)
elseif(${projectPrefix}API_CONTRACTS STREQUAL "MS-GSL")
if(NOT TARGET Microsoft.GSL::GSL)
find_package(Microsoft.GSL REQUIRED)
endif()
target_link_libraries(mp-units-core ${${projectPrefix}TARGET_SCOPE} Microsoft.GSL::GSL)
target_compile_definitions(mp-units-core ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}API_CONTRACTS=3)
endif()
target_link_libraries(mp-units-core ${${projectPrefix}TARGET_SCOPE} mp-units-contracts)
# C++20 modules
if(${projectPrefix}BUILD_CXX_MODULES)
+2 -11
View File
@@ -24,6 +24,7 @@
#include <mp-units/bits/hacks.h>
#include <mp-units/compat_macros.h>
#include <mp-units/ext/contracts.h>
#include <array>
#include <compare>
#include <concepts>
@@ -44,23 +45,13 @@
#include <utility>
#if MP_UNITS_HOSTED
#include <mp-units/ext/format.h>
#include <cmath>
#include <locale>
#include <ostream>
#include <random>
#include <sstream>
#include <string>
#if MP_UNITS_USE_FMTLIB
MP_UNITS_DIAGNOSTIC_PUSH
MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/format.h>
MP_UNITS_DIAGNOSTIC_POP
#else
#include <format>
#endif
#endif
#if __cpp_lib_text_encoding
+2
View File
@@ -35,6 +35,8 @@
#include <mp-units/ext/algorithm.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <mp-units/ext/format.h>
#include <array>
#include <concepts>
#include <cstdint>
@@ -56,7 +56,7 @@ template<QuantitySpec A, QuantitySpec B>
}
template<QuantitySpec A, QuantitySpec B>
[[nodiscard]] consteval auto have_common_base(A a, B b)
[[nodiscard]] consteval bool have_common_base(A a, B b)
{
constexpr std::size_t a_length = hierarchy_path_length(A{});
constexpr std::size_t b_length = hierarchy_path_length(B{});
@@ -68,7 +68,7 @@ template<QuantitySpec A, QuantitySpec B>
template<QuantitySpec A, QuantitySpec B>
requires(have_common_base_in_hierarchy_of_equal_length(A{}, B{}))
[[nodiscard]] consteval auto get_common_base_for_hierarchy_of_equal_length(A a, B b)
[[nodiscard]] consteval QuantitySpec auto get_common_base_for_hierarchy_of_equal_length(A a, B b)
{
if constexpr (is_same_v<A, B>)
return a;
@@ -78,14 +78,29 @@ template<QuantitySpec A, QuantitySpec B>
template<QuantitySpec A, QuantitySpec B>
requires(have_common_base(A{}, B{}))
[[nodiscard]] consteval auto get_common_base(A a, B b)
[[nodiscard]] consteval QuantitySpec auto get_common_base(A a, B b)
{
constexpr int a_length = hierarchy_path_length(A{});
constexpr int b_length = hierarchy_path_length(B{});
constexpr std::size_t a_length = hierarchy_path_length(A{});
constexpr std::size_t b_length = hierarchy_path_length(B{});
if constexpr (a_length > b_length)
return get_common_base_for_hierarchy_of_equal_length(hierarchy_path_advance<a_length - b_length>(a), b);
else
return get_common_base_for_hierarchy_of_equal_length(a, hierarchy_path_advance<b_length - a_length>(b));
}
template<QuantitySpec Child, QuantitySpec Parent>
[[nodiscard]] consteval bool is_child_of(Child ch, Parent p)
{
if constexpr (Child{} == Parent{})
return std::true_type{};
else {
constexpr std::size_t child_length = hierarchy_path_length(Child{});
constexpr std::size_t parent_length = hierarchy_path_length(Parent{});
if constexpr (parent_length > child_length)
return false;
else
return hierarchy_path_advance<child_length - parent_length>(ch) == p;
}
}
} // namespace mp_units::detail
+1
View File
@@ -27,6 +27,7 @@
#include <mp-units/compat_macros.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export
#include <cstdint>
#include <numeric>
+4 -21
View File
@@ -28,14 +28,14 @@
#ifdef MP_UNITS_API_NO_CRTP
#define QUANTITY_SPEC(name, ...) \
inline constexpr struct name : ::mp_units::quantity_spec<__VA_ARGS__> { \
#define QUANTITY_SPEC(name, ...) \
inline constexpr struct name final : ::mp_units::quantity_spec<__VA_ARGS__> { \
} name
#else
#define QUANTITY_SPEC(name, ...) \
inline constexpr struct name : ::mp_units::quantity_spec<name, __VA_ARGS__> { \
#define QUANTITY_SPEC(name, ...) \
inline constexpr struct name final : ::mp_units::quantity_spec<name, __VA_ARGS__> { \
} name
#endif
@@ -83,26 +83,12 @@
#ifndef MP_UNITS_IN_MODULE_INTERFACE
// IWYU pragma: begin_exports
#if MP_UNITS_USE_FMTLIB
MP_UNITS_DIAGNOSTIC_PUSH
MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/format.h>
MP_UNITS_DIAGNOSTIC_POP
#else
#include <format>
#endif
// IWYU pragma: end_exports
#endif
#endif // MP_UNITS_HOSTED
#if MP_UNITS_API_CONTRACTS == 2 || __has_include(<gsl/gsl-lite.hpp>)
#include <gsl/gsl-lite.hpp>
#define MP_UNITS_EXPECTS(expr) gsl_Expects(expr)
#define MP_UNITS_EXPECTS_DEBUG(expr) gsl_ExpectsDebug(expr)
#define MP_UNITS_ASSERT(expr) gsl_Assert(expr)
@@ -110,9 +96,6 @@ MP_UNITS_DIAGNOSTIC_POP
#elif MP_UNITS_API_CONTRACTS == 3 || __has_include(<gsl/gsl>)
#include <gsl/gsl>
#include <cassert>
#define MP_UNITS_EXPECTS(expr) Expects(expr)
#if defined NDEBUG
#define MP_UNITS_EXPECTS_DEBUG(expr) static_cast<void>(0)
+49
View File
@@ -0,0 +1,49 @@
// 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
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/bits/hacks.h>
#include <mp-units/compat_macros.h>
#if MP_UNITS_API_CONTRACTS == 2 || __has_include(<gsl/gsl-lite.hpp>)
#if MP_UNITS_HOSTED
#include <gsl/gsl-lite.hpp>
#else
#include <mp-units/bits/requires_hosted.h>
#endif
#elif MP_UNITS_API_CONTRACTS == 3 || __has_include(<gsl/gsl>)
#if MP_UNITS_HOSTED
#include <gsl/gsl>
#include <cassert>
#else
#include <mp-units/bits/requires_hosted.h>
#endif
#endif
#endif // MP_UNITS_IN_MODULE_INTERFACE
@@ -32,12 +32,14 @@
#include <mp-units/ext/type_traits.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export
#include <cstddef>
#include <cstdlib>
#include <ranges>
#include <string_view>
#if MP_UNITS_HOSTED
#include <mp-units/ext/format.h>
#include <ostream>
#endif
#endif
+42
View File
@@ -0,0 +1,42 @@
// 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
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/bits/requires_hosted.h>
//
#include <mp-units/bits/hacks.h>
#include <mp-units/compat_macros.h>
#if MP_UNITS_USE_FMTLIB
MP_UNITS_DIAGNOSTIC_PUSH
MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/format.h>
MP_UNITS_DIAGNOSTIC_POP
#else
#include <format>
#endif
#endif
@@ -34,6 +34,7 @@
#include <mp-units/framework/symbol_text.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <array>
#include <cstdint>
#include <iterator>
@@ -61,9 +62,9 @@ namespace mp_units {
* For example:
*
* @code{.cpp}
* inline constexpr struct dim_length : base_dimension<"L"> {} dim_length;
* inline constexpr struct dim_time : base_dimension<"T"> {} dim_time;
* inline constexpr struct dim_mass : base_dimension<"M"> {} dim_mass;
* inline constexpr struct dim_length final : base_dimension<"L"> {} dim_length;
* inline constexpr struct dim_time final : base_dimension<"T"> {} dim_time;
* inline constexpr struct dim_mass final : base_dimension<"M"> {} dim_mass;
* @endcode
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
@@ -86,6 +87,9 @@ struct base_dimension_less : std::bool_constant<(Lhs::symbol < Rhs::symbol)> {};
template<typename T1, typename T2>
using type_list_of_base_dimension_less = expr_less<T1, T2, base_dimension_less>;
template<detail::DerivedDimensionExpr... Expr>
struct derived_dimension_impl : detail::expr_fractions<detail::is_dimension_one, Expr...> {};
} // namespace detail
/**
@@ -131,7 +135,7 @@ using type_list_of_base_dimension_less = expr_less<T1, T2, base_dimension_less>;
* instantiate this type automatically based on the dimensional arithmetic equation provided by the user.
*/
template<detail::DerivedDimensionExpr... Expr>
struct derived_dimension : detail::expr_fractions<detail::is_dimension_one, Expr...> {};
struct derived_dimension final : detail::derived_dimension_impl<Expr...> {};
/**
* @brief Dimension one
@@ -140,7 +144,7 @@ struct derived_dimension : detail::expr_fractions<detail::is_dimension_one, Expr
* dimensions are zero. It is a dimension of a quantity of dimension one also known as
* "dimensionless".
*/
MP_UNITS_EXPORT inline constexpr struct dimension_one : derived_dimension<> {
MP_UNITS_EXPORT inline constexpr struct dimension_one final : detail::derived_dimension_impl<> {
} dimension_one;
namespace detail {
@@ -279,13 +283,13 @@ constexpr Out dimension_symbol_impl(Out out, const type_list<Nums...>& nums, con
}
template<typename CharT, std::output_iterator<CharT> Out, typename... Expr>
constexpr Out dimension_symbol_impl(Out out, const derived_dimension<Expr...>&, const dimension_symbol_formatting& fmt,
bool negative_power)
constexpr Out dimension_symbol_impl(Out out, const derived_dimension_impl<Expr...>&,
const dimension_symbol_formatting& fmt, bool negative_power)
{
(void)negative_power;
MP_UNITS_EXPECTS(negative_power == false);
return dimension_symbol_impl<CharT>(out, typename derived_dimension<Expr...>::_num_{},
typename derived_dimension<Expr...>::_den_{}, fmt);
return dimension_symbol_impl<CharT>(out, typename derived_dimension_impl<Expr...>::_num_{},
typename derived_dimension_impl<Expr...>::_den_{}, fmt);
}
@@ -42,20 +42,13 @@ template<typename T>
inline constexpr bool is_derived_from_specialization_of_base_dimension =
requires(T* t) { to_base_specialization_of_base_dimension(t); };
template<typename T>
inline constexpr bool is_specialization_of_base_dimension = false;
template<symbol_text Symbol>
inline constexpr bool is_specialization_of_base_dimension<base_dimension<Symbol>> = true;
/**
* @brief A concept matching all named base dimensions in the library.
*
* Satisfied by all dimension types derived from a specialization of `base_dimension`.
*/
template<typename T>
concept BaseDimension =
is_derived_from_specialization_of_base_dimension<T> && (!is_specialization_of_base_dimension<T>);
concept BaseDimension = is_derived_from_specialization_of_base_dimension<T> && std::is_final_v<T>;
template<typename T>
struct is_dimension_one : std::false_type {};
@@ -91,7 +84,8 @@ namespace detail {
* being the `dimension_one`.
*/
template<typename T>
concept DerivedDimension = is_specialization_of<T, derived_dimension> || is_dimension_one<T>::value;
concept DerivedDimension =
(is_specialization_of<T, derived_dimension> || is_dimension_one<T>::value) && std::is_final_v<T>;
} // namespace detail
@@ -29,6 +29,7 @@
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <cstdint>
#include <functional>
#endif
namespace mp_units {
@@ -517,8 +517,7 @@ template<typename T, auto... Ms>
constexpr T get_value(const magnitude<Ms...>&)
{
// Force the expression to be evaluated in a constexpr context, to catch, e.g., overflow.
constexpr auto result = detail::checked_static_cast<T>((detail::compute_base_power<T>(Ms) * ... * T{1}));
constexpr T result = detail::checked_static_cast<T>((detail::compute_base_power<T>(Ms) * ... * T{1}));
return result;
}
@@ -677,16 +676,6 @@ template<auto... Ms>
[[nodiscard]] consteval auto denominator(Magnitude auto m) { return numerator(pow<-1>(m)); }
// TODO This probably should not be exported but is used in chrono.h
MP_UNITS_EXPORT constexpr ratio as_ratio(Magnitude auto m)
requires(is_rational(decltype(m){}))
{
return ratio{
get_value<std::intmax_t>(numerator(m)),
get_value<std::intmax_t>(denominator(m)),
};
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Common Magnitude.
//
@@ -758,7 +747,7 @@ template<auto H1, auto... T1, auto H2, auto... T2>
template<auto... Ms>
[[nodiscard]] consteval auto common_magnitude_type_impl(magnitude<Ms...>)
{
return (decltype(get_base_value(Ms)){} * ... * std::intmax_t{});
return (std::intmax_t{} * ... * decltype(get_base_value(Ms)){});
}
// Returns the most precise type to express the magnitude factor
+10 -4
View File
@@ -37,6 +37,7 @@
#include <mp-units/framework/unit_concepts.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export
#include <utility>
#endif
@@ -45,16 +46,21 @@ namespace mp_units {
namespace detail {
template<auto UFrom, auto UTo>
concept IntegralConversionFactor = Unit<decltype(UFrom)> && Unit<decltype(UTo)> &&
is_integral(get_canonical_unit(UFrom).mag / get_canonical_unit(UTo).mag);
template<Unit UFrom, Unit UTo>
[[nodiscard]] consteval bool integral_conversion_factor(UFrom from, UTo to)
{
if constexpr (is_same_v<UFrom, UTo>)
return true;
else
return is_integral(get_canonical_unit(from).mag / get_canonical_unit(to).mag);
}
template<typename QFrom, typename QTo>
concept QuantityConvertibleTo =
Quantity<QFrom> && Quantity<QTo> && implicitly_convertible(QFrom::quantity_spec, QTo::quantity_spec) &&
convertible(QFrom::unit, QTo::unit) &&
(treat_as_floating_point<typename QTo::rep> ||
(!treat_as_floating_point<typename QFrom::rep> && IntegralConversionFactor<QFrom::unit, QTo::unit>)) &&
(!treat_as_floating_point<typename QFrom::rep> && (integral_conversion_factor(QFrom::unit, QTo::unit)))) &&
// TODO consider providing constraints of sudo_cast here rather than testing if it can be called (its return type is
// deduced thus the function is evaluated here and may emit truncating conversion or other warnings)
requires(QFrom q) { detail::sudo_cast<QTo>(q); };
@@ -35,11 +35,10 @@
namespace mp_units {
MP_UNITS_EXPORT template<typename Derived, QuantitySpec auto QS>
MP_UNITS_EXPORT template<QuantitySpec auto QS>
// NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
struct absolute_point_origin {
static constexpr QuantitySpec auto quantity_spec = QS;
using _type_ = absolute_point_origin;
};
MP_UNITS_EXPORT template<QuantityPoint auto QP>
@@ -56,7 +55,7 @@ struct relative_point_origin {
};
template<QuantitySpec auto QS>
struct zeroth_point_origin_ : absolute_point_origin<zeroth_point_origin_<QS>, QS> {};
struct zeroth_point_origin_ final : absolute_point_origin<QS> {};
MP_UNITS_EXPORT template<QuantitySpec auto QS>
inline constexpr zeroth_point_origin_<QS> zeroth_point_origin;
@@ -81,9 +80,8 @@ MP_UNITS_EXPORT template<PointOrigin PO1, PointOrigin PO2>
[[nodiscard]] consteval bool operator==(PO1 po1, PO2 po2)
{
if constexpr (detail::AbsolutePointOrigin<PO1> && detail::AbsolutePointOrigin<PO2>)
return is_same_v<typename PO1::_type_, typename PO2::_type_> ||
(detail::is_zeroth_point_origin(po1) && detail::is_zeroth_point_origin(po2) &&
interconvertible(po1.quantity_spec, po2.quantity_spec));
return is_same_v<PO1, PO2> || (detail::is_zeroth_point_origin(po1) && detail::is_zeroth_point_origin(po2) &&
interconvertible(po1.quantity_spec, po2.quantity_spec));
else if constexpr (detail::RelativePointOrigin<PO1> && detail::RelativePointOrigin<PO2>)
return PO1::quantity_point == PO2::quantity_point;
else if constexpr (detail::RelativePointOrigin<PO1>)
@@ -32,7 +32,7 @@
namespace mp_units {
MP_UNITS_EXPORT template<typename Derived, QuantitySpec auto QS>
MP_UNITS_EXPORT template<QuantitySpec auto QS>
struct absolute_point_origin;
namespace detail {
@@ -40,22 +40,15 @@ namespace detail {
template<typename T>
inline constexpr bool is_quantity_point = false;
template<typename T>
inline constexpr bool is_specialization_of_absolute_point_origin = false;
template<typename D, auto Q>
inline constexpr bool is_specialization_of_absolute_point_origin<absolute_point_origin<D, Q>> = true;
template<typename D, auto Q>
void to_base_specialization_of_absolute_point_origin(const volatile absolute_point_origin<D, Q>*);
template<auto Q>
void to_base_specialization_of_absolute_point_origin(const volatile absolute_point_origin<Q>*);
template<typename T>
inline constexpr bool is_derived_from_specialization_of_absolute_point_origin =
requires(T* t) { to_base_specialization_of_absolute_point_origin(t); };
template<typename T>
concept AbsolutePointOrigin =
is_derived_from_specialization_of_absolute_point_origin<T> && !is_specialization_of_absolute_point_origin<T>;
concept AbsolutePointOrigin = is_derived_from_specialization_of_absolute_point_origin<T> && std::is_final_v<T>;
} // namespace detail
@@ -72,12 +65,6 @@ struct relative_point_origin;
namespace detail {
template<typename T>
inline constexpr bool is_specialization_of_relative_point_origin = false;
template<auto QP>
inline constexpr bool is_specialization_of_relative_point_origin<relative_point_origin<QP>> = true;
template<auto QP>
void to_base_specialization_of_relative_point_origin(const volatile relative_point_origin<QP>*);
@@ -86,8 +73,7 @@ inline constexpr bool is_derived_from_specialization_of_relative_point_origin =
requires(T* t) { to_base_specialization_of_relative_point_origin(t); };
template<typename T>
concept RelativePointOrigin =
is_derived_from_specialization_of_relative_point_origin<T> && !is_specialization_of_relative_point_origin<T>;
concept RelativePointOrigin = is_derived_from_specialization_of_relative_point_origin<T> && std::is_final_v<T>;
} // namespace detail
@@ -23,9 +23,9 @@
#pragma once
// IWYU pragma: private, include <mp-units/framework.h>
#include <mp-units/bits/get_common_base.h>
#include <mp-units/bits/hacks.h>
#include <mp-units/bits/module_macros.h>
#include <mp-units/bits/quantity_spec_hierarchy.h>
#include <mp-units/compat_macros.h>
#include <mp-units/ext/algorithm.h>
#include <mp-units/ext/type_name.h>
@@ -199,13 +199,13 @@ MP_UNITS_EXPORT_END
* For example:
*
* @code{.cpp}
* inline constexpr struct dim_length : base_dimension<"L"> {} dim_length;
* inline constexpr struct dim_mass : base_dimension<"M"> {} dim_mass;
* inline constexpr struct dim_time : base_dimension<"T"> {} dim_time;
* inline constexpr struct dim_length final : base_dimension<"L"> {} dim_length;
* inline constexpr struct dim_mass final : base_dimension<"M"> {} dim_mass;
* inline constexpr struct dim_time final : base_dimension<"T"> {} dim_time;
*
* inline constexpr struct length : quantity_spec<dim_length> {} length;
* inline constexpr struct mass : quantity_spec<dim_mass> {} mass;
* inline constexpr struct time : quantity_spec<dim_time> {} time;
* inline constexpr struct length final : quantity_spec<dim_length> {} length;
* inline constexpr struct mass final : quantity_spec<dim_mass> {} mass;
* inline constexpr struct time final : quantity_spec<dim_time> {} time;
* @endcode
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
@@ -225,6 +225,7 @@ template<typename Self, detail::BaseDimension auto Dim, one_of<quantity_characte
requires(... && !QuantitySpec<decltype(Args)>)
struct quantity_spec<Self, Dim, Args...> : detail::quantity_spec_interface<Self> {
#endif
using _base_type_ = quantity_spec;
static constexpr detail::BaseDimension auto dimension = Dim;
static constexpr quantity_character character = detail::quantity_character_init<Args...>(quantity_character::scalar);
};
@@ -243,12 +244,12 @@ struct quantity_spec<Self, Dim, Args...> : detail::quantity_spec_interface<Self>
* For example:
*
* @code{.cpp}
* inline constexpr struct area : quantity_spec<pow<2>(length)> {} area;
* inline constexpr struct volume : quantity_spec<pow<3>(length)> {} volume;
* inline constexpr struct velocity : quantity_spec<position_vector / duration> {} velocity;
* inline constexpr struct speed : quantity_spec<length / time> {} speed;
* inline constexpr struct force : quantity_spec<mass * acceleration, quantity_character::vector> {} force;
* inline constexpr struct power : quantity_spec<force * velocity, quantity_character::scalar> {} power;
* inline constexpr struct area final : quantity_spec<pow<2>(length)> {} area;
* inline constexpr struct volume final : quantity_spec<pow<3>(length)> {} volume;
* inline constexpr struct velocity final : quantity_spec<position_vector / duration> {} velocity;
* inline constexpr struct speed final : quantity_spec<length / time> {} speed;
* inline constexpr struct force final : quantity_spec<mass * acceleration, quantity_character::vector> {} force;
* inline constexpr struct power final : quantity_spec<force * velocity, quantity_character::scalar> {} power;
* @endcode
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
@@ -260,19 +261,32 @@ struct quantity_spec<Self, Dim, Args...> : detail::quantity_spec_interface<Self>
* @tparam Args optionally a value of a `quantity_character` in case the base quantity should not be scalar
*/
#ifdef MP_UNITS_API_NO_CRTP
template<detail::IntermediateDerivedQuantitySpec auto Eq, one_of<quantity_character> auto... Args>
template<detail::DerivedQuantitySpec auto Eq, one_of<quantity_character> auto... Args>
requires(... && !QuantitySpec<decltype(Args)>)
struct quantity_spec<Eq, Args...> : detail::quantity_spec_interface {
#else
template<typename Self, detail::IntermediateDerivedQuantitySpec auto Eq, one_of<quantity_character> auto... Args>
template<typename Self, detail::DerivedQuantitySpec auto Eq, one_of<quantity_character> auto... Args>
requires(... && !QuantitySpec<decltype(Args)>)
struct quantity_spec<Self, Eq, Args...> : detail::quantity_spec_interface<Self> {
#endif
using _base_type_ = quantity_spec;
static constexpr auto _equation_ = Eq;
static constexpr Dimension auto dimension = Eq.dimension;
static constexpr quantity_character character = detail::quantity_character_init<Args...>(Eq.character);
};
namespace detail {
template<QuantitySpec auto Q, bool = requires { Q._equation_; }>
struct propagate_equation {};
template<QuantitySpec auto Q>
struct propagate_equation<Q, true> {
static constexpr auto _equation_ = Q._equation_;
};
} // namespace detail
/**
* @brief Specialization defining a leaf quantity in the hierarchy
*
@@ -285,10 +299,10 @@ struct quantity_spec<Self, Eq, Args...> : detail::quantity_spec_interface<Self>
* For example:
*
* @code{.cpp}
* inline constexpr struct width : quantity_spec<length> {} width;
* inline constexpr struct height : quantity_spec<length> {} height;
* inline constexpr struct diameter : quantity_spec<width> {} diameter;
* inline constexpr struct position_vector : quantity_spec<length, quantity_character::vector> {} position_vector;
* inline constexpr struct width final : quantity_spec<length> {} width;
* inline constexpr struct height final : quantity_spec<length> {} height;
* inline constexpr struct diameter final : quantity_spec<width> {} diameter;
* inline constexpr struct position_vector final : quantity_spec<length, quantity_character::vector> {} position_vector;
* @endcode
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
@@ -303,13 +317,15 @@ struct quantity_spec<Self, Eq, Args...> : detail::quantity_spec_interface<Self>
#ifdef MP_UNITS_API_NO_CRTP
template<detail::NamedQuantitySpec auto QS, one_of<quantity_character, struct is_kind> auto... Args>
requires(... && !QuantitySpec<decltype(Args)>)
struct quantity_spec<QS, Args...> : decltype(QS) {
struct quantity_spec<QS, Args...> : detail::propagate_equation<QS>, detail::quantity_spec_interface {
#else
template<typename Self, detail::NamedQuantitySpec auto QS, one_of<quantity_character, struct is_kind> auto... Args>
requires(... && !QuantitySpec<decltype(Args)>)
struct quantity_spec<Self, QS, Args...> : decltype(QS) {
struct quantity_spec<Self, QS, Args...> : detail::propagate_equation<QS>, detail::quantity_spec_interface<Self> {
#endif
using _base_type_ = quantity_spec;
static constexpr auto _parent_ = QS;
static constexpr Dimension auto dimension = _parent_.dimension;
static constexpr quantity_character character = detail::quantity_character_init<Args...>(QS.character);
#ifndef MP_UNITS_API_NO_CRTP
@@ -330,6 +346,7 @@ struct quantity_spec<Self, QS, Args...> : decltype(QS) {
#endif
};
// clang-format off
/**
* @brief Specialization defining a leaf derived quantity in the hierarchy and refining paren't equation
*
@@ -343,10 +360,10 @@ struct quantity_spec<Self, QS, Args...> : decltype(QS) {
* For example:
*
* @code{.cpp}
* inline constexpr struct angular_measure : quantity_spec<dimensionless, arc_length / radius, is_kind> {}
* angular_measure; inline constexpr struct velocity : quantity_spec<speed, position_vector / duration> {} velocity;
* inline constexpr struct weight : quantity_spec<force, mass * acceleration_of_free_fall> {} weight;
* inline constexpr struct kinetic_energy : quantity_spec<mechanical_energy, mass * pow<2>(speed)> {} kinetic_energy;
* inline constexpr struct angular_measure final : quantity_spec<dimensionless, arc_length / radius, is_kind> {} angular_measure;
* inline constexpr struct velocity final : quantity_spec<speed, position_vector / duration> {} velocity;
* inline constexpr struct weight final : quantity_spec<force, mass * acceleration_of_free_fall> {} weight;
* inline constexpr struct kinetic_energy final : quantity_spec<mechanical_energy, mass * pow<2>(speed)> {} kinetic_energy;
* @endcode
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
@@ -358,25 +375,50 @@ struct quantity_spec<Self, QS, Args...> : decltype(QS) {
* @tparam Args optionally a value of a `quantity_character` in case the base quantity should not be scalar
* or `is_kind` in case the quantity starts a new hierarchy tree of a kind
*/
// clang-format on
#ifdef MP_UNITS_API_NO_CRTP
template<detail::NamedQuantitySpec auto QS, detail::IntermediateDerivedQuantitySpec auto Eq,
template<detail::NamedQuantitySpec auto QS, detail::DerivedQuantitySpec auto Eq,
one_of<quantity_character, struct is_kind> auto... Args>
requires(!requires { QS._equation_; } || (requires {
QS._equation_;
} && (explicitly_convertible(Eq, QS._equation_)))) && (... && !QuantitySpec<decltype(Args)>)
struct quantity_spec<QS, Eq, Args...> : quantity_spec<QS, Args...> {
struct quantity_spec<QS, Eq, Args...> : detail::quantity_spec_interface {
#else
template<typename Self, detail::NamedQuantitySpec auto QS, detail::IntermediateDerivedQuantitySpec auto Eq,
template<typename Self, detail::NamedQuantitySpec auto QS, detail::DerivedQuantitySpec auto Eq,
one_of<quantity_character, struct is_kind> auto... Args>
requires(!requires { QS._equation_; } || (requires {
QS._equation_;
} && (explicitly_convertible(Eq, QS._equation_)))) && (... && !QuantitySpec<decltype(Args)>)
struct quantity_spec<Self, QS, Eq, Args...> : quantity_spec<Self, QS, Args...> {
struct quantity_spec<Self, QS, Eq, Args...> : detail::quantity_spec_interface<Self> {
#endif
using _base_type_ = quantity_spec;
static constexpr auto _parent_ = QS;
static constexpr auto _equation_ = Eq;
static constexpr Dimension auto dimension = _parent_.dimension;
static constexpr quantity_character character = detail::quantity_character_init<Args...>(Eq.character);
};
namespace detail {
template<detail::DerivedQuantitySpecExpr... Expr>
struct derived_quantity_spec_impl :
#ifdef MP_UNITS_API_NO_CRTP
detail::quantity_spec_interface,
#else
detail::quantity_spec_interface<derived_quantity_spec<Expr...>>,
#endif
detail::expr_fractions<detail::is_dimensionless, Expr...> {
using _base_type_ = derived_quantity_spec_impl;
using _base_ = detail::expr_fractions<detail::is_dimensionless, Expr...>;
static constexpr Dimension auto dimension =
detail::expr_map<detail::to_dimension, derived_dimension, struct dimension_one,
detail::type_list_of_base_dimension_less>(_base_{});
static constexpr quantity_character character =
detail::derived_quantity_character(typename _base_::_num_{}, typename _base_::_den_{});
};
} // namespace detail
/**
* @brief A specification of a derived quantity
@@ -421,22 +463,8 @@ struct quantity_spec<Self, QS, Eq, Args...> : quantity_spec<Self, QS, Args...> {
* @note User should not instantiate this type! It is not exported from the C++ module. The library will
* instantiate this type automatically based on the dimensional arithmetic equation provided by the user.
*/
template<detail::IntermediateDerivedQuantitySpecExpr... Expr>
struct derived_quantity_spec :
#ifdef MP_UNITS_API_NO_CRTP
detail::quantity_spec_interface,
#else
detail::quantity_spec_interface<derived_quantity_spec<Expr...>>,
#endif
detail::expr_fractions<detail::is_dimensionless, Expr...> {
using _base_ = detail::expr_fractions<detail::is_dimensionless, Expr...>;
static constexpr Dimension auto dimension =
detail::expr_map<detail::to_dimension, derived_dimension, struct dimension_one,
detail::type_list_of_base_dimension_less>(_base_{});
static constexpr quantity_character character =
detail::derived_quantity_character(typename _base_::_num_{}, typename _base_::_den_{});
};
template<detail::DerivedQuantitySpecExpr... Expr>
struct derived_quantity_spec final : detail::derived_quantity_spec_impl<Expr...> {};
/**
* @brief Quantity of dimension one
@@ -446,6 +474,13 @@ struct derived_quantity_spec :
*/
MP_UNITS_EXPORT QUANTITY_SPEC(dimensionless, derived_quantity_spec<>{});
namespace detail {
template<>
struct is_dimensionless<struct dimensionless> : std::true_type {};
} // namespace detail
/**
* @brief Quantity kind specifier
*
@@ -454,7 +489,7 @@ MP_UNITS_EXPORT QUANTITY_SPEC(dimensionless, derived_quantity_spec<>{});
namespace detail {
template<typename T>
concept QuantitySpecWithNoSpecifiers = detail::NamedQuantitySpec<T> || detail::IntermediateDerivedQuantitySpec<T>;
concept QuantitySpecWithNoSpecifiers = detail::NamedQuantitySpec<T> || detail::DerivedQuantitySpec<T>;
template<QuantitySpec Q>
[[nodiscard]] consteval QuantitySpec auto get_kind_tree_root(Q q);
@@ -464,7 +499,8 @@ template<QuantitySpec Q>
#ifdef MP_UNITS_API_NO_CRTP
template<typename Q>
requires detail::QuantitySpecWithNoSpecifiers<Q> && (detail::get_kind_tree_root(Q{}) == Q{})
struct kind_of_<Q> : Q {
struct kind_of_<Q> final : Q::_base_type_ {
using _base_type_ = kind_of_;
static constexpr auto _quantity_spec_ = Q{};
};
#else
@@ -476,7 +512,8 @@ template<typename Q>
template<detail::QuantitySpecWithNoSpecifiers Q>
requires(detail::get_kind_tree_root(Q{}) == Q{})
#endif
struct kind_of_<Q> : quantity_spec<kind_of_<Q>, Q{}> {
struct kind_of_<Q> final : quantity_spec<kind_of_<Q>, Q{}>::_base_type_ {
using _base_type_ = kind_of_;
static constexpr auto _quantity_spec_ = Q{};
};
#endif
@@ -487,9 +524,6 @@ inline constexpr kind_of_<MP_UNITS_REMOVE_CONST(decltype(Q))> kind_of;
namespace detail {
template<>
struct is_dimensionless<struct dimensionless> : std::true_type {};
template<QuantitySpec auto... From, QuantitySpec Q>
[[nodiscard]] consteval QuantitySpec auto clone_kind_of(Q q)
{
@@ -556,7 +590,7 @@ template<std::intmax_t Num, std::intmax_t Den = 1, QuantitySpec Q>
return dimensionless;
else if constexpr (detail::ratio{Num, Den} == 1)
return q;
else if constexpr (detail::IntermediateDerivedQuantitySpec<Q>)
else if constexpr (detail::DerivedQuantitySpec<Q>)
return detail::clone_kind_of<Q{}>(
detail::expr_pow<Num, Den, derived_quantity_spec, struct dimensionless, detail::type_list_of_quantity_spec_less>(
detail::remove_kind(q)));
@@ -590,8 +624,6 @@ MP_UNITS_EXPORT_END
namespace detail {
enum class specs_convertible_result : std::int8_t { no, cast, explicit_conversion, yes };
template<QuantitySpec Q>
[[nodiscard]] consteval int get_complexity(Q);
@@ -616,7 +648,7 @@ template<typename Q>
template<QuantitySpec Q>
[[nodiscard]] consteval int get_complexity(Q)
{
if constexpr (detail::IntermediateDerivedQuantitySpec<Q>)
if constexpr (detail::DerivedQuantitySpec<Q>)
return get_complexity(typename Q::_num_{}) + get_complexity(typename Q::_den_{});
else if constexpr (requires { Q::_equation_; })
return 1 + get_complexity(Q::_equation_);
@@ -659,6 +691,8 @@ template<QuantitySpec Q>
return true;
}
enum class specs_convertible_result : std::int8_t { no, cast, explicit_conversion, yes };
template<QuantitySpec Q>
struct explode_to_equation_result {
Q equation;
@@ -709,7 +743,7 @@ explode_result(Q) -> explode_result<Q>;
#endif
template<int Complexity, IntermediateDerivedQuantitySpec Q>
template<int Complexity, DerivedQuantitySpec Q>
[[nodiscard]] consteval auto explode(Q q);
template<int Complexity, NamedQuantitySpec Q>
@@ -771,7 +805,7 @@ template<int Complexity, QuantitySpec Q>
return explode_result{dimensionless};
}
template<int Complexity, IntermediateDerivedQuantitySpec Q>
template<int Complexity, DerivedQuantitySpec Q>
[[nodiscard]] consteval auto explode(Q q)
{
constexpr auto c = get_complexity(Q{});
@@ -1304,7 +1338,7 @@ template<typename... DensTo>
return specs_convertible_result::yes;
}
template<IntermediateDerivedQuantitySpec From, IntermediateDerivedQuantitySpec To>
template<DerivedQuantitySpec From, DerivedQuantitySpec To>
[[nodiscard]] consteval specs_convertible_result are_ingredients_convertible(From, To)
{
return are_ingredients_convertible(type_list_sort<typename From::_num_, type_list_of_ingredients_less>{},
@@ -1313,7 +1347,7 @@ template<IntermediateDerivedQuantitySpec From, IntermediateDerivedQuantitySpec T
type_list_sort<typename To::_den_, type_list_of_ingredients_less>{});
}
template<IntermediateDerivedQuantitySpec From, NamedQuantitySpec To>
template<DerivedQuantitySpec From, NamedQuantitySpec To>
[[nodiscard]] consteval specs_convertible_result are_ingredients_convertible(From, To)
{
return are_ingredients_convertible(type_list_sort<typename From::_num_, type_list_of_ingredients_less>{},
@@ -1321,7 +1355,7 @@ template<IntermediateDerivedQuantitySpec From, NamedQuantitySpec To>
type_list<To>{}, type_list<>{});
}
template<NamedQuantitySpec From, IntermediateDerivedQuantitySpec To>
template<NamedQuantitySpec From, DerivedQuantitySpec To>
[[nodiscard]] consteval specs_convertible_result are_ingredients_convertible(From, To)
{
return are_ingredients_convertible(type_list<From>{}, type_list<>{},
@@ -1353,8 +1387,8 @@ template<NamedQuantitySpec From, NamedQuantitySpec To>
using enum specs_convertible_result;
if constexpr (have_common_base(From{}, To{})) {
if constexpr (std::derived_from<From, To>) return yes;
if constexpr (std::derived_from<To, From>) return explicit_conversion;
if constexpr (is_child_of(From{}, To{})) return yes;
if constexpr (is_child_of(To{}, From{})) return explicit_conversion;
if constexpr (get_kind(From{}) == get_kind(To{})) return cast;
return no;
} else if constexpr (get_kind(From{}) != get_kind(To{}))
@@ -1385,9 +1419,9 @@ template<QuantitySpec From, QuantitySpec To>
return yes;
else if constexpr (NamedQuantitySpec<From> && NamedQuantitySpec<To>) {
return convertible_named(from, to);
} else if constexpr (IntermediateDerivedQuantitySpec<From> && IntermediateDerivedQuantitySpec<To>) {
} else if constexpr (DerivedQuantitySpec<From> && DerivedQuantitySpec<To>) {
return are_ingredients_convertible(from, to);
} else if constexpr (IntermediateDerivedQuantitySpec<From>) {
} else if constexpr (DerivedQuantitySpec<From>) {
auto res = explode<get_complexity(to)>(from);
if constexpr (NamedQuantitySpec<decltype(res.quantity)>)
return convertible_impl(res.quantity, to);
@@ -1396,7 +1430,7 @@ template<QuantitySpec From, QuantitySpec To>
return min(eq.result, convertible_impl(res.quantity, eq.equation));
} else
return are_ingredients_convertible(from, to);
} else if constexpr (IntermediateDerivedQuantitySpec<To>) {
} else if constexpr (DerivedQuantitySpec<To>) {
auto res = explode<get_complexity(from)>(to);
if constexpr (NamedQuantitySpec<decltype(res.quantity)>)
return min(res.result, convertible_impl(from, res.quantity));
@@ -1473,7 +1507,7 @@ template<QuantitySpec Q>
return q;
} else if constexpr (requires { Q::_parent_; }) {
return get_kind_tree_root(Q::_parent_);
} else if constexpr (detail::IntermediateDerivedQuantitySpec<Q>) {
} else if constexpr (detail::DerivedQuantitySpec<Q>) {
return detail::expr_map<detail::to_kind, derived_quantity_spec, struct dimensionless,
detail::type_list_of_quantity_spec_less>(q);
} else {
@@ -1511,11 +1545,11 @@ template<QuantitySpec Q1, QuantitySpec Q2>
else if constexpr (detail::NestedQuantityKindSpecOf<Q2{}, Q1{}>)
return detail::remove_kind(q2);
else if constexpr ((detail::QuantityKindSpec<Q1> && !detail::QuantityKindSpec<Q2>) ||
(detail::IntermediateDerivedQuantitySpec<QQ1> && detail::NamedQuantitySpec<QQ2> &&
(detail::DerivedQuantitySpec<QQ1> && detail::NamedQuantitySpec<QQ2> &&
implicitly_convertible(Q1{}, Q2{})))
return q2;
else if constexpr ((!detail::QuantityKindSpec<Q1> && detail::QuantityKindSpec<Q2>) ||
(detail::NamedQuantitySpec<QQ1> && detail::IntermediateDerivedQuantitySpec<QQ2> &&
(detail::NamedQuantitySpec<QQ1> && detail::DerivedQuantitySpec<QQ2> &&
implicitly_convertible(Q2{}, Q1{})))
return q1;
else if constexpr (detail::have_common_base(Q1{}, Q2{}))
@@ -64,25 +64,14 @@ template<typename T>
inline constexpr bool is_derived_from_specialization_of_quantity_spec =
requires(T* t) { to_base_specialization_of_quantity_spec(t); };
template<typename T>
inline constexpr bool is_specialization_of_quantity_spec = false;
#ifdef MP_UNITS_API_NO_CRTP
template<auto... Args>
inline constexpr bool is_specialization_of_quantity_spec<quantity_spec<Args...>> = true;
#else
template<typename T, auto... Args>
inline constexpr bool is_specialization_of_quantity_spec<quantity_spec<T, Args...>> = true;
#endif
/**
* @brief Concept matching all named quantity specification types
*
* Satisfied by all types that derive from `quantity_spec`.
*/
template<typename T>
concept NamedQuantitySpec = is_derived_from_specialization_of_quantity_spec<T> &&
(!is_specialization_of_quantity_spec<T>)&&(!QuantityKindSpec<T>);
concept NamedQuantitySpec =
is_derived_from_specialization_of_quantity_spec<T> && std::is_final_v<T> && (!QuantityKindSpec<T>);
template<typename T>
struct is_dimensionless : std::false_type {};
@@ -101,13 +90,12 @@ inline constexpr bool is_per_of_quantity_specs<per<Ts...>> =
(... && (NamedQuantitySpec<Ts> || is_dimensionless<Ts>::value || is_power_of_quantity_spec<Ts>));
template<typename T>
concept IntermediateDerivedQuantitySpecExpr =
detail::NamedQuantitySpec<T> || detail::is_dimensionless<T>::value || detail::is_power_of_quantity_spec<T> ||
detail::is_per_of_quantity_specs<T>;
concept DerivedQuantitySpecExpr = detail::NamedQuantitySpec<T> || detail::is_dimensionless<T>::value ||
detail::is_power_of_quantity_spec<T> || detail::is_per_of_quantity_specs<T>;
} // namespace detail
template<detail::IntermediateDerivedQuantitySpecExpr... Expr>
template<detail::DerivedQuantitySpecExpr... Expr>
struct derived_quantity_spec;
namespace detail {
@@ -121,7 +109,7 @@ namespace detail {
* explicitly not supported here.
*/
template<typename T>
concept IntermediateDerivedQuantitySpec =
concept DerivedQuantitySpec =
is_specialization_of<T, derived_quantity_spec> ||
(QuantityKindSpec<T> &&
is_specialization_of<std::remove_const_t<decltype(T::_quantity_spec_)>, derived_quantity_spec>);
@@ -130,20 +118,21 @@ concept IntermediateDerivedQuantitySpec =
MP_UNITS_EXPORT template<typename T>
concept QuantitySpec =
detail::NamedQuantitySpec<T> || detail::IntermediateDerivedQuantitySpec<T> || detail::QuantityKindSpec<T>;
concept QuantitySpec = detail::NamedQuantitySpec<T> || detail::DerivedQuantitySpec<T> || detail::QuantityKindSpec<T>;
MP_UNITS_EXPORT template<QuantitySpec Q>
[[nodiscard]] consteval detail::QuantityKindSpec auto get_kind(Q q);
namespace detail {
template<auto To, auto From>
concept NestedQuantityKindSpecOf =
QuantitySpec<decltype(From)> && QuantitySpec<decltype(To)> && get_kind(From) != get_kind(To) &&
std::derived_from<decltype(To), std::remove_const_t<decltype(get_kind(From)._quantity_spec_)>>;
template<QuantitySpec Child, QuantitySpec Parent>
[[nodiscard]] consteval bool is_child_of(Child ch, Parent p);
}
template<auto To, auto From>
concept NestedQuantityKindSpecOf = QuantitySpec<decltype(From)> && QuantitySpec<decltype(To)> &&
get_kind(From) != get_kind(To) && is_child_of(To, get_kind(From)._quantity_spec_);
} // namespace detail
MP_UNITS_EXPORT template<typename T, auto QS>
concept QuantitySpecOf =
@@ -31,6 +31,7 @@
#include <mp-units/ext/fixed_string.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export
#include <cstddef>
#include <cstdint>
@@ -45,10 +45,10 @@ namespace mp_units {
* @code{.cpp}
* // hypothetical natural system of units for c=1
*
* inline constexpr struct second : named_unit<"s"> {} second;
* inline constexpr struct minute : named_unit<"min", mag<60> * second> {} minute;
* inline constexpr struct gram : named_unit<"g"> {} gram;
* inline constexpr struct kilogram : decltype(si::kilo<gram>) {} kilogram;
* inline constexpr struct second final : named_unit<"s"> {} second;
* inline constexpr struct minute final : named_unit<"min", mag<60> * second> {} minute;
* inline constexpr struct gram final : named_unit<"g"> {} gram;
* inline constexpr auto kilogram = si::kilo<gram>;
*
* inline constexpr struct time : system_reference<isq::time, second> {} time;
* inline constexpr struct length : system_reference<isq::length, second> {} length;
+63 -78
View File
@@ -41,6 +41,7 @@
#include <mp-units/framework/unit_concepts.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <array>
#include <cstdint>
#include <iterator>
@@ -62,6 +63,13 @@ struct propagate_point_origin<U, true> {
static constexpr auto point_origin = U::point_origin;
};
template<Magnitude auto M, Unit U>
struct scaled_unit_impl : detail::propagate_point_origin<U> {
using _base_type_ = scaled_unit_impl; // exposition only
static constexpr MP_UNITS_CONSTRAINED_AUTO_WORKAROUND(Magnitude) auto mag = M;
static constexpr U reference_unit{};
};
} // namespace detail
/**
@@ -74,10 +82,7 @@ struct propagate_point_origin<U, true> {
* instantiate this type automatically based on the unit arithmetic equation provided by the user.
*/
template<Magnitude auto M, Unit U>
struct scaled_unit : detail::propagate_point_origin<U> {
static constexpr MP_UNITS_CONSTRAINED_AUTO_WORKAROUND(Magnitude) auto mag = M;
static constexpr U reference_unit{};
};
struct scaled_unit final : detail::scaled_unit_impl<M, U> {};
namespace detail {
@@ -100,12 +105,12 @@ inline constexpr bool is_specialization_of_scaled_unit<scaled_unit<M, U>> = true
* For example:
*
* @code{.cpp}
* inline constexpr struct second : named_unit<"s", time> {} second;
* inline constexpr struct metre : named_unit<"m", length> {} metre;
* inline constexpr struct hertz : named_unit<"Hz", inverse(second)> {} hertz;
* inline constexpr struct newton : named_unit<"N", kilogram * metre / square(second)> {} newton;
* inline constexpr struct degree_Celsius : named_unit<{u8"°C", "`C"}, kelvin> {} degree_Celsius;
* inline constexpr struct minute : named_unit<"min", mag<60> * second> {} minute;
* inline constexpr struct second final : named_unit<"s", kind_of<time>> {} second;
* inline constexpr struct metre final : named_unit<"m", kind_of<length> {} metre;
* inline constexpr struct hertz final : named_unit<"Hz", inverse(second), kind_of<frequency>> {} hertz;
* inline constexpr struct newton final : named_unit<"N", kilogram * metre / square(second)> {} newton;
* inline constexpr struct degree_Celsius final : named_unit<{u8"°C", "`C"}, kelvin, zeroth_degree_Celsius> {}
* degree_Celsius; inline constexpr struct minute final : named_unit<"min", mag<60> * second> {} minute;
* @endcode
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
@@ -137,6 +142,7 @@ struct named_unit;
template<symbol_text Symbol, detail::QuantityKindSpec auto QS>
requires(!Symbol.empty()) && detail::BaseDimension<std::remove_const_t<decltype(QS.dimension)>>
struct named_unit<Symbol, QS> {
using _base_type_ = named_unit; // exposition only
static constexpr auto symbol = Symbol; ///< Unique base unit identifier
static constexpr auto quantity_spec = QS;
};
@@ -144,6 +150,7 @@ struct named_unit<Symbol, QS> {
template<symbol_text Symbol, detail::QuantityKindSpec auto QS, PointOrigin auto PO>
requires(!Symbol.empty()) && detail::BaseDimension<std::remove_const_t<decltype(QS.dimension)>>
struct named_unit<Symbol, QS, PO> {
using _base_type_ = named_unit; // exposition only
static constexpr auto symbol = Symbol; ///< Unique base unit identifier
static constexpr auto quantity_spec = QS;
static constexpr auto point_origin = PO;
@@ -162,6 +169,7 @@ struct named_unit<Symbol, QS, PO> {
template<symbol_text Symbol>
requires(!Symbol.empty())
struct named_unit<Symbol> {
using _base_type_ = named_unit; // exposition only
static constexpr auto symbol = Symbol; ///< Unique base unit identifier
};
@@ -175,13 +183,15 @@ struct named_unit<Symbol> {
*/
template<symbol_text Symbol, Unit auto U>
requires(!Symbol.empty())
struct named_unit<Symbol, U> : decltype(U) {
struct named_unit<Symbol, U> : decltype(U)::_base_type_ {
using _base_type_ = named_unit; // exposition only
static constexpr auto symbol = Symbol; ///< Unique unit identifier
};
template<symbol_text Symbol, Unit auto U, PointOrigin auto PO>
requires(!Symbol.empty())
struct named_unit<Symbol, U, PO> : decltype(U) {
struct named_unit<Symbol, U, PO> : decltype(U)::_base_type_ {
using _base_type_ = named_unit; // exposition only
static constexpr auto symbol = Symbol; ///< Unique unit identifier
static constexpr auto point_origin = PO;
};
@@ -197,14 +207,16 @@ struct named_unit<Symbol, U, PO> : decltype(U) {
*/
template<symbol_text Symbol, AssociatedUnit auto U, detail::QuantityKindSpec auto QS>
requires(!Symbol.empty()) && (QS.dimension == detail::get_associated_quantity(U).dimension)
struct named_unit<Symbol, U, QS> : decltype(U) {
struct named_unit<Symbol, U, QS> : decltype(U)::_base_type_ {
using _base_type_ = named_unit; // exposition only
static constexpr auto symbol = Symbol; ///< Unique unit identifier
static constexpr auto quantity_spec = QS;
};
template<symbol_text Symbol, AssociatedUnit auto U, detail::QuantityKindSpec auto QS, PointOrigin auto PO>
requires(!Symbol.empty()) && (QS.dimension == detail::get_associated_quantity(U).dimension)
struct named_unit<Symbol, U, QS, PO> : decltype(U) {
struct named_unit<Symbol, U, QS, PO> : decltype(U)::_base_type_ {
using _base_type_ = named_unit; // exposition only
static constexpr auto symbol = Symbol; ///< Unique unit identifier
static constexpr auto quantity_spec = QS;
static constexpr auto point_origin = PO;
@@ -225,7 +237,7 @@ struct named_unit<Symbol, U, QS, PO> : decltype(U) {
* template<PrefixableUnit auto U>
* inline constexpr kilo_<U> kilo;
*
* inline constexpr struct kilogram : decltype(si::kilo<gram>) {} kilogram;
* inline constexpr auto kilogram = si::kilo<gram>;
* @endcode
*
* @tparam Symbol a prefix text to prepend to a unit symbol
@@ -234,7 +246,8 @@ struct named_unit<Symbol, U, QS, PO> : decltype(U) {
*/
MP_UNITS_EXPORT template<symbol_text Symbol, Magnitude auto M, PrefixableUnit auto U>
requires(!Symbol.empty())
struct prefixed_unit : decltype(M * U) {
struct prefixed_unit : decltype(M * U)::_base_type_ {
using _base_type_ = prefixed_unit; // exposition only
static constexpr auto symbol = Symbol + U.symbol;
};
@@ -243,6 +256,11 @@ namespace detail {
template<typename T>
struct is_one : std::false_type {};
template<DerivedUnitExpr... Expr>
struct derived_unit_impl : detail::expr_fractions<detail::is_one, Expr...> {
using _base_type_ = derived_unit_impl; // exposition only
};
} // namespace detail
/**
@@ -291,7 +309,7 @@ struct is_one : std::false_type {};
* instantiate this type automatically based on the unit arithmetic equation provided by the user.
*/
template<detail::DerivedUnitExpr... Expr>
struct derived_unit : detail::expr_fractions<detail::is_one, Expr...> {};
struct derived_unit final : detail::derived_unit_impl<Expr...> {};
/**
* @brief Unit one
@@ -299,7 +317,7 @@ struct derived_unit : detail::expr_fractions<detail::is_one, Expr...> {};
* Unit of a dimensionless quantity.
*/
// clang-format off
MP_UNITS_EXPORT inline constexpr struct one : derived_unit<> {} one;
MP_UNITS_EXPORT inline constexpr struct one final : detail::derived_unit_impl<> {} one;
// clang-format on
namespace detail {
@@ -334,9 +352,6 @@ canonical_unit(M, U) -> canonical_unit<M, U>;
#endif
template<Unit T, symbol_text Symbol, detail::QuantityKindSpec auto Q, auto... Args>
[[nodiscard]] consteval auto get_canonical_unit_impl(T t, const named_unit<Symbol, Q, Args...>&);
template<Unit T, symbol_text Symbol, auto... Args>
[[nodiscard]] consteval auto get_canonical_unit_impl(T t, const named_unit<Symbol, Args...>&);
@@ -347,21 +362,15 @@ template<typename T, typename F, int Num, int... Den>
[[nodiscard]] consteval auto get_canonical_unit_impl(T, const power<F, Num, Den...>&);
template<Unit T, typename... Expr>
[[nodiscard]] consteval auto get_canonical_unit_impl(T, const derived_unit<Expr...>&);
[[nodiscard]] consteval auto get_canonical_unit_impl(T, const derived_unit_impl<Expr...>&);
template<Unit T, auto M, typename U>
[[nodiscard]] consteval auto get_canonical_unit_impl(T, const scaled_unit<M, U>&)
[[nodiscard]] consteval auto get_canonical_unit_impl(T, const scaled_unit_impl<M, U>&)
{
auto base = get_canonical_unit_impl(U{}, U{});
return canonical_unit{M * base.mag, base.reference_unit};
}
template<Unit T, symbol_text Symbol, detail::QuantityKindSpec auto Q, auto... Args>
[[nodiscard]] consteval auto get_canonical_unit_impl(T t, const named_unit<Symbol, Q, Args...>&)
{
return canonical_unit{mag<1>, t};
}
template<Unit T, symbol_text Symbol, auto... Args>
[[nodiscard]] consteval auto get_canonical_unit_impl(T t, const named_unit<Symbol, Args...>&)
{
@@ -405,7 +414,7 @@ template<typename... Us>
}
template<Unit T, typename... Expr>
[[nodiscard]] consteval auto get_canonical_unit_impl(T, const derived_unit<Expr...>&)
[[nodiscard]] consteval auto get_canonical_unit_impl(T, const derived_unit_impl<Expr...>&)
{
auto num = get_canonical_unit_impl(typename derived_unit<Expr...>::_num_{});
auto den = get_canonical_unit_impl(typename derived_unit<Expr...>::_den_{});
@@ -493,43 +502,13 @@ MP_UNITS_EXPORT_END
namespace detail {
[[nodiscard]] consteval bool have_same_canonical_reference_unit_impl(...) { return false; }
template<symbol_text Symbol, auto... D>
[[nodiscard]] consteval bool have_same_canonical_reference_unit_impl(const named_unit<Symbol, D...>&,
const named_unit<Symbol, D...>&)
template<Unit U1, Unit U2>
[[nodiscard]] consteval bool have_same_canonical_reference_unit(U1 u1, U2 u2)
{
return true;
}
template<typename F1, typename F2, auto... Vs>
[[nodiscard]] consteval bool have_same_canonical_reference_unit_impl(const power<F1, Vs...>&, const power<F2, Vs...>&)
{
return have_same_canonical_reference_unit_impl(F1{}, F2{});
}
template<typename... Us1, typename... Us2>
requires(sizeof...(Us1) == sizeof...(Us2))
[[nodiscard]] consteval bool have_same_canonical_reference_unit_impl(const type_list<Us1...>&, const type_list<Us2...>&)
{
return (... && have_same_canonical_reference_unit_impl(Us1{}, Us2{}));
}
template<typename... Expr1, typename... Expr2>
[[nodiscard]] consteval bool have_same_canonical_reference_unit_impl(const derived_unit<Expr1...>&,
const derived_unit<Expr2...>&)
{
return have_same_canonical_reference_unit_impl(typename derived_unit<Expr1...>::_num_{},
typename derived_unit<Expr2...>::_num_{}) &&
have_same_canonical_reference_unit_impl(typename derived_unit<Expr1...>::_den_{},
typename derived_unit<Expr2...>::_den_{});
}
[[nodiscard]] consteval bool have_same_canonical_reference_unit(Unit auto u1, Unit auto u2)
{
auto canonical_lhs = get_canonical_unit(u1);
auto canonical_rhs = get_canonical_unit(u2);
return have_same_canonical_reference_unit_impl(canonical_lhs.reference_unit, canonical_rhs.reference_unit);
if constexpr (is_same_v<U1, U2>)
return true;
else
return is_same_v<decltype(get_canonical_unit(u1).reference_unit), decltype(get_canonical_unit(u2).reference_unit)>;
}
} // namespace detail
@@ -621,17 +600,21 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Unit U>
// common dimensionless units
// clang-format off
inline constexpr struct percent : named_unit<"%", mag_ratio<1, 100> * one> {} percent;
inline constexpr struct per_mille : named_unit<symbol_text{u8"‰", "%o"}, mag_ratio<1, 1000> * one> {} per_mille;
inline constexpr struct parts_per_million : named_unit<"ppm", mag_ratio<1, 1'000'000> * one> {} parts_per_million;
inline constexpr struct percent final : named_unit<"%", mag_ratio<1, 100> * one> {} percent;
inline constexpr struct per_mille final : named_unit<symbol_text{u8"‰", "%o"}, mag_ratio<1, 1000> * one> {} per_mille;
inline constexpr struct parts_per_million final : named_unit<"ppm", mag_ratio<1, 1'000'000> * one> {} parts_per_million;
inline constexpr auto ppm = parts_per_million;
// clang-format on
// convertible_to
[[nodiscard]] consteval bool convertible(Unit auto from, Unit auto to)
template<Unit U1, Unit U2>
[[nodiscard]] consteval bool convertible(U1 from, U2 to)
{
return detail::have_same_canonical_reference_unit(from, to);
if constexpr (is_same_v<U1, U2>)
return true;
else
return detail::have_same_canonical_reference_unit(from, to);
}
// Common unit
@@ -641,10 +624,12 @@ template<Unit U1, Unit U2>
[[nodiscard]] consteval Unit auto common_unit(U1 u1, U2 u2)
requires(detail::have_same_canonical_reference_unit(u1, u2))
{
if constexpr (U1{} == U2{}) {
if constexpr (std::derived_from<U1, U2>)
if constexpr (is_same_v<U1, U2>)
return u1;
else if constexpr (U1{} == U2{}) {
if constexpr (std::derived_from<U1, typename U2::_base_type_>)
return u1;
else if constexpr (std::derived_from<U2, U1>)
else if constexpr (std::derived_from<U2, typename U1::_base_type_>)
return u2;
else
// TODO Check if there is a better choice here
@@ -738,7 +723,7 @@ constexpr Out unit_symbol_impl(Out out, U, const unit_symbol_formatting& fmt, bo
}
template<typename CharT, std::output_iterator<CharT> Out, auto M, typename U>
constexpr Out unit_symbol_impl(Out out, const scaled_unit<M, U>& u, const unit_symbol_formatting& fmt,
constexpr Out unit_symbol_impl(Out out, const scaled_unit_impl<M, U>& u, const unit_symbol_formatting& fmt,
bool negative_power)
{
if constexpr (M == mag<1>) {
@@ -807,13 +792,13 @@ constexpr Out unit_symbol_impl(Out out, const type_list<Nums...>& nums, const ty
}
template<typename CharT, std::output_iterator<CharT> Out, typename... Expr>
constexpr Out unit_symbol_impl(Out out, const derived_unit<Expr...>&, const unit_symbol_formatting& fmt,
constexpr Out unit_symbol_impl(Out out, const derived_unit_impl<Expr...>&, const unit_symbol_formatting& fmt,
bool negative_power)
{
(void)negative_power;
MP_UNITS_EXPECTS(negative_power == false);
return unit_symbol_impl<CharT>(out, typename derived_unit<Expr...>::_num_{}, typename derived_unit<Expr...>::_den_{},
fmt);
return unit_symbol_impl<CharT>(out, typename derived_unit_impl<Expr...>::_num_{},
typename derived_unit_impl<Expr...>::_den_{}, fmt);
}
} // namespace detail
@@ -33,9 +33,8 @@ namespace mp_units {
namespace detail {
// do not refactor below to a variable template - GCC-11 does not like it
template<typename T>
struct is_unit : std::false_type {};
inline constexpr bool is_unit = false;
} // namespace detail
@@ -45,7 +44,7 @@ struct is_unit : std::false_type {};
* Satisfied by all unit types provided by the library.
*/
MP_UNITS_EXPORT template<typename T>
concept Unit = detail::is_unit<T>::value;
concept Unit = detail::is_unit<T>;
template<Magnitude auto M, Unit U>
struct scaled_unit;
@@ -53,6 +52,8 @@ struct scaled_unit;
MP_UNITS_EXPORT template<symbol_text Symbol, auto...>
struct named_unit;
MP_UNITS_EXPORT struct one;
namespace detail {
template<symbol_text Symbol, auto... Args>
@@ -127,25 +128,17 @@ void is_unit_impl(const scaled_unit<M, U>*);
template<symbol_text Symbol, auto... Args>
void is_unit_impl(const named_unit<Symbol, Args...>*);
template<symbol_text Symbol, auto M, auto U>
void is_unit_impl(const prefixed_unit<Symbol, M, U>*);
template<typename... Expr>
void is_unit_impl(const derived_unit<Expr...>*);
template<typename T>
inline constexpr bool is_specialization_of_unit = false;
template<symbol_text Symbol, auto... Args>
inline constexpr bool is_specialization_of_unit<named_unit<Symbol, Args...>> = true;
void is_unit_impl(const one*);
template<typename T>
inline constexpr bool is_specialization_of_prefixed_unit = false;
template<symbol_text Symbol, Magnitude auto M, PrefixableUnit auto U>
inline constexpr bool is_specialization_of_prefixed_unit<prefixed_unit<Symbol, M, U>> = true;
template<typename T>
requires requires(T* t) { is_unit_impl(t); } && (!is_specialization_of_named_unit<T>) &&
(!is_specialization_of_prefixed_unit<T>)
struct is_unit<T> : std::true_type {};
requires requires(T* t) { is_unit_impl(t); } && std::is_final_v<T>
inline constexpr bool is_unit<T> = true;
template<Unit U>
[[nodiscard]] consteval bool has_associated_quantity(U);
@@ -197,7 +190,8 @@ concept UnitOf =
namespace detail {
[[nodiscard]] consteval bool have_same_canonical_reference_unit(Unit auto u1, Unit auto u2);
template<Unit U1, Unit U2>
[[nodiscard]] consteval bool have_same_canonical_reference_unit(U1 u1, U2 u2);
}
@@ -210,7 +204,7 @@ namespace detail {
MP_UNITS_EXPORT template<typename U, auto U2, auto QS>
concept UnitCompatibleWith =
Unit<U> && Unit<MP_UNITS_REMOVE_CONST(decltype(U2))> && QuantitySpec<MP_UNITS_REMOVE_CONST(decltype(QS))> &&
(!AssociatedUnit<U> || UnitOf<U, QS>)&&detail::have_same_canonical_reference_unit(U{}, U2);
(!AssociatedUnit<U> || UnitOf<U, QS>)&&(detail::have_same_canonical_reference_unit(U{}, U2));
} // namespace mp_units
+22
View File
@@ -1,3 +1,25 @@
// 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.
module;
#include <mp-units/bits/core_gmf.h>