Skip to content

Commit

Permalink
refactor: zeroth_point_origin() renamed to default_point_origin()
Browse files Browse the repository at this point in the history
… and `implicit_zeroth_point_origin` renamed to `zeroth_point_origin`
  • Loading branch information
mpusz committed Dec 22, 2023
1 parent bf51bfc commit be1705b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 97 deletions.
8 changes: 5 additions & 3 deletions docs/users_guide/framework_basics/the_affine_space.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ origin:

```cpp
template<Reference auto R,
PointOriginFor<get_quantity_spec(R)> auto PO = zeroth_point_origin(R),
PointOriginFor<get_quantity_spec(R)> auto PO = default_point_origin(R),
RepresentationOf<get_quantity_spec(R).character> Rep = double>
class quantity_point;
```
Expand All @@ -95,7 +95,7 @@ zeroth point using the following rules:

- if the measurement unit of a quantity specifies its point origin in its definition
(e.g., degree Celsius), then this point is being used,
- otherwise, an instantiation of `implicit_zeroth_point_origin<QuantitySpec>` is being used which
- otherwise, an instantiation of `zeroth_point_origin<QuantitySpec>` is being used which
provides a zeroth point for a specific quantity type.

!!! tip
Expand Down Expand Up @@ -385,10 +385,12 @@ The above can be used as an origin for subsequent _points_:
constexpr quantity_point first_climb_alt = everest_base_camp + isq::altitude(std::uint8_t{42} * m);
static_assert(first_climb_alt.quantity_from(everest_base_camp) == 42 * m);
static_assert(first_climb_alt.quantity_from(mean_sea_level) == 5406 * m);
static_assert(first_climb_alt.quantity_from_zero() == 5406 * m);
```
As we can see above, the `quantity_from()` member function returns a relative distance from the
provided point origin.
provided point origin while the `quantity_from_zero()` returns the distance from the absolute point
origin.
### Converting between different representations of the same _point_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ The **mp-units** library comes with built-in interoperability with those types.
```

1. `my_origin` is not defined in terms of `chrono_point_origin<Clock>`.
2. `implicit_zeroth_point_origin` is not defined in terms of `chrono_point_origin<Clock>`.
2. `zeroth_point_origin` is not defined in terms of `chrono_point_origin<Clock>`.

Here is an example of how interoperability described in this chapter can be used in practice:

Expand Down
28 changes: 14 additions & 14 deletions src/core/include/mp-units/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ struct relative_point_origin {
};

template<QuantitySpec auto QS>
struct implicit_zeroth_point_origin_ : absolute_point_origin<implicit_zeroth_point_origin_<QS>, QS> {};
struct zeroth_point_origin_ : absolute_point_origin<zeroth_point_origin_<QS>, QS> {};

template<QuantitySpec auto QS>
inline constexpr implicit_zeroth_point_origin_<QS> implicit_zeroth_point_origin;
inline constexpr zeroth_point_origin_<QS> zeroth_point_origin;

