feat: initial very dirty version of V2 framework
This commit is contained in:
1 parent
6bccc57800
commit
daf97a3a86
22 files changed
+1829
-996
No files matched your search
+158
-114
@@ -22,27 +22,28 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/derived_symbol_text.h>
|
||||
#include <units/bits/external/downcasting.h>
|
||||
#include <units/bits/expression_template.h>
|
||||
#include <units/bits/external/fixed_string.h>
|
||||
#include <units/bits/external/type_name.h>
|
||||
#include <units/bits/external/type_traits.h>
|
||||
#include <units/magnitude.h>
|
||||
#include <units/symbol_text.h>
|
||||
|
||||
// #include <units/bits/derived_symbol_text.h>
|
||||
|
||||
// IWYU pragma: begin_exports
|
||||
#include <units/bits/derived_scaled_unit.h>
|
||||
#include <units/bits/external/fixed_string.h>
|
||||
#include <units/magnitude.h>
|
||||
#include <units/prefix.h>
|
||||
#include <units/ratio.h>
|
||||
#include <units/symbol_text.h>
|
||||
// #include <units/bits/absolute_magnitude.h>
|
||||
// #include <units/ratio.h>
|
||||
// IWYU pragma: end_exports
|
||||
|
||||
namespace units {
|
||||
|
||||
namespace detail {
|
||||
// namespace detail {
|
||||
|
||||
template<typename>
|
||||
inline constexpr bool can_be_prefixed = false;
|
||||
// template<typename>
|
||||
// inline constexpr bool can_be_prefixed = false;
|
||||
|
||||
} // namespace detail
|
||||
// } // namespace detail
|
||||
|
||||
/**
|
||||
* @brief A common point for a hierarchy of units
|
||||
@@ -63,16 +64,56 @@ inline constexpr bool can_be_prefixed = false;
|
||||
* it gets the incomplete child's type with the CRTP idiom.
|
||||
*/
|
||||
template<Magnitude auto M, typename U>
|
||||
struct scaled_unit : downcast_base<scaled_unit<M, U>> {
|
||||
struct scaled_unit {
|
||||
static constexpr UNITS_MSVC_WORKAROUND(Magnitude) auto mag = M;
|
||||
using reference = U;
|
||||
};
|
||||
|
||||
template<Dimension D, Magnitude auto M>
|
||||
using downcast_unit = downcast<scaled_unit<M, typename dimension_unit<D>::reference>>;
|
||||
// TODO: Remove when P1985 accepted
|
||||
namespace detail {
|
||||
|
||||
template<Unit U1, Unit U2>
|
||||
struct same_unit_reference : is_same<typename U1::reference, typename U2::reference> {};
|
||||
template<Magnitude auto M, typename U>
|
||||
void to_base_scaled_unit(const volatile scaled_unit<M, U>*);
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/**
|
||||
* @brief A concept matching all unit types in the library
|
||||
*
|
||||
* Satisfied by all unit types derived from an specialization of :class:`scaled_unit`.
|
||||
*/
|
||||
template<typename T>
|
||||
concept Unit = requires(T* t) { detail::to_base_scaled_unit(t); };
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename>
|
||||
inline constexpr bool is_named = false;
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
concept NamedUnit = Unit<T> && detail::is_named<T>;
|
||||
|
||||
template<Unit auto U1, Unit auto U2>
|
||||
struct same_unit_reference : is_same<typename decltype(U1)::reference, typename decltype(U2)::reference> {};
|
||||
|
||||
// TODO add checking for `per` and power elements as well
|
||||
template<typename T>
|
||||
concept UnitSpec = Unit<T> || is_specialization_of<T, per> || detail::is_specialization_of_power<T>;
|
||||
|
||||
template<UnitSpec... Us>
|
||||
struct derived_unit : detail::expr_fractions<derived_unit<>, Us...>, scaled_unit<mag<1>(), derived_unit<Us...>> {};
|
||||
|
||||
// : detail::normalized_dimension<derived_dimension<>, Ds...> {};
|
||||
|
||||
/**
|
||||
* @brief Unit one
|
||||
*
|
||||
* Unit of a dimensionless quantity.
|
||||
*/
|
||||
inline constexpr struct one : derived_unit<> {
|
||||
} one;
|
||||
|
||||
/**
|
||||
* @brief A named unit
|
||||
@@ -80,27 +121,35 @@ struct same_unit_reference : is_same<typename U1::reference, typename U2::refere
|
||||
* Defines a named (in most cases coherent) unit that is then passed to a dimension definition.
|
||||
* A named unit may be composed with a prefix to create a prefixed_unit.
|
||||
*
|
||||
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
|
||||
* @tparam Symbol a short text representation of the unit
|
||||
*/
|
||||
template<typename Child, basic_symbol_text Symbol>
|
||||
struct named_unit : downcast_dispatch<Child, scaled_unit<mag<1>(), Child>> {
|
||||
template<basic_symbol_text Symbol, auto...>
|
||||
struct named_unit;
|
||||
|
||||
template<basic_symbol_text Symbol>
|
||||
struct named_unit<Symbol> : scaled_unit<mag<1>(), named_unit<Symbol>> {
|
||||
static constexpr auto symbol = Symbol;
|
||||
};
|
||||
|
||||
template<basic_symbol_text Symbol, auto U>
|
||||
requires is_specialization_of<std::remove_const_t<decltype(U)>, derived_unit>
|
||||
struct named_unit<Symbol, U> : decltype(U) {
|
||||
static constexpr auto symbol = Symbol;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief A named scaled unit
|
||||
*
|
||||
* Defines a new named unit that is a scaled version of another unit.
|
||||
* A named unit may be composed with a prefix to create a prefixed_unit.
|
||||
*
|
||||
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
|
||||
* @tparam Symbol a short text representation of the unit
|
||||
* @tparam M the Magnitude by which to scale U
|
||||
* @tparam U a reference unit to scale
|
||||
*/
|
||||
template<typename Child, basic_symbol_text Symbol, Magnitude auto M, Unit U>
|
||||
struct named_scaled_unit : downcast_dispatch<Child, scaled_unit<M * U::mag, typename U::reference>> {
|
||||
template<basic_symbol_text Symbol, Magnitude auto M, Unit auto U>
|
||||
struct named_scaled_unit : scaled_unit<M* decltype(U)::mag, typename decltype(U)::reference> {
|
||||
static constexpr auto symbol = Symbol;
|
||||
};
|
||||
|
||||
@@ -115,11 +164,15 @@ struct named_scaled_unit : downcast_dispatch<Child, scaled_unit<M * U::mag, type
|
||||
* @tparam P prefix to be appied to the reference unit
|
||||
* @tparam U reference unit
|
||||
*/
|
||||
template<typename Child, Prefix P, NamedUnit U>
|
||||
requires detail::can_be_prefixed<U>
|
||||
struct prefixed_unit : downcast_dispatch<Child, scaled_unit<P::mag * U::mag, typename U::reference>> {
|
||||
static constexpr auto symbol = P::symbol + U::symbol;
|
||||
};
|
||||
// template<typename Child, Prefix P, NamedUnit U>
|
||||
// requires detail::can_be_prefixed<U>
|
||||
// struct prefixed_unit : downcast_dispatch<Child, scaled_unit<P::mag * U::mag, typename U::reference>> {
|
||||
// static constexpr auto symbol = P::symbol + U::symbol;
|
||||
// };
|
||||
|
||||
|
||||
template<basic_symbol_text Symbol, Magnitude auto M, NamedUnit auto U>
|
||||
struct prefixed_unit : scaled_unit<M* decltype(U)::mag, typename decltype(U)::reference> {};
|
||||
|
||||
/**
|
||||
* @brief A coherent unit of a derived quantity
|
||||
@@ -129,105 +182,96 @@ struct prefixed_unit : downcast_dispatch<Child, scaled_unit<P::mag * U::mag, typ
|
||||
*
|
||||
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
|
||||
*/
|
||||
template<typename Child>
|
||||
struct derived_unit : downcast_dispatch<Child, scaled_unit<mag<1>(), Child>> {};
|
||||
|
||||
/**
|
||||
* @brief A unit with a deduced ratio and symbol
|
||||
*
|
||||
* Defines a new unit with a deduced ratio and symbol based on the recipe from the provided
|
||||
* derived dimension. The number and order of provided units should match the recipe of the
|
||||
* derived dimension. All of the units provided should also be a named ones so it is possible
|
||||
* to create a deduced symbol text.
|
||||
*
|
||||
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
|
||||
* @tparam Dim a derived dimension recipe to use for deduction
|
||||
* @tparam U the unit of the first composite dimension from provided derived dimension's recipe
|
||||
* @tparam URest the units for the rest of dimensions from the recipe
|
||||
*/
|
||||
template<typename Child, DerivedDimension Dim, NamedUnit U, NamedUnit... URest>
|
||||
requires detail::compatible_units<typename Dim::recipe, U, URest...>
|
||||
struct derived_scaled_unit : downcast_dispatch<Child, detail::derived_scaled_unit<Dim, U, URest...>> {
|
||||
static constexpr auto symbol = detail::derived_symbol_text<Dim, U, URest...>();
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief An aliased named unit
|
||||
*
|
||||
* Defines a named alias for another unit. It is useful to assign alternative names and symbols
|
||||
* to the already predefined units (i.e. "tonne" for "megagram").
|
||||
* A alias unit may be composed with a prefix to create a prefixed_alias_unit.
|
||||
*
|
||||
* @tparam U Unit for which an alias is defined
|
||||
* @tparam Symbol a short text representation of the unit
|
||||
*/
|
||||
template<Unit U, basic_symbol_text Symbol>
|
||||
struct alias_unit : U {
|
||||
static constexpr auto symbol = Symbol;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A prefixed alias unit
|
||||
*
|
||||
* Defines a new unit that is an alias for a scaled version of another unit by the provided
|
||||
* prefix. It is only possible to create such a unit if the given prefix type matches the one
|
||||
* defined in a reference unit.
|
||||
*
|
||||
* @tparam U Unit for which an alias is defined
|
||||
* @tparam P prefix to be appied to the reference unit
|
||||
* @tparam AU reference alias unit
|
||||
*/
|
||||
template<Unit U, Prefix P, AliasUnit AU>
|
||||
requires(!AliasUnit<U>)
|
||||
struct prefixed_alias_unit : U {
|
||||
static constexpr auto symbol = P::symbol + AU::symbol;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Unknown coherent unit
|
||||
*
|
||||
* Used as a coherent unit of an unknown dimension.
|
||||
*/
|
||||
template<Exponent... Es>
|
||||
struct unknown_coherent_unit :
|
||||
downcast_dispatch<unknown_coherent_unit<Es...>,
|
||||
scaled_unit<detail::absolute_magnitude(exponent_list<Es...>()), unknown_coherent_unit<Es...>>> {};
|
||||
// template<typename Child>
|
||||
// struct derived_unit : downcast_dispatch<Child, scaled_unit<mag<1>(), Child>> {};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename Child, basic_symbol_text Symbol>
|
||||
void is_named_impl(const volatile named_unit<Child, Symbol>*);
|
||||
template<basic_symbol_text Symbol>
|
||||
void is_named_impl(const volatile named_unit<Symbol>*);
|
||||
|
||||
template<typename Child, basic_symbol_text Symbol, Magnitude auto M, typename U>
|
||||
void is_named_impl(const volatile named_scaled_unit<Child, Symbol, M, U>*);
|
||||
|
||||
template<typename Child, typename P, typename U>
|
||||
void is_named_impl(const volatile prefixed_unit<Child, P, U>*);
|
||||
|
||||
template<typename U, basic_symbol_text Symbol>
|
||||
void is_named_impl(const volatile alias_unit<U, Symbol>*);
|
||||
|
||||
template<typename U, typename P, typename AU>
|
||||
void is_named_impl(const volatile prefixed_alias_unit<U, P, AU>*);
|
||||
template<basic_symbol_text Symbol, Magnitude auto M, auto U>
|
||||
void is_named_impl(const volatile named_scaled_unit<Symbol, M, U>*);
|
||||
|
||||
template<Unit U>
|
||||
inline constexpr bool is_named<U> = requires(U * u) { is_named_impl(u); };
|
||||
|
||||
template<typename Child, basic_symbol_text Symbol>
|
||||
void can_be_prefixed_impl(const volatile named_unit<Child, Symbol>*);
|
||||
// template<typename Child, basic_symbol_text Symbol>
|
||||
// void can_be_prefixed_impl(const volatile named_unit<Child, Symbol>*);
|
||||
|
||||
template<typename Child, basic_symbol_text Symbol, Magnitude auto M, typename U>
|
||||
void can_be_prefixed_impl(const volatile named_scaled_unit<Child, Symbol, M, U>*);
|
||||
// template<typename Child, basic_symbol_text Symbol, Magnitude auto M, typename U>
|
||||
// void can_be_prefixed_impl(const volatile named_scaled_unit<Child, Symbol, M, U>*);
|
||||
|
||||
template<typename U, basic_symbol_text Symbol>
|
||||
void can_be_prefixed_impl(const volatile alias_unit<U, Symbol>*);
|
||||
// template<typename U, basic_symbol_text Symbol>
|
||||
// void can_be_prefixed_impl(const volatile alias_unit<U, Symbol>*);
|
||||
|
||||
template<Unit U>
|
||||
inline constexpr bool can_be_prefixed<U> = requires(U * u) { can_be_prefixed_impl(u); };
|
||||
// template<Unit U>
|
||||
// inline constexpr bool can_be_prefixed<U> = requires(U * u) { can_be_prefixed_impl(u); };
|
||||
|
||||
template<Magnitude auto M, typename U>
|
||||
inline constexpr bool can_be_prefixed<scaled_unit<M, U>> = can_be_prefixed<typename U::reference>;
|
||||
// template<Magnitude auto M, typename U>
|
||||
// inline constexpr bool can_be_prefixed<scaled_unit<M, U>> = can_be_prefixed<typename U::reference>;
|
||||
|
||||
|
||||
template<Unit U1, Unit U2>
|
||||
struct unit_less : std::bool_constant<type_name<U1>() < type_name<U2>()> {};
|
||||
|
||||
template<typename T1, typename T2>
|
||||
using type_list_of_unit_less = expr_less<T1, T2, unit_less>;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Unit U1, Unit U2>
|
||||
constexpr Unit auto operator*(U1, U2)
|
||||
{
|
||||
return detail::expr_multiply<U1, U2, struct one, detail::type_list_of_unit_less, derived_unit>();
|
||||
}
|
||||
|
||||
template<Unit U1, Unit U2>
|
||||
constexpr Unit auto operator/(U1, U2)
|
||||
{
|
||||
return detail::expr_divide<U1, U2, struct one, detail::type_list_of_unit_less, derived_unit>();
|
||||
}
|
||||
|
||||
template<Unit U>
|
||||
constexpr Unit auto operator/(int value, U)
|
||||
{
|
||||
gsl_Assert(value == 1);
|
||||
return detail::expr_invert<U, struct one, derived_unit>();
|
||||
}
|
||||
|
||||
template<Unit U1, Unit U2>
|
||||
constexpr bool operator==(U1, U2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// template<BaseDimension D1, BaseDimension D2>
|
||||
// constexpr bool operator==(D1, D2)
|
||||
// {
|
||||
// return D1::symbol == D2::symbol;
|
||||
// }
|
||||
|
||||
// template<BaseDimension D1, Dimension D2>
|
||||
// requires(type_list_size<typename D2::normalized_den> == 0) && (type_list_size<typename D2::normalized_num> == 1) &&
|
||||
// BaseDimension<type_list_front<typename D2::normalized_num>>
|
||||
// constexpr bool operator==(D1, D2)
|
||||
// {
|
||||
// return D1::symbol == type_list_front<typename D2::normalized_num>::symbol;
|
||||
// }
|
||||
|
||||
// template<Dimension D1, BaseDimension D2>
|
||||
// requires(type_list_size<typename D1::normalized_den> == 0) && (type_list_size<typename D1::normalized_num> == 1) &&
|
||||
// BaseDimension<type_list_front<typename D1::normalized_num>>
|
||||
// constexpr bool operator==(D1, D2)
|
||||
// {
|
||||
// return type_list_front<typename D1::normalized_num>::symbol == D2::symbol;
|
||||
// }
|
||||
|
||||
// template<DerivedDimension D1, DerivedDimension D2>
|
||||
// constexpr bool operator==(D1, D2)
|
||||
// {
|
||||
// return std::is_same_v<typename D1::normalized_num, typename D2::normalized_num> &&
|
||||
// std::is_same_v<typename D1::normalized_den, typename D2::normalized_den>;
|
||||
// }
|
||||
|
||||
} // namespace units
|
||||
Reference in new issue
Block a user