refactor: constant_unit abstraction removed and replaced with regular named_unit
This commit is contained in:
1 parent
6364fcdf7e
commit
7a730fde58
8 files changed
+66
-144
No files matched your search
@@ -50,10 +50,6 @@ struct scaled_unit;
|
||||
template<basic_symbol_text Symbol, auto...>
|
||||
struct named_unit;
|
||||
|
||||
template<basic_symbol_text Symbol, Unit auto U>
|
||||
requires(!Symbol.empty())
|
||||
struct constant_unit;
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<basic_symbol_text Symbol, auto... Args>
|
||||
@@ -69,21 +65,13 @@ inline constexpr bool is_specialization_of_named_unit = false;
|
||||
template<basic_symbol_text Symbol, auto... Args>
|
||||
inline constexpr bool is_specialization_of_named_unit<named_unit<Symbol, Args...>> = true;
|
||||
|
||||
template<basic_symbol_text Symbol, auto U>
|
||||
void to_base_specialization_of_constant_unit(const volatile constant_unit<Symbol, U>*);
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_derived_from_specialization_of_constant_unit =
|
||||
requires(T* t) { to_base_specialization_of_constant_unit(t); };
|
||||
|
||||
/**
|
||||
* @brief A concept matching all units with special names
|
||||
*
|
||||
* Satisfied by all unit types derived from the specialization of `named_unit` (but not constant units).
|
||||
*/
|
||||
template<typename T>
|
||||
concept NamedUnit = Unit<T> && detail::is_derived_from_specialization_of_named_unit<T> &&
|
||||
(!detail::is_derived_from_specialization_of_constant_unit<T>);
|
||||
concept NamedUnit = Unit<T> && detail::is_derived_from_specialization_of_named_unit<T>;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
@@ -144,12 +132,6 @@ inline constexpr bool is_specialization_of_unit = false;
|
||||
template<basic_symbol_text Symbol, auto... Args>
|
||||
inline constexpr bool is_specialization_of_unit<named_unit<Symbol, Args...>> = true;
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_specialization_of_constant_unit = false;
|
||||
|
||||
template<basic_symbol_text Symbol, auto U>
|
||||
inline constexpr bool is_specialization_of_constant_unit<constant_unit<Symbol, U>> = true;
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_specialization_of_prefixed_unit = false;
|
||||
|
||||
@@ -158,8 +140,7 @@ inline constexpr bool is_specialization_of_prefixed_unit<prefixed_unit<Symbol, M
|
||||
|
||||
template<typename T>
|
||||
requires requires(T* t) { is_unit_impl(t); }
|
||||
inline constexpr bool is_unit<T> = !is_specialization_of_named_unit<T> && !is_specialization_of_constant_unit<T> &&
|
||||
!is_specialization_of_prefixed_unit<T>;
|
||||
inline constexpr bool is_unit<T> = !is_specialization_of_named_unit<T> && !is_specialization_of_prefixed_unit<T>;
|
||||
|
||||
template<Unit U>
|
||||
[[nodiscard]] consteval bool has_associated_quantity(U);
|
||||
|
||||
@@ -162,41 +162,6 @@ struct named_unit<Symbol, U, QS> : std::remove_const_t<decltype(U)> {
|
||||
static constexpr auto quantity_spec = QS;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A unit of a physical constant
|
||||
*
|
||||
* Defines a unit of a physical constant together with its value encoded as a unit ratio.
|
||||
*
|
||||
* This allows moving all of the constant-related ratio manipulation to the compile-time domain
|
||||
* (i.e. multiplying and then dividing by the same constant will eliminate the item from the final
|
||||
* type).
|
||||
* As a result we have faster runtime performance and no precision loss due to eager floating-point
|
||||
* operations. Also, if the user prefers integral types for a quantity representation, this will
|
||||
* not force the user to convert to a floating-point type right away. Only when a final quantity
|
||||
* number needs to actually account for the constant value, the floating-point operation (if any)
|
||||
* can be triggered lazily with the `value_cast<U>()`.
|
||||
*
|
||||
* For example:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* inline constexpr struct standard_gravity_unit :
|
||||
* constant_unit<"g", mag<ratio{980'665, 100'000}> * metre / square(second)> {} standard_gravity_unit;
|
||||
* @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 unit types in the source code. All operations
|
||||
* are done on the objects. Contrarily, the unit 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 a short text representation of the constant
|
||||
*
|
||||
* @note Constant symbol is printed in the text output encapsulated inside square brackets `[]`
|
||||
* and before any regular units
|
||||
*/
|
||||
template<basic_symbol_text Symbol, Unit auto U>
|
||||
requires(!Symbol.empty())
|
||||
struct constant_unit : named_unit<'[' + Symbol + ']', U> {};
|
||||
|
||||
/**
|
||||
* @brief A prefixed unit
|
||||
*
|
||||
@@ -400,23 +365,15 @@ template<Unit T, typename... Expr>
|
||||
template<Unit Lhs, Unit Rhs>
|
||||
[[nodiscard]] consteval bool less(Lhs, Rhs)
|
||||
{
|
||||
if constexpr ((is_derived_from_specialization_of_constant_unit<Lhs> &&
|
||||
is_derived_from_specialization_of_constant_unit<Rhs>) ||
|
||||
(!is_derived_from_specialization_of_constant_unit<Lhs> &&
|
||||
!is_derived_from_specialization_of_constant_unit<Rhs>)) {
|
||||
if constexpr (requires {
|
||||
Lhs::symbol;
|
||||
Rhs::symbol;
|
||||
})
|
||||
// prefer symbols comparison if possible as it gives typically better results
|
||||
// i.e. it puts upper case in from so `N m` is correct
|
||||
return Lhs::symbol < Rhs::symbol;
|
||||
else
|
||||
return type_name<Lhs>() < type_name<Rhs>();
|
||||
} else {
|
||||
// put constants at the front of units list in the expression
|
||||
return is_derived_from_specialization_of_constant_unit<Lhs>;
|
||||
}
|
||||
if constexpr (requires {
|
||||
Lhs::symbol;
|
||||
Rhs::symbol;
|
||||
})
|
||||
// prefer symbols comparison if possible as it gives typically better results
|
||||
// i.e. it puts upper case in from so `N m` is correct
|
||||
return Lhs::symbol < Rhs::symbol;
|
||||
else
|
||||
return type_name<Lhs>() < type_name<Rhs>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user