feat: system's definition highly simplified by removing the need for a system_reference for most cases

This commit is contained in:
Mateusz Pusz committed 2022-10-25 07:27:08 +02:00
1 parent 8dcdc7bdac
commit cb861dd81a
24 files changed
+779 -780

No files matched your search

+5 -1
View File
@@ -102,7 +102,11 @@ template<typename T>
concept AliasUnit = requires(T* t) { detail::to_base_alias_unit(t); };
// BaseDimension
template<basic_fixed_string Symbol, NamedUnit U>
#ifdef __cpp_explicit_this_parameter__
template<basic_fixed_string Symbol>
#else
template<typename Self, basic_fixed_string Symbol>
#endif
struct base_dimension;
namespace detail {
@@ -457,6 +457,9 @@ template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To
*
* @tparam Num Exponent numerator
* @tparam Den Exponent denominator
* @tparam To destination type list to put the result to
* @tparam OneType type that represents the value `1`
* @tparam Pred binary less then predicate
* @tparam T Expression being the base of the operation
*/
template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To, typename OneType,
@@ -467,6 +470,44 @@ template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To
return expr_pow_impl<Num, Den, To, OneType, Pred>(typename T::_num_{}, typename T::_den_{});
}
// expr_map
template<typename T, template<typename...> typename Proj>
struct expr_type_map {
using type = Proj<T>;
};
template<typename F, int... Ints, template<typename...> typename Proj>
struct expr_type_map<power<F, Ints...>, Proj> {
using type = power<Proj<F>, Ints...>;
};
template<template<typename...> typename Proj, template<typename...> typename To, typename OneType,
template<typename, typename> typename Pred, typename... Nums, typename... Dens>
[[nodiscard]] consteval auto expr_map_impl(type_list<Nums...>, type_list<Dens...>)
{
using nums = type_list_sort<type_list<typename expr_type_map<Nums, Proj>::type...>, Pred>;
using dens = type_list_sort<type_list<typename expr_type_map<Dens, Proj>::type...>, Pred>;
return detail::get_optimized_expression<nums, dens, OneType, Pred, To>();
}
/**
* @brief Maps contents of one expression template to another resulting in a different type list
*
* @tparam Proj Projection to be used for mapping
* @tparam To destination type list to put the result to
* @tparam OneType type that represents the value `1`
* @tparam Pred binary less then predicate
* @tparam T expression template to map from
*/
template<template<typename...> typename Proj, template<typename...> typename To, typename OneType,
template<typename, typename> typename Pred, typename T>
[[nodiscard]] consteval auto expr_map(T)
{
return expr_map_impl<Proj, To, OneType, Pred>(typename T::_num_{}, typename T::_den_{});
}
} // namespace detail
} // namespace units
+180 -85
View File
@@ -25,64 +25,10 @@
#include <units/bits/expression_template.h>
#include <units/bits/external/fixed_string.h>
#include <units/bits/external/type_traits.h>
#include <units/unit.h>
namespace units {
/**
* @brief A dimension of a base quantity
*
* Base quantity is a quantity in a conventionally chosen subset of a given system of quantities, where no quantity
* in the subset can be expressed in terms of the other quantities within that subset. They are referred to as
* being mutually independent since a base quantity cannot be expressed as a product of powers of the other base
* quantities.
*
* Symbol template parameters is an unique identifier of the base dimension. The same identifiers can be multiplied
* and divided which will result with an adjustment of its factor in an exponent of a derived_dimension
* (in case of zero the dimension will be simplified and removed from further analysis of current expresion).
*
* User should derive a strong type from this class template rather than use it directly in the source code.
* For example:
*
* @code{.cpp}
* inline constexpr struct length_dim : base_dimension<"L"> {} length_dim;
* inline constexpr struct time_dim : base_dimension<"T"> {} time_dim;
* inline constexpr struct mass_dim : base_dimension<"M"> {} mass_dim;
* @endcode
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
* Besides defining them user never works with the dimension types in the source code. All operations
* are done on the objects. Contrarily, the dimension types are the only one visible in the compilation
* errors. Having them of the same names improves user experience and somehow blurs those separate domains.
*
* @tparam Symbol an unique identifier of the base dimension used to provide dimensional analysis support
*/
template<basic_fixed_string Symbol>
struct base_dimension {
static constexpr auto symbol = Symbol; ///< Unique base dimension identifier
};
namespace detail {
template<basic_fixed_string Symbol>
void to_base_base_dimension(const volatile base_dimension<Symbol>*);
template<typename T>
inline constexpr bool is_specialization_of_base_dimension = false;
template<basic_fixed_string Symbol>
inline constexpr bool is_specialization_of_base_dimension<base_dimension<Symbol>> = true;
} // namespace detail
/**
* @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 = requires(T* t) { detail::to_base_base_dimension(t); } &&
(!detail::is_specialization_of_base_dimension<T>);
namespace detail {
template<typename T>
@@ -107,6 +53,66 @@ concept DerivedDimension = detail::is_derived_dimension<T>;
template<typename T>
concept Dimension = BaseDimension<T> || DerivedDimension<T>;
namespace detail {
[[nodiscard]] consteval Dimension auto get_dimension_for(Unit auto);
}
template<Dimension D, Unit U>
struct reference;
/**
* @brief A dimension of a base quantity
*
* Base quantity is a quantity in a conventionally chosen subset of a given system of quantities, where no quantity
* in the subset can be expressed in terms of the other quantities within that subset. They are referred to as
* being mutually independent since a base quantity cannot be expressed as a product of powers of the other base
* quantities.
*
* Symbol template parameters is an unique identifier of the base dimension. The same identifiers can be multiplied
* and divided which will result with an adjustment of its factor in an exponent of a derived_dimension
* (in case of zero the dimension will be simplified and removed from further analysis of current expresion).
*
* User should derive a strong type from this class template rather than use it directly in the source code.
* For example:
*
* @code{.cpp}
* inline constexpr struct length : base_dimension<"L"> {} length;
* inline constexpr struct time : base_dimension<"T"> {} time;
* inline constexpr struct mass : base_dimension<"M"> {} mass;
* @endcode
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
* Besides defining them user never works with the dimension types in the source code. All operations
* are done on the objects. Contrarily, the dimension types are the only one visible in the compilation
* errors. Having them of the same names improves user experience and somehow blurs those separate domains.
*
* @tparam Symbol an unique identifier of the base dimension used to provide dimensional analysis support
*/
#ifdef __cpp_explicit_this_parameter__
template<basic_fixed_string Symbol>
#else
template<typename Self, basic_fixed_string Symbol>
#endif
struct base_dimension {
static constexpr auto symbol = Symbol; ///< Unique base dimension identifier
#ifdef __cpp_explicit_this_parameter__
template<Unit U, typename Self>
requires(convertible(Self{}, detail::get_dimension_for(U{})))
[[nodiscard]] constexpr reference<Self, U> operator[](this const Self, U)
#else
template<Unit U>
requires(convertible(Self{}, detail::get_dimension_for(U{})))
[[nodiscard]] constexpr reference<Self, U> operator[](U) const
#endif
{
return {};
}
};
namespace detail {
template<BaseDimension Lhs, BaseDimension Rhs>
@@ -116,12 +122,13 @@ template<typename T1, typename T2>
using type_list_of_base_dimension_less = expr_less<T1, T2, base_dimension_less>;
template<typename T>
inline constexpr bool is_one_dim = false;
inline constexpr bool is_dimensionless = false;
template<typename T>
inline constexpr bool is_power_of_dim =
requires {
requires is_specialization_of_power<T> && (BaseDimension<typename T::factor> || is_one_dim<typename T::factor>);
requires is_specialization_of_power<T> &&
(BaseDimension<typename T::factor> || is_dimensionless<typename T::factor>);
};
template<typename T>
@@ -129,15 +136,15 @@ inline constexpr bool is_per_of_dims = false;
template<typename... Ts>
inline constexpr bool is_per_of_dims<per<Ts...>> =
(... && (BaseDimension<Ts> || is_one_dim<Ts> || is_power_of_dim<Ts>));
(... && (BaseDimension<Ts> || is_dimensionless<Ts> || is_power_of_dim<Ts>));
} // namespace detail
template<typename T>
concept DerivedDimensionSpec =
BaseDimension<T> || detail::is_one_dim<T> || detail::is_power_of_dim<T> || detail::is_per_of_dims<T>;
BaseDimension<T> || detail::is_dimensionless<T> || detail::is_power_of_dim<T> || detail::is_per_of_dims<T>;
template<DerivedDimensionSpec... Ds>
template<typename...>
struct derived_dimension;
namespace detail {
@@ -160,47 +167,83 @@ struct derived_dimension_impl : detail::expr_fractions<derived_dimension<>, Ds..
* more digestable for the user. The positive exponents are ordered first and all negative exponents are put as a list
* into the `per<...>` class template. If a power of exponent is different than `1` the dimension type is enclosed in
* `power<Dim, Num, Den>` class template. Otherwise, it is just put directly in the list without any wrapper. There
* is also one special case. In case all of the exponents are negative than the `one_dim` being a dimension of
* is also one special case. In case all of the exponents are negative than the `dimensionless` being a dimension of
* a dimensionless quantity is put in the front to increase the readability.
*
* For example:
*
* @code{.cpp}
* inline constexpr struct frequency_dim : decltype(1 / time_dim) {} frequency_dim;
* inline constexpr struct speed_dim : decltype(length_dim / time_dim) {} speed_dim;
* inline constexpr struct acceleration_dim : decltype(speed_dim / time_dim) {} acceleration_dim;
* inline constexpr struct force_dim : decltype(mass_dim * acceleration_dim) {} force_dim;
* inline constexpr struct energy_dim : decltype(force_dim * length_dim) {} energy_dim;
* inline constexpr struct moment_of_force_dim : decltype(length_dim * force_dim) {} moment_of_force_dim;
* inline constexpr struct torque_dim : decltype(moment_of_force_dim) {} torque_dim;
* DERIVED_DIMENSION(frequency, 1 / time);
* DERIVED_DIMENSION(speed, length / time);
* DERIVED_DIMENSION(acceleration, speed / time);
* DERIVED_DIMENSION(force, mass * acceleration);
* DERIVED_DIMENSION(energy, force * length);
* DERIVED_DIMENSION(moment_of_force, length * force);
* DERIVED_DIMENSION(torque, moment_of_force);
* @endcode
*
* - `frequency_dim` will be derived from type `derived_dimension<one_dim, per<time_dim>>`
* - `speed_dim` will be derived from type `derived_dimension<length_dim, per<time_dim>>`
* - `acceleration_dim` will be derived from type `derived_dimension<length_dim, per<power<time_dim, 2>>>`
* - `force_dim` will be derived from type `derived_dimension<length_dim, mass_dim, per<power<time_dim, 2>>>`
* - `energy_dim` will be derived from type `derived_dimension<power<length_dim, 2>, mass_dim, per<power<time_dim, 2>>>`
* - `frequency` will be derived from type `derived_dimension<dimensionless, per<time>>`
* - `speed` will be derived from type `derived_dimension<length, per<time>>`
* - `acceleration` will be derived from type `derived_dimension<length, per<power<time, 2>>>`
* - `force` will be derived from type `derived_dimension<length, mass, per<power<time, 2>>>`
* - `energy` will be derived from type `derived_dimension<power<length, 2>, mass, per<power<time, 2>>>`
*
* @note A common convention in this library is to assign the same name for a type and an object of this type.
* Besides defining them user never works with the dimension types in the source code. All operations
* are done on the objects. Contrarily, the dimension types are the only one visible in the compilation
* errors. Having them of the same names improves user experience and somehow blurs those separate domains.
*
* Two dimensions are deemed equal when they are of the same type. With that strong type `speed_dim` and
* `derived_dimension<length_dim, per<time_dim>>` are considered not equal. They are convertible though.
* Two dimensions are deemed equal when they are of the same type. With that strong type `speed` and
* `derived_dimension<length, per<time>>` are considered not equal. They are convertible though.
* User can implicitly convert up and down the inheritance hierarchy between those two.
* `torque_dim` and `moment_of_force_dim` are convertible as well. However, `energy_dim` and `torque_dim`
* `torque` and `moment_of_force` are convertible as well. However, `energy` and `torque`
* are not convertible as they do not inherit from each other. They are from two separate branches of
* dimensionally equivalent quantities.
*
* @tparam Ds a parameter pack consisting tokens allowed in the dimension specification
* (base dimensions, `one_dim`, `power<Dim, Num, Den>`, `per<...>`)
* (base dimensions, `dimensionless`, `power<Dim, Num, Den>`, `per<...>`)
*
* @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.
*/
#ifdef __cpp_explicit_this_parameter__
template<DerivedDimensionSpec... Ds>
struct derived_dimension : detail::derived_dimension_impl<Ds...> {};
struct derived_dimension : detail::derived_dimension_impl<Ds...> {
template<Unit U, typename Self>
requires(convertible(Self{}, detail::get_dimension_for(U{})))
[[nodiscard]] constexpr reference<Self, U> operator[](this const Self, U)
{
return {};
}
};
#else
template<typename...>
struct derived_dimension;
template<DerivedDimensionSpec... Ds>
struct derived_dimension<Ds...> : detail::derived_dimension_impl<Ds...> {
template<Unit U>
requires(convertible(derived_dimension{}, detail::get_dimension_for(U{})))
[[nodiscard]] constexpr reference<derived_dimension, U> operator[](U) const
{
return {};
}
};
template<typename Self, DerivedDimension D>
struct derived_dimension<Self, D> : D {
template<Unit U>
requires(convertible(Self{}, detail::get_dimension_for(U{})))
[[nodiscard]] constexpr reference<Self, U> operator[](U) const
{
return {};
}
};
#endif
namespace detail {
@@ -223,13 +266,13 @@ inline constexpr bool is_derived_dimension<T> = true;
* Dimension for which all the exponents of the factors corresponding to the base
* dimensions are zero. Also commonly named as "dimensionless".
*/
inline constexpr struct one_dim : derived_dimension<> {
} one_dim;
inline constexpr struct dimensionless : derived_dimension<> {
} dimensionless;
namespace detail {
template<>
inline constexpr bool is_one_dim<struct one_dim> = true;
inline constexpr bool is_dimensionless<struct dimensionless> = true;
template<Dimension T>
struct dim_type_impl {
@@ -252,14 +295,14 @@ using dim_type = dim_type_impl<T>::type;
template<Dimension Lhs, Dimension Rhs>
[[nodiscard]] consteval Dimension auto operator*(Lhs, Rhs)
{
return detail::expr_multiply<derived_dimension, struct one_dim, detail::type_list_of_base_dimension_less>(
return detail::expr_multiply<derived_dimension, struct dimensionless, detail::type_list_of_base_dimension_less>(
detail::dim_type<Lhs>{}, detail::dim_type<Rhs>{});
}
template<Dimension Lhs, Dimension Rhs>
[[nodiscard]] consteval Dimension auto operator/(Lhs, Rhs)
{
return detail::expr_divide<derived_dimension, struct one_dim, detail::type_list_of_base_dimension_less>(
return detail::expr_divide<derived_dimension, struct dimensionless, detail::type_list_of_base_dimension_less>(
detail::dim_type<Lhs>{}, detail::dim_type<Rhs>{});
}
@@ -267,7 +310,7 @@ template<Dimension D>
[[nodiscard]] consteval Dimension auto operator/(int value, D)
{
gsl_Expects(value == 1);
return detail::expr_invert<derived_dimension, struct one_dim>(detail::dim_type<D>{});
return detail::expr_invert<derived_dimension, struct dimensionless>(detail::dim_type<D>{});
}
template<Dimension D>
@@ -304,9 +347,38 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Dimension D>
else
return derived_dimension<power<D, Num, Den>>{};
} else
return detail::expr_pow<Num, Den, derived_dimension, struct one_dim, detail::type_list_of_base_dimension_less>(d);
return detail::expr_pow<Num, Den, derived_dimension, struct dimensionless,
detail::type_list_of_base_dimension_less>(d);
}
namespace detail {
template<Unit U>
[[nodiscard]] consteval Dimension auto get_dimension_for_impl(U)
requires requires { U::base_dimension; }
{
return U::base_dimension;
}
template<Unit U>
requires requires { U::base_dimension; }
using to_base_dimension = std::remove_const_t<decltype(U::base_dimension)>;
template<typename... Us>
[[nodiscard]] consteval Dimension auto get_dimension_for_impl(const derived_unit<Us...>& u)
{
return detail::expr_map<to_base_dimension, derived_dimension, struct dimensionless,
detail::type_list_of_base_dimension_less>(u);
}
[[nodiscard]] consteval Dimension auto get_dimension_for(Unit auto u)
{
return get_dimension_for_impl(get_canonical_unit(u).reference_unit);
}
} // namespace detail
// TODO consider adding the support for text output of the dimensional equation
} // namespace units
@@ -327,3 +399,26 @@ struct common_type<D1, D2> {
};
} // namespace std
#ifdef __cpp_explicit_this_parameter__
#define BASE_DIMENSION(name, symbol) \
inline constexpr struct name : base_dimension<symbol> { \
} name
#define DERIVED_DIMENSION(name, base) \
inline constexpr struct name : base { \
} name
#else
#define BASE_DIMENSION(name, symbol) \
inline constexpr struct name : base_dimension<name, symbol> { \
} name
#define DERIVED_DIMENSION(name, base) \
inline constexpr struct name : derived_dimension<name, base> { \
} name
#endif
+2 -2
View File
@@ -48,7 +48,7 @@ inline constexpr auto make_quantity = [](Representation auto&& v) {
};
template<typename T>
concept quantity_one = Quantity<T> && (T::dimension == one_dim) && (T::unit == one);
concept quantity_one = Quantity<T> && (T::dimension == dimensionless) && (T::unit == one);
} // namespace detail
@@ -506,7 +506,7 @@ template<Quantity Q1, Quantity Q2>
template<Quantity Q1, Quantity Q2>
requires(!floating_point_<typename Q1::rep>) && (!floating_point_<typename Q2::rep>) &&
(std::convertible_to<Q2, Q1> || quantity_of<Q2, one_dim>) &&
(std::convertible_to<Q2, Q1> || quantity_of<Q2, dimensionless>) &&
(quantity_value_for_<std::modulus<>, typename Q1::rep, typename Q2::rep>)
[[nodiscard]] constexpr Quantity auto operator%(const Q1& lhs, const Q2& rhs)
{
-3
View File
@@ -162,9 +162,6 @@ struct system_reference {
}
};
inline constexpr struct dimensionless : system_reference<one_dim, one> {
} dimensionless;
} // namespace units
namespace std {
+80 -14
View File
@@ -29,19 +29,52 @@
#include <units/bits/external/type_name.h>
#include <units/bits/external/type_traits.h>
#include <units/magnitude.h>
#include <units/ratio.h>
#include <units/symbol_text.h>
#include <iterator>
#include <string>
// #include <units/bits/derived_symbol_text.h>
// IWYU pragma: begin_exports
// #include <units/bits/absolute_magnitude.h>
// #include <units/ratio.h>
// IWYU pragma: end_exports
namespace units {
#ifdef __cpp_explicit_this_parameter__
template<basic_fixed_string Symbol>
#else
template<typename Self, basic_fixed_string Symbol>
#endif
struct base_dimension;
namespace detail {
#ifdef __cpp_explicit_this_parameter__
template<basic_fixed_string Symbol>
void to_base_base_dimension(const volatile base_dimension<Symbol>*);
#else
template<typename Self, basic_fixed_string Symbol>
void to_base_base_dimension(const volatile base_dimension<Self, Symbol>*);
#endif
template<typename T>
inline constexpr bool is_specialization_of_base_dimension = false;
#ifdef __cpp_explicit_this_parameter__
template<basic_fixed_string Symbol>
inline constexpr bool is_specialization_of_base_dimension<base_dimension<Symbol>> = true;
#else
template<typename Self, basic_fixed_string Symbol>
inline constexpr bool is_specialization_of_base_dimension<base_dimension<Self, Symbol>> = true;
#endif
} // namespace detail
/**
* @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 = requires(T* t) { detail::to_base_base_dimension(t); } &&
(!detail::is_specialization_of_base_dimension<T>);
namespace detail {
template<typename T>
@@ -86,14 +119,16 @@ inline constexpr bool is_specialization_of_scaled_unit<scaled_unit<M, U>> = true
/**
* @brief A named unit
*
* Defines a unit with a special name.
* Defines a unit with a special name. It may be used to provide a base unit in the system
* of units (i.e. `metre`) or a name assigned to another scaled or derived unit
* (i.e. `hour`, `joule`).
* Most of the named units may be composed with a prefix to create a `prefixed_unit`.
*
* For example:
*
* @code{.cpp}
* inline constexpr struct second : named_unit<"s"> {} second;
* inline constexpr struct metre : named_unit<"m"> {} metre;
* 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", 1 / second> {} hertz;
* inline constexpr struct newton : named_unit<"N", kilogram * metre / square<second>> {} newton;
* inline constexpr struct degree_Celsius : named_unit<basic_symbol_text{"\u00B0C", "`C"}, kelvin> {} degree_Celsius;
@@ -111,11 +146,33 @@ template<basic_symbol_text Symbol, auto...>
struct named_unit;
/**
* @brief Specialization for base unit
* @brief Specialization for unit of a specified base dimension
*
* Defines a base unit in the system of units (i.e. `metre`).
* or a name assigned to another scaled or derived unit (i.e. `hour`, `joule`).
* Most of the named units may be composed with a prefix to create a `prefixed_unit`.
* This is the preferred way to define a measurement unit for a base dimension. For example `si::metre`
* is a unit to measure `isq::length` in the SI system.
*
* @note It does not have to (or sometimes even can't) be a proper system's base unit. For example
* a base unit of mass in the SI is `si::kilogram` but here you are about to provide an `si::gram`
* and it will work just fine as those two are convertible to each other. A similar case would be
* the `cgs::centimetre` that is a base unit for `isq::length` in the CGS system.
*
* @tparam Symbol a short text representation of the unit
* @tparam BaseDimension base dimension measured with this unit
*/
template<basic_symbol_text Symbol, BaseDimension auto D>
requires(!Symbol.empty())
struct named_unit<Symbol, D> {
static constexpr auto symbol = Symbol; ///< Unique base unit identifier
static constexpr auto base_dimension = D;
};
/**
* @brief Specialization for a unit that can be reused by several base dimensions
*
* This specialization is used in rare cases where more than one base dimension in a specific
* system of units uses the same unit. For example in a hypothetical system of units where
* constant for speed of light `c = 1`, length and time could be measured in seconds. In such
* cases `system_reference` has to be used to explicitly express such a binding.
*
* @tparam Symbol a short text representation of the unit
*/
@@ -319,6 +376,9 @@ struct canonical_unit {
U reference_unit;
};
template<Unit T, basic_symbol_text Symbol, BaseDimension auto D>
[[nodiscard]] consteval auto get_canonical_unit_impl(T t, const named_unit<Symbol, D>&);
template<Unit T, basic_symbol_text Symbol>
[[nodiscard]] consteval auto get_canonical_unit_impl(T t, const named_unit<Symbol>&);
@@ -338,6 +398,12 @@ template<Unit T, auto M, typename U>
return canonical_unit{M * base.mag, base.reference_unit};
}
template<Unit T, basic_symbol_text Symbol, BaseDimension auto D>
[[nodiscard]] consteval auto get_canonical_unit_impl(T t, const named_unit<Symbol, D>&)
{
return canonical_unit{mag<1>, t};
}
template<Unit T, basic_symbol_text Symbol>
[[nodiscard]] consteval auto get_canonical_unit_impl(T t, const named_unit<Symbol>&)
{