forked from mpusz/mp-units
refactor: 💥 basic_concepts, quantity and quantity_cast refactored
BREAKING CHANGE: ScalableNumber renamed to QuantityValue Resolves #107
This commit is contained in:
@@ -11,7 +11,7 @@ Interface
|
||||
The difference is that it uses ``double`` as a default representation and has
|
||||
a few additional member types and functions::
|
||||
|
||||
template<Dimension D, UnitOf<D> U, ScalableNumber Rep = double>
|
||||
template<Dimension D, UnitOf<D> U, QuantityValue Rep = double>
|
||||
class quantity {
|
||||
public:
|
||||
using dimension = D;
|
||||
@@ -27,7 +27,7 @@ a few additional member types and functions::
|
||||
[[nodiscard]] constexpr Quantity auto operator*(const quantity<D1, U1, Rep1>& lhs,
|
||||
const quantity<D2, U2, Rep2>& rhs);
|
||||
|
||||
template<ScalableNumber Value, typename D, typename U, typename Rep>
|
||||
template<QuantityValue Value, typename D, typename U, typename Rep>
|
||||
requires std::magma<std::ranges::divided_by, Value, Rep>
|
||||
[[nodiscard]] constexpr Quantity auto operator/(const Value& v,
|
||||
const quantity<D, U, Rep>& q);
|
||||
|
||||
@@ -18,7 +18,7 @@ For example the speed of light constant in :term:`SI` is defined as::
|
||||
|
||||
namespace si::si2019 {
|
||||
|
||||
template<ScalableNumber Rep = double>
|
||||
template<QuantityValue Rep = double>
|
||||
inline constexpr auto speed_of_light = speed<metre_per_second, Rep>(299792458);
|
||||
|
||||
}
|
||||
@@ -27,7 +27,7 @@ The same constant defined for natural units may be provided as::
|
||||
|
||||
namespace natural {
|
||||
|
||||
template<ScalableNumber Rep = double>
|
||||
template<QuantityValue Rep = double>
|
||||
inline constexpr auto speed_of_light = speed<one, Rep>(1);
|
||||
|
||||
}
|
||||
|
||||
@@ -58,8 +58,8 @@ dimension, than we will end up with just a scalable number type:
|
||||
Time auto dur1 = 10_q_s;
|
||||
Time auto dur2 = 2_q_s;
|
||||
Frequency auto fr1 = 5_q_Hz;
|
||||
ScalableNumber auto v1 = dur1 / dur2; // 5
|
||||
ScalableNumber auto v2 = dur1 * fr1; // 50
|
||||
QuantityValue auto v1 = dur1 / dur2; // 5
|
||||
QuantityValue auto v2 = dur1 * fr1; // 50
|
||||
|
||||
Quantity points have a more restricted set of operations.
|
||||
Quantity points can't be added together,
|
||||
|
||||
@@ -34,7 +34,7 @@ type to ``double`` by default::
|
||||
|
||||
namespace si {
|
||||
|
||||
template<Unit U, ScalableNumber Rep = double>
|
||||
template<Unit U, QuantityValue Rep = double>
|
||||
using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
}
|
||||
@@ -75,7 +75,7 @@ be used::
|
||||
All instances of `quantity` class always match the `Quantity` concept.
|
||||
All other regular types that are not quantities are called
|
||||
:term:`scalable numbers <scalable number>` by the library and match the
|
||||
`ScalableNumber` concept.
|
||||
`QuantityValue` concept.
|
||||
|
||||
However, the above is not the most important usage of those concepts. Let's
|
||||
assume that the user wants to implement an ``avg_speed`` function that will
|
||||
@@ -185,7 +185,7 @@ are provided::
|
||||
template<typename T>
|
||||
concept Dimensionless = QuantityOf<T, dim_one>;
|
||||
|
||||
template<Unit U, ScalableNumber Rep = double>
|
||||
template<Unit U, QuantityValue Rep = double>
|
||||
using dimensionless = quantity<dim_one, U, Rep>;
|
||||
|
||||
There are two special units provided for usage with such a quantity:
|
||||
|
||||
@@ -63,7 +63,7 @@ Concepts
|
||||
satisfy :expr:`Quantity<typename T::value_type>` recursively
|
||||
(i.e. ``std::optional<si::length<si::metre>>``).
|
||||
|
||||
.. concept:: template<typename T> ScalableNumber
|
||||
.. concept:: template<typename T> QuantityValue
|
||||
|
||||
A concept matching non-Quantity types. Satisfied by types that match
|
||||
:expr:`(!Quantity<T>) && (!WrappedQuantity<T>) && std::regular<T>` and satisfy one of the
|
||||
|
||||
@@ -9,11 +9,11 @@ its own custom logic for it (i.e. use a complex number or a measurement class th
|
||||
not only a value but also a measurement error).
|
||||
|
||||
|
||||
A `ScalableNumber` concept
|
||||
A `QuantityValue` concept
|
||||
--------------------------
|
||||
|
||||
To support a minimum set of `quantity` operations all custom representation types have to
|
||||
satisfy at least the `ScalableNumber` concept. Which means that they:
|
||||
satisfy at least the `QuantityValue` concept. Which means that they:
|
||||
|
||||
- cannot be quantities by themselves,
|
||||
- cannot be wrappers over the `quantity` type (i.e. ``std::optional<si::length<si::metre>>``),
|
||||
@@ -101,7 +101,7 @@ The only difference here is that in this case we have to explicitly cast the `qu
|
||||
Additional Requirements
|
||||
-----------------------
|
||||
|
||||
As noted in the previous chapter, the `ScalableNumber` concept guarantees us the possibility
|
||||
As noted in the previous chapter, the `QuantityValue` concept guarantees us the possibility
|
||||
to construct quantities, convert between the units of the same dimension, and compare them
|
||||
for equality. To provide additional `quantity` operations the custom representation type
|
||||
have to satisfy more requirements.
|
||||
@@ -200,7 +200,7 @@ The `quantity` class template has a few static member functions: `quantity::zero
|
||||
representation type. The default implementation is provided through the `quantity_values` class
|
||||
template::
|
||||
|
||||
template<ScalableNumber Rep>
|
||||
template<QuantityValue Rep>
|
||||
struct quantity_values {
|
||||
static constexpr Rep zero() noexcept { return Rep(0); }
|
||||
static constexpr Rep one() noexcept { return Rep(1); }
|
||||
@@ -220,7 +220,7 @@ library's framework treat floating-point representation types differently than t
|
||||
ones. This behavior can also be extended to the custom representation types with
|
||||
`treat_as_floating_point` customization point which default definition is::
|
||||
|
||||
template<ScalableNumber Rep>
|
||||
template<QuantityValue Rep>
|
||||
inline constexpr bool treat_as_floating_point = std::is_floating_point_v<Rep>;
|
||||
|
||||
If our representation type should have a floating-point semantics or if it is a class
|
||||
|
||||
@@ -121,7 +121,7 @@ coherent unit::
|
||||
struct desk_per_hour : deduced_unit<desk_per_hour, dim_desk_rate, desk, si::hour> {};
|
||||
|
||||
// a quantity of our dimension
|
||||
template<Unit U, ScalableNumber Rep = double>
|
||||
template<UnitOf<dim_desk_rate> U, QuantityValue Rep = double>
|
||||
using desk_rate = quantity<dim_desk_rate, U, Rep>;
|
||||
|
||||
// a concept matching the above quantity
|
||||
@@ -150,7 +150,7 @@ define a new base dimension, its units, quantity helper, concept, and UDLs::
|
||||
struct person : named_unit<person, "person", no_prefix> {};
|
||||
struct dim_people : base_dimension<"people", person> {};
|
||||
|
||||
template<Unit U, ScalableNumber Rep = double>
|
||||
template<UnitOf<dim_people> U, QuantityValue Rep = double>
|
||||
using people = quantity<dim_people, U, Rep>;
|
||||
|
||||
template<typename T>
|
||||
@@ -169,7 +169,7 @@ With the above we can now define a new derived dimension::
|
||||
|
||||
struct person_per_desk : deduced_unit<person_per_desk, dim_occupancy_rate, person, desk> {};
|
||||
|
||||
template<Unit U, ScalableNumber Rep = double>
|
||||
template<UnitOf<dim_occupancy_rate> U, QuantityValue Rep = double>
|
||||
using occupancy_rate = quantity<dim_occupancy_rate, U, Rep>;
|
||||
|
||||
template<typename T>
|
||||
@@ -216,7 +216,7 @@ Such units do not share their references with base units of other systems:
|
||||
|
||||
struct dim_length : base_dimension<"L", foot> {};
|
||||
|
||||
template<Unit U, ScalableNumber Rep = double>
|
||||
template<UnitOf<dim_length> U, QuantityValue Rep = double>
|
||||
using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
} // namespace fps
|
||||
@@ -236,7 +236,7 @@ different systems:
|
||||
|
||||
struct dim_length : base_dimension<"L", metre> {};
|
||||
|
||||
template<Unit U, ScalableNumber Rep = double>
|
||||
template<UnitOf<dim_length> U, QuantityValue Rep = double>
|
||||
using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
namespace fps {
|
||||
@@ -246,7 +246,7 @@ different systems:
|
||||
|
||||
struct dim_length : base_dimension<"L", foot> {};
|
||||
|
||||
template<Unit U, ScalableNumber Rep = double>
|
||||
template<UnitOf<dim_length> U, QuantityValue Rep = double>
|
||||
using length = quantity<dim_length, U, Rep>;
|
||||
|
||||
} // namespace fps
|
||||
|
||||
Reference in New Issue
Block a user