namespace detail {

template<PointOrigin PO>
inline constexpr bool is_specialization_of_implicit_zeroth_point_origin = false;
inline constexpr bool is_specialization_of_zeroth_point_origin = false;

template<auto QS>
inline constexpr bool is_specialization_of_implicit_zeroth_point_origin<implicit_zeroth_point_origin_<QS>> = true;
inline constexpr bool is_specialization_of_zeroth_point_origin<zeroth_point_origin_<QS>> = true;

template<PointOrigin PO>
[[nodiscard]] consteval bool is_implicit_zeroth_point_origin(PO)
[[nodiscard]] consteval bool is_zeroth_point_origin(PO)
{
return is_specialization_of_implicit_zeroth_point_origin<PO>;
return is_specialization_of_zeroth_point_origin<PO>;
}

} // namespace detail
Expand All @@ -90,7 +90,7 @@ template<PointOrigin PO1, PointOrigin PO2>
{
if constexpr (detail::AbsolutePointOrigin<PO1> && detail::AbsolutePointOrigin<PO2>)
return is_same_v<typename PO1::_type_, typename PO2::_type_> ||
(detail::is_implicit_zeroth_point_origin(po1) && detail::is_implicit_zeroth_point_origin(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;
Expand All @@ -103,12 +103,12 @@ template<PointOrigin PO1, PointOrigin PO2>
}

template<Reference R>
[[nodiscard]] consteval PointOriginFor<get_quantity_spec(R{})> auto zeroth_point_origin(R)
[[nodiscard]] consteval PointOriginFor<get_quantity_spec(R{})> auto default_point_origin(R)
{
if constexpr (requires { get_unit(R{}).point_origin; })
return get_unit(R{}).point_origin;
else
return implicit_zeroth_point_origin<get_quantity_spec(R{})>;
return zeroth_point_origin<get_quantity_spec(R{})>;
}

namespace detail {
Expand All @@ -133,7 +133,7 @@ template<PointOrigin PO>
* @tparam PO a type that represents the origin point from which the quantity point is measured from
* @tparam Rep a type to be used to represent values of a quantity point
*/
template<Reference auto R, PointOriginFor<get_quantity_spec(R)> auto PO = zeroth_point_origin(R),
template<Reference auto R, PointOriginFor<get_quantity_spec(R)> auto PO = default_point_origin(R),
RepresentationOf<get_quantity_spec(R).character> Rep = double>
class quantity_point {
public:
Expand Down Expand Up @@ -169,7 +169,7 @@ class quantity_point {

template<typename Q>
requires QuantityOf<std::remove_cvref_t<Q>, get_quantity_spec(R)> && std::constructible_from<quantity_type, Q> &&
(point_origin == zeroth_point_origin(R)) && (implicitly_convertible(Q::quantity_spec, quantity_spec))
(point_origin == default_point_origin(R)) && (implicitly_convertible(Q::quantity_spec, quantity_spec))
constexpr explicit quantity_point(Q&& q) : quantity_from_origin_is_an_implementation_detail_(std::forward<Q>(q))
{
}
Expand Down Expand Up @@ -377,7 +377,7 @@ class quantity_point {

// CTAD
template<Quantity Q>
quantity_point(Q q) -> quantity_point<Q::reference, zeroth_point_origin(Q::reference), typename Q::rep>;
quantity_point(Q q) -> quantity_point<Q::reference, default_point_origin(Q::reference), typename Q::rep>;

template<Quantity Q, PointOriginFor<Q::quantity_spec> PO>
quantity_point(Q q, PO) -> quantity_point<Q::reference, PO{}, typename Q::rep>;
Expand All @@ -396,7 +396,7 @@ template<auto R1, auto PO1, typename Rep1, auto R2, typename Rep2>
const quantity<R2, Rep2>& q)
requires requires { qp.quantity_ref_from(PO1) + q; }
{
if constexpr (detail::is_implicit_zeroth_point_origin(PO1))
if constexpr (detail::is_zeroth_point_origin(PO1))
return quantity_point{qp.quantity_ref_from(PO1) + q};
else
return quantity_point{qp.quantity_ref_from(PO1) + q, PO1};
Expand Down Expand Up @@ -433,7 +433,7 @@ template<auto R1, auto PO1, typename Rep1, auto R2, typename Rep2>
const quantity<R2, Rep2>& q)
requires requires { qp.quantity_ref_from(PO1) - q; }
{
if constexpr (detail::is_implicit_zeroth_point_origin(PO1))
if constexpr (detail::is_zeroth_point_origin(PO1))
return quantity_point{qp.quantity_ref_from(PO1) - q};
else
return quantity_point{qp.quantity_ref_from(PO1) - q, PO1};
Expand Down
Loading

0 comments on commit be1705b

Please sign in to comment.