Skip to content

Commit

Permalink
refactor: 💥 from now on point origins must be marked final + `absol…
Browse files Browse the repository at this point in the history
…ute_point_origin` does not use CRTP anymore
  • Loading branch information
mpusz committed Jun 13, 2024
1 parent bd6b1e6 commit 05fc1e2
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 97 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- (!) feat: ABI concerns resolved with introduction of u8 strings for symbols
- (!) feat: API-related Conan, CMake, and preprocessor options redesigned
- (!) feat: `core.h` removed
- (!) feat: from now on units, dimensions, and quantity specifications have to be marked as `final`
- (!) feat: from now on units, dimensions, quantity specifications, and point origins have to be marked as `final`
- feat: implicit point origins support added
- feat: unit default point origin support added
- feat: `fma`, `isfinite`, `isinf`, and `isnan` math function added by [@NAThompson](https://github.com/NAThompson)
Expand Down Expand Up @@ -47,6 +47,7 @@
- (!) refactor: `framework.h` introduced
- (!) refactor: type list tools made an implementation detail of the library
- (!) refactor: header files with the entire system definitions moved up in the directory tree
- (!) refactor: `absolute_point_origin` does not use CRTP anymore
- refactor: system's units do not inherit from one another anymore
- refactor: all units made `final`
- refactor: math functions constraints refactored
Expand Down
26 changes: 22 additions & 4 deletions docs/blog/posts/2.2.0-released.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ origins. For example:
=== "Before"

```cpp
constexpr struct zero : absolute_point_origin<zero, currency> {} zero;
constexpr struct zero final : absolute_point_origin<currency> {} zero;

quantity_point price_usd = zero + 100 * USD;
```
Expand Down Expand Up @@ -300,25 +300,43 @@ is why it was renamed to `symbol_text` (:boom: **breaking change** :boom:).
```


## Changes to dimension and quantity specification definitions
## Changes to dimension, quantity specification, and point origins definitions

Similarly to units, now also all dimensions and quantity specifications have to be marked final
(:boom: **breaking change** :boom:).
Similarly to units, now also all dimensions, quantity specifications, and point origins have to be
marked `final` (:boom: **breaking change** :boom:).

=== "Now"

```cpp
inline constexpr struct dim_length final : base_dimension<"L"> {} dim_length;
inline constexpr struct length final : quantity_spec<dim_length> {} length;

inline constexpr struct absolute_zero final : absolute_point_origin<isq::thermodynamic_temperature> {} absolute_zero;
inline constexpr auto zeroth_kelvin = absolute_zero;
inline constexpr struct kelvin final : named_unit<"K", kind_of<isq::thermodynamic_temperature>, zeroth_kelvin> {} kelvin;

inline constexpr struct ice_point final : relative_point_origin<quantity_point{273'150 * milli<kelvin>}> {} ice_point;
inline constexpr auto zeroth_degree_Celsius = ice_point;
inline constexpr struct degree_Celsius final : named_unit<symbol_text{u8"°C", "`C"}, kelvin, zeroth_degree_Celsius> {} degree_Celsius;
```

=== "Before"

```cpp
inline constexpr struct dim_length : base_dimension<"L"> {} dim_length;
inline constexpr struct length : quantity_spec<dim_length> {} length;

inline constexpr struct absolute_zero : absolute_point_origin<absolute_zero, isq::thermodynamic_temperature> {} absolute_zero;
inline constexpr struct zeroth_kelvin : decltype(absolute_zero) {} zeroth_kelvin;
inline constexpr struct kelvin : named_unit<"K", kind_of<isq::thermodynamic_temperature>, zeroth_kelvin> {} kelvin;

inline constexpr struct ice_point : relative_point_origin<quantity_point{273'150 * milli<kelvin>}> {} ice_point;
inline constexpr struct zeroth_degree_Celsius : decltype(ice_point) {} zeroth_degree_Celsius;
inline constexpr struct degree_Celsius : named_unit<symbol_text{u8"°C", "`C"}, kelvin, zeroth_degree_Celsius> {} degree_Celsius;
```

Please also note, that the `absolute_point_origin` does not use CRTP idiom anymore (:boom: **breaking change** :boom:).


## Improved text output

Expand Down
8 changes: 6 additions & 2 deletions docs/users_guide/framework_basics/concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ or derived [quantity](../../appendix/glossary.md#quantity):
by the library's framework based on the [quantity equation](../../appendix/glossary.md#quantity-equation)
provided in the [quantity specification](../../appendix/glossary.md#quantity_spec).

All of the above dimensions have to be marked as `final`.


### `DimensionOf<T, V>` { #DimensionOf }

Expand All @@ -42,6 +44,8 @@ including:
- Intermediate [derived quantity](../../appendix/glossary.md#derived-quantity) specifications being
a result of a [quantity equations](../../appendix/glossary.md#quantity-equation) on other specifications.

All of the above quantity specifications have to be marked as `final`.


### `QuantitySpecOf<T, V>` { #QuantitySpecOf }

Expand Down Expand Up @@ -237,7 +241,7 @@ implicitly convertible from quantity specification `V`, which means that `V` mus
However, if we define `mean_sea_level` in the following way:

```cpp
inline constexpr struct mean_sea_level : absolute_point_origin<isq::altitude> {} mean_sea_level;
inline constexpr struct mean_sea_level final : absolute_point_origin<isq::altitude> {} mean_sea_level;
```

then it can't be used as a point origin for _points_ of `isq::length` or `isq::width` as none of them
Expand Down Expand Up @@ -330,7 +334,7 @@ for which an instantiation of `quantity_point_like_traits` type trait yields a v
struct mp_units::quantity_point_like_traits<std::chrono::time_point<C, std::chrono::seconds>> {
using T = std::chrono::time_point<C, std::chrono::seconds>;
static constexpr auto reference = si::second;
static constexpr struct point_origin : absolute_point_origin<isq::time> {} point_origin{};
static constexpr struct point_origin final : absolute_point_origin<isq::time> {} point_origin{};
using rep = std::chrono::seconds::rep;

[[nodiscard]] static constexpr convert_implicitly<quantity<reference, rep>> to_quantity(const T& qp)
Expand Down
4 changes: 2 additions & 2 deletions docs/users_guide/framework_basics/design_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,13 @@ For example:
- the absolute point origin can be defined in the following way:

```cpp
inline constexpr struct absolute_zero : absolute_point_origin<isq::thermodynamic_temperature> {} absolute_zero;
inline constexpr struct absolute_zero final : absolute_point_origin<isq::thermodynamic_temperature> {} absolute_zero;
```
- the relative point origin can be defined in the following way:
```cpp
inline constexpr struct ice_point : relative_point_origin<absolute_zero + 273'150 * milli<kelvin>> {} ice_point;
inline constexpr struct ice_point final : relative_point_origin<absolute_zero + 273'150 * milli<kelvin>> {} ice_point;
```


Expand Down
34 changes: 13 additions & 21 deletions docs/users_guide/framework_basics/the_affine_space.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ origin.
![affine_space_2](affine_space_2.svg){style="width:80%;display: block;margin: 0 auto;"}

```cpp
inline constexpr struct origin : absolute_point_origin<origin, isq::distance> {} origin;
inline constexpr struct origin final : absolute_point_origin<isq::distance> {} origin;

// quantity_point<si::metre, origin> qp1{100 * m}; // Compile-time error
// quantity_point<si::metre, origin> qp2{120 * m}; // Compile-time error
Expand All @@ -197,14 +197,6 @@ assert(origin - qp2 == -120 * m);
// assert(origin - origin == 0 * m); // Compile-time error
```
!!! info
The `absolute_point_origin` class template uses the CRTP idiom to enforce the uniqueness of
such a type. You should pass the type of a derived class as the first argument of the template
instantiation.
*[CRTP]: Curiously Recurring Template Parameter
We can't construct a quantity point directly from the quantity anymore when a custom, named origin
is used. To prevent potential safety and maintenance issues, we always need to
explicitly provide both a compatible origin and a quantity measured from it to construct a quantity
Expand Down Expand Up @@ -249,8 +241,8 @@ type and unit is being used:
![affine_space_3](affine_space_3.svg){style="width:80%;display: block;margin: 0 auto;"}

```cpp
inline constexpr struct origin1 : absolute_point_origin<origin1, isq::distance> {} origin1;
inline constexpr struct origin2 : absolute_point_origin<origin2, isq::distance> {} origin2;
inline constexpr struct origin1 final : absolute_point_origin<isq::distance> {} origin1;
inline constexpr struct origin2 final : absolute_point_origin<isq::distance> {} origin2;

quantity_point qp1 = origin1 + 100 * m;
quantity_point qp2 = origin2 + 120 * m;
Expand Down Expand Up @@ -284,10 +276,10 @@ For such cases, relative point origins should be used:
![affine_space_4](affine_space_4.svg){style="width:80%;display: block;margin: 0 auto;"}
```cpp
inline constexpr struct A : absolute_point_origin<A, isq::distance> {} A;
inline constexpr struct B : relative_point_origin<A + 10 * m> {} B;
inline constexpr struct C : relative_point_origin<B + 10 * m> {} C;
inline constexpr struct D : relative_point_origin<A + 30 * m> {} D;
inline constexpr struct A final : absolute_point_origin<isq::distance> {} A;
inline constexpr struct B final : relative_point_origin<A + 10 * m> {} B;
inline constexpr struct C final : relative_point_origin<B + 10 * m> {} C;
inline constexpr struct D final : relative_point_origin<A + 30 * m> {} D;
quantity_point qp1 = C + 100 * m;
quantity_point qp2 = D + 120 * m;
Expand Down Expand Up @@ -392,17 +384,17 @@ point origins for this purpose:
```cpp
namespace si {
inline constexpr struct absolute_zero : absolute_point_origin<absolute_zero, isq::thermodynamic_temperature> {} absolute_zero;
inline constexpr struct zeroth_kelvin : decltype(absolute_zero) {} zeroth_kelvin;
inline constexpr struct absolute_zero final : absolute_point_origin<isq::thermodynamic_temperature> {} absolute_zero;
inline constexpr auto zeroth_kelvin = absolute_zero;
inline constexpr struct ice_point : relative_point_origin<quantity_point{273'150 * milli<kelvin>}> {} ice_point;
inline constexpr struct zeroth_degree_Celsius : decltype(ice_point) {} zeroth_degree_Celsius;
inline constexpr struct ice_point final : relative_point_origin<quantity_point{273'150 * milli<kelvin>}> {} ice_point;
inline constexpr auto zeroth_degree_Celsius = ice_point;
}
namespace usc {
inline constexpr struct zeroth_degree_Fahrenheit :
inline constexpr struct zeroth_degree_Fahrenheit final :
relative_point_origin<quantity_point{-32 * (mag_ratio<5, 9> * si::degree_Celsius)}> {} zeroth_degree_Fahrenheit;
}
Expand Down Expand Up @@ -481,7 +473,7 @@ the following way:
![affine_space_6](affine_space_6.svg){style="width:80%;display: block;margin: 0 auto;"}
```cpp
constexpr struct room_reference_temp : relative_point_origin<quantity_point{21 * deg_C}> {} room_reference_temp;
constexpr struct room_reference_temp final : relative_point_origin<quantity_point{21 * deg_C}> {} room_reference_temp;
using room_temp = quantity_point<isq::Celsius_temperature[deg_C], room_reference_temp>;
constexpr auto step_delta = isq::Celsius_temperature(0.5 * deg_C);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ include the _mp-units/systems/si/chrono.h_ file to benefit from it. This file pr
to the `std::chrono` abstractions:

```cpp
inline constexpr struct ts_origin : relative_point_origin<chrono_point_origin<system_clock> + 1 * h> {} ts_origin;
inline constexpr struct my_origin : absolute_point_origin<my_origin, isq::time> {} my_origin;
inline constexpr struct ts_origin final : relative_point_origin<chrono_point_origin<system_clock> + 1 * h> {} ts_origin;
inline constexpr struct my_origin final : absolute_point_origin<isq::time> {} my_origin;

quantity_point qp1 = sys_seconds{1s};
auto tp1 = to_chrono_time_point(qp1); // OK
Expand Down
7 changes: 3 additions & 4 deletions example/include/geographic.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import mp_units;

namespace geographic {

inline constexpr struct mean_sea_level : mp_units::absolute_point_origin<mean_sea_level, mp_units::isq::altitude> {
inline constexpr struct mean_sea_level final : mp_units::absolute_point_origin<mp_units::isq::altitude> {
} mean_sea_level;

using msl_altitude = mp_units::quantity_point<mp_units::isq::altitude[mp_units::si::metre], mean_sea_level>;
Expand Down Expand Up @@ -68,10 +68,9 @@ struct MP_UNITS_STD_FMT::formatter<geographic::msl_altitude, Char> :

namespace geographic {

inline constexpr struct equator : mp_units::absolute_point_origin<equator, mp_units::isq::angular_measure> {
inline constexpr struct equator final : mp_units::absolute_point_origin<mp_units::isq::angular_measure> {
} equator;
inline constexpr struct prime_meridian :
mp_units::absolute_point_origin<prime_meridian, mp_units::isq::angular_measure> {
inline constexpr struct prime_meridian final : mp_units::absolute_point_origin<mp_units::isq::angular_measure> {
} prime_meridian;


Expand Down
4 changes: 2 additions & 2 deletions example/unmanned_aerial_vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using namespace geographic;
enum class earth_gravity_model : std::int8_t { egm84_15, egm95_5, egm2008_1 };

template<earth_gravity_model M>
struct height_above_ellipsoid_t : absolute_point_origin<height_above_ellipsoid_t<M>, isq::altitude> {
struct height_above_ellipsoid_t final : absolute_point_origin<isq::altitude> {
static constexpr earth_gravity_model egm = M;
};
template<earth_gravity_model M>
Expand Down Expand Up @@ -115,7 +115,7 @@ hae_altitude<M> to_hae(msl_altitude msl, position<long double> pos)
// **** HAL ****

// clang-format off
inline constexpr struct height_above_launch : absolute_point_origin<height_above_launch, isq::altitude> {} height_above_launch;
inline constexpr struct height_above_launch final : absolute_point_origin<isq::altitude> {} height_above_launch;
// clang-format on

using hal_altitude = quantity_point<isq::altitude[si::metre], height_above_launch>;
Expand Down
10 changes: 4 additions & 6 deletions src/core/include/mp-units/framework/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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;
Expand All @@ -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>)
Expand Down
24 changes: 5 additions & 19 deletions src/core/include/mp-units/framework/quantity_point_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,23 @@

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 {

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

Expand All @@ -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>*);

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/systems/include/mp-units/systems/si/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct quantity_like_traits<std::chrono::duration<Rep, Period>> {
};

template<typename C>
struct chrono_point_origin_ : absolute_point_origin<chrono_point_origin_<C>, isq::time> {
struct chrono_point_origin_ final : absolute_point_origin<isq::time> {
using clock = C;
};
MP_UNITS_EXPORT template<typename C>
Expand Down
8 changes: 4 additions & 4 deletions src/systems/include/mp-units/systems/si/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ inline constexpr struct gram final : named_unit<"g", kind_of<isq::mass>> {} gram
inline constexpr auto kilogram = kilo<gram>;
inline constexpr struct ampere final : named_unit<"A", kind_of<isq::electric_current>> {} ampere;

inline constexpr struct absolute_zero : absolute_point_origin<absolute_zero, isq::thermodynamic_temperature> {} absolute_zero;
inline constexpr struct zeroth_kelvin : decltype(absolute_zero) {} zeroth_kelvin;
inline constexpr struct absolute_zero final : absolute_point_origin<isq::thermodynamic_temperature> {} absolute_zero;
inline constexpr auto zeroth_kelvin = absolute_zero;
inline constexpr struct kelvin final : named_unit<"K", kind_of<isq::thermodynamic_temperature>, zeroth_kelvin> {} kelvin;

inline constexpr struct mole final : named_unit<"mol", kind_of<isq::amount_of_substance>> {} mole;
Expand Down Expand Up @@ -77,8 +77,8 @@ inline constexpr struct weber final : named_unit<"Wb", volt * second> {} weber;
inline constexpr struct tesla final : named_unit<"T", weber / square(metre)> {} tesla;
inline constexpr struct henry final : named_unit<"H", weber / ampere> {} henry;

inline constexpr struct ice_point : relative_point_origin<quantity_point{273'150 * milli<kelvin>}> {} ice_point;
inline constexpr struct zeroth_degree_Celsius : decltype(ice_point) {} zeroth_degree_Celsius;
inline constexpr struct ice_point final : relative_point_origin<quantity_point{273'150 * milli<kelvin>}> {} ice_point;
inline constexpr auto zeroth_degree_Celsius = ice_point;
inline constexpr struct degree_Celsius final : named_unit<symbol_text{u8"°C", "`C"}, kelvin, zeroth_degree_Celsius> {} degree_Celsius;

inline constexpr struct lumen final : named_unit<"lm", candela * steradian> {} lumen;
Expand Down
2 changes: 1 addition & 1 deletion src/systems/include/mp-units/systems/usc.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ inline constexpr struct troy_pound final : named_unit<"lb t", mag<12> * troy_onc
inline constexpr struct inch_of_mercury final : named_unit<"inHg", mag_ratio<3'386'389, 1'000> * si::pascal> {} inch_of_mercury;

// https://en.wikipedia.org/wiki/United_States_customary_units#Temperature
inline constexpr struct zeroth_degree_Fahrenheit : relative_point_origin<quantity_point{-32 * (mag_ratio<5, 9> * si::degree_Celsius)}> {} zeroth_degree_Fahrenheit;
inline constexpr struct zeroth_degree_Fahrenheit final : relative_point_origin<quantity_point{-32 * (mag_ratio<5, 9> * si::degree_Celsius)}> {} zeroth_degree_Fahrenheit;
inline constexpr struct degree_Fahrenheit final : named_unit<symbol_text{u8"°F", "`F"}, mag_ratio<5, 9> * si::degree_Celsius, zeroth_degree_Fahrenheit> {} degree_Fahrenheit;

// clang-format on
Expand Down
Loading

0 comments on commit 05fc1e2

Please sign in to comment.