Skip to content

Commit

Permalink
refactor: 💥 tag types should not expose their members
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Nov 4, 2024
1 parent 8217399 commit 5810420
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 119 deletions.
6 changes: 2 additions & 4 deletions example/currency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ inline constexpr auto JPY = japanese_jen;
static_assert(!std::equality_comparable_with<quantity<euro, int>, quantity<us_dollar, int>>);


#if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
#if 0 || !MP_UNITS_API_STRING_VIEW_RET // NOLINT(readability-avoid-unconditional-preprocessor-if)

// if you have only a few currencies to handle
template<Unit auto From, Unit auto To>
Expand All @@ -77,8 +77,6 @@ template<Unit auto From, Unit auto To>

#else

[[nodiscard]] std::string_view to_string_view(Unit auto u) { return u.symbol.portable().c_str(); }

template<Unit auto From, Unit auto To>
[[nodiscard]] double exchange_rate(std::chrono::sys_seconds timestamp)
{
Expand All @@ -88,7 +86,7 @@ template<Unit auto From, Unit auto To>
// ...
};

return rates.at(std::make_pair(to_string_view(From), to_string_view(To)));
return rates.at(std::make_pair(unit_symbol(From), unit_symbol(To)));
}

#endif
Expand Down
16 changes: 8 additions & 8 deletions src/core/include/mp-units/bits/get_associated_quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ template<typename... Nums, typename... Dens>
template<AssociatedUnit U>
[[nodiscard]] consteval auto all_are_kinds(U)
{
if constexpr (requires { U::quantity_spec; })
return QuantityKindSpec<std::remove_const_t<decltype(U::quantity_spec)>>;
else if constexpr (requires { U::reference_unit; })
return all_are_kinds(U::reference_unit);
if constexpr (requires { U::_quantity_spec_; })
return QuantityKindSpec<std::remove_const_t<decltype(U::_quantity_spec_)>>;
else if constexpr (requires { U::_reference_unit_; })
return all_are_kinds(U::_reference_unit_);
else if constexpr (requires { typename U::_num_; }) {
return all_are_kinds(typename U::_num_{}, typename U::_den_{});
}
Expand All @@ -75,10 +75,10 @@ template<typename... Us>
template<AssociatedUnit U>
[[nodiscard]] consteval auto get_associated_quantity_impl(U u)
{
if constexpr (requires { U::quantity_spec; })
return remove_kind(U::quantity_spec);
else if constexpr (requires { U::reference_unit; })
return get_associated_quantity_impl(U::reference_unit);
if constexpr (requires { U::_quantity_spec_; })
return remove_kind(U::_quantity_spec_);
else if constexpr (requires { U::_reference_unit_; })
return get_associated_quantity_impl(U::_reference_unit_);
else if constexpr (requires { typename U::_num_; }) {
return expr_map<to_quantity_spec, derived_quantity_spec, struct dimensionless, type_list_of_quantity_spec_less>(u);
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/include/mp-units/framework/dimension.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct dimension_interface {
*/
MP_UNITS_EXPORT template<symbol_text Symbol>
struct base_dimension : detail::dimension_interface {
static constexpr auto symbol = Symbol; ///< Unique base dimension identifier
static constexpr auto _symbol_ = Symbol; ///< Unique base dimension identifier
};

/**
Expand Down Expand Up @@ -229,10 +229,10 @@ MP_UNITS_EXPORT_END
namespace detail {

template<typename CharT, std::output_iterator<CharT> Out, Dimension D>
requires requires { D::symbol; }
requires requires { D::_symbol_; }
constexpr Out dimension_symbol_impl(Out out, D, const dimension_symbol_formatting& fmt, bool negative_power)
{
return copy_symbol<CharT>(D::symbol, fmt.encoding, negative_power, out);
return copy_symbol<CharT>(D::_symbol_, fmt.encoding, negative_power, out);
}

template<typename CharT, std::output_iterator<CharT> Out, typename F, int Num, int... Den>
Expand Down
63 changes: 32 additions & 31 deletions src/core/include/mp-units/framework/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,40 @@ template<PointOrigin PO>
}

struct point_origin_interface {
template<PointOrigin PO, typename FwdQ, QuantityOf<PO::quantity_spec> Q = std::remove_cvref_t<FwdQ>>
template<PointOrigin PO, typename FwdQ, QuantityOf<PO::_quantity_spec_> Q = std::remove_cvref_t<FwdQ>>
[[nodiscard]] friend constexpr quantity_point<Q::reference, MP_UNITS_EXPRESSION_WORKAROUND(PO{}), typename Q::rep>
operator+(PO, FwdQ&& q)
{
return quantity_point{std::forward<FwdQ>(q), PO{}};
}

template<Quantity FwdQ, PointOrigin PO, QuantityOf<PO::quantity_spec> Q = std::remove_cvref_t<FwdQ>>
template<Quantity FwdQ, PointOrigin PO, QuantityOf<PO::_quantity_spec_> Q = std::remove_cvref_t<FwdQ>>
[[nodiscard]] friend constexpr quantity_point<Q::reference, MP_UNITS_EXPRESSION_WORKAROUND(PO{}), typename Q::rep>
operator+(FwdQ&& q, PO po)
{
return po + std::forward<FwdQ>(q);
}

template<PointOrigin PO, Quantity Q>
requires ReferenceOf<std::remove_const_t<decltype(Q::reference)>, PO::quantity_spec>
requires ReferenceOf<std::remove_const_t<decltype(Q::reference)>, PO::_quantity_spec_>
[[nodiscard]] friend constexpr QuantityPoint auto operator-(PO po, const Q& q)
requires requires { -q; }
{
return po + (-q);
}

template<PointOrigin PO1, detail::SameAbsolutePointOriginAs<PO1{}> PO2>
requires QuantitySpecOf<std::remove_const_t<decltype(PO1::quantity_spec)>, PO2::quantity_spec> &&
requires QuantitySpecOf<std::remove_const_t<decltype(PO1::_quantity_spec_)>, PO2::_quantity_spec_> &&
(is_derived_from_specialization_of_v<PO1, relative_point_origin> ||
is_derived_from_specialization_of_v<PO2, relative_point_origin>)
[[nodiscard]] friend constexpr Quantity auto operator-(PO1 po1, PO2 po2)
{
if constexpr (is_derived_from_specialization_of_v<PO1, absolute_point_origin>) {
return po1 - po2.quantity_point;
return po1 - po2._quantity_point_;
} else if constexpr (is_derived_from_specialization_of_v<PO2, absolute_point_origin>) {
return po1.quantity_point - po2;
return po1._quantity_point_ - po2;
} else {
return po1.quantity_point - po2.quantity_point;
return po1._quantity_point_ - po2._quantity_point_;
}
}

Expand All @@ -95,35 +95,35 @@ struct point_origin_interface {
if constexpr (is_derived_from_specialization_of_v<PO1, absolute_point_origin> &&
is_derived_from_specialization_of_v<PO2, absolute_point_origin>)
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));
interconvertible(po1._quantity_spec_, po2._quantity_spec_));
else if constexpr (is_derived_from_specialization_of_v<PO1, relative_point_origin> &&
is_derived_from_specialization_of_v<PO2, relative_point_origin>)
return PO1::quantity_point == PO2::quantity_point;
return PO1::_quantity_point_ == PO2::_quantity_point_;
else if constexpr (is_derived_from_specialization_of_v<PO1, relative_point_origin>)
return detail::same_absolute_point_origins(po1, po2) && is_eq_zero(PO1::quantity_point.quantity_from_zero());
return detail::same_absolute_point_origins(po1, po2) && is_eq_zero(PO1::_quantity_point_.quantity_from_zero());
else if constexpr (is_derived_from_specialization_of_v<PO2, relative_point_origin>)
return detail::same_absolute_point_origins(po1, po2) && is_eq_zero(PO2::quantity_point.quantity_from_zero());
return detail::same_absolute_point_origins(po1, po2) && is_eq_zero(PO2::_quantity_point_.quantity_from_zero());
}
};

} // namespace detail

MP_UNITS_EXPORT template<QuantitySpec auto QS>
struct absolute_point_origin : detail::point_origin_interface {
static constexpr QuantitySpec auto quantity_spec = QS;
static constexpr QuantitySpec auto _quantity_spec_ = QS;
};

MP_UNITS_EXPORT template<QuantityPoint auto QP>
struct relative_point_origin : detail::point_origin_interface {
static constexpr QuantityPoint auto quantity_point = QP;
static constexpr QuantitySpec auto quantity_spec = []() {
static constexpr QuantityPoint auto _quantity_point_ = QP;
static constexpr QuantitySpec auto _quantity_spec_ = []() {
// select the strongest of specs
if constexpr (detail::QuantityKindSpec<std::remove_const_t<decltype(QP.quantity_spec)>>)
return QP.point_origin.quantity_spec;
return QP.point_origin._quantity_spec_;
else
return QP.quantity_spec;
}();
static constexpr PointOrigin auto absolute_point_origin = QP.absolute_point_origin;
static constexpr PointOrigin auto _absolute_point_origin_ = QP.absolute_point_origin;
};

template<QuantitySpec auto QS>
Expand All @@ -143,8 +143,8 @@ constexpr bool is_specialization_of_zeroth_point_origin<zeroth_point_origin_<QS>
MP_UNITS_EXPORT template<Reference 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;
if constexpr (requires { get_unit(R{})._point_origin_; })
return get_unit(R{})._point_origin_;
else
return zeroth_point_origin<get_quantity_spec(R{})>;
}
Expand All @@ -157,7 +157,7 @@ template<PointOrigin PO>
if constexpr (is_derived_from_specialization_of_v<PO, absolute_point_origin>)
return po;
else
return po.quantity_point.absolute_point_origin;
return po._quantity_point_.absolute_point_origin;
}

} // namespace detail
Expand Down Expand Up @@ -220,7 +220,7 @@ class quantity_point {
{
}

template<typename FwdQ, PointOrigin PO2, QuantityOf<PO2::quantity_spec> Q = std::remove_cvref_t<FwdQ>>
template<typename FwdQ, PointOrigin PO2, QuantityOf<PO2::_quantity_spec_> Q = std::remove_cvref_t<FwdQ>>
requires std::constructible_from<quantity_type, FwdQ> && detail::SameAbsolutePointOriginAs<PO2, PO>
constexpr quantity_point(FwdQ&& q, PO2) :
quantity_point(
Expand Down Expand Up @@ -309,9 +309,9 @@ class quantity_point {

[[nodiscard]] constexpr Quantity auto quantity_from_zero() const
{
if constexpr (requires { unit.point_origin; }) {
if constexpr (requires { unit._point_origin_; }) {
// original quantity point unit can be lost in the below operation
const auto q = quantity_from(unit.point_origin);
const auto q = quantity_from(unit._point_origin_);
if constexpr (requires { q.in(unit); })
// restore the unit if possible (non-truncating)
return q.in(unit);
Expand Down Expand Up @@ -452,7 +452,7 @@ class quantity_point {
// binary operators on quantity points
template<std::derived_from<quantity_point> QP, auto R2, typename Rep2>
// TODO simplify when gcc catches up
requires ReferenceOf<MP_UNITS_REMOVE_CONST(decltype(R2)), PO.quantity_spec>
requires ReferenceOf<MP_UNITS_REMOVE_CONST(decltype(R2)), PO._quantity_spec_>
[[nodiscard]] friend constexpr QuantityPoint auto operator+(const QP& qp, const quantity<R2, Rep2>& q)
requires requires { qp.quantity_ref_from(PO) + q; }
{
Expand All @@ -464,7 +464,7 @@ class quantity_point {

template<auto R1, typename Rep1, std::derived_from<quantity_point> QP>
// TODO simplify when gcc catches up
requires ReferenceOf<MP_UNITS_REMOVE_CONST(decltype(R1)), PO.quantity_spec>
requires ReferenceOf<MP_UNITS_REMOVE_CONST(decltype(R1)), PO._quantity_spec_>
[[nodiscard]] friend constexpr QuantityPoint auto operator+(const quantity<R1, Rep1>& q, const QP& qp)
requires requires { q + qp.quantity_ref_from(PO); }
{
Expand All @@ -473,7 +473,7 @@ class quantity_point {

template<std::derived_from<quantity_point> QP, auto R2, typename Rep2>
// TODO simplify when gcc catches up
requires ReferenceOf<MP_UNITS_REMOVE_CONST(decltype(R2)), PO.quantity_spec>
requires ReferenceOf<MP_UNITS_REMOVE_CONST(decltype(R2)), PO._quantity_spec_>
[[nodiscard]] friend constexpr QuantityPoint auto operator-(const QP& qp, const quantity<R2, Rep2>& q)
requires requires { qp.quantity_ref_from(PO) - q; }
{
Expand All @@ -497,7 +497,7 @@ class quantity_point {

template<std::derived_from<quantity_point> QP, PointOrigin PO2>
requires QuantityPointOf<quantity_point, PO2{}> &&
ReferenceOf<std::remove_const_t<decltype(reference)>, PO2::quantity_spec>
ReferenceOf<std::remove_const_t<decltype(reference)>, PO2::_quantity_spec_>
[[nodiscard]] friend constexpr Quantity auto operator-(const QP& qp, PO2 po)
{
if constexpr (point_origin == po)
Expand All @@ -508,18 +508,19 @@ class quantity_point {
else
return qp.quantity_ref_from(point_origin) + (qp.point_origin - qp.absolute_point_origin);
} else {
if constexpr (point_origin == po.quantity_point.point_origin)
return qp.quantity_ref_from(point_origin) - po.quantity_point.quantity_ref_from(po.quantity_point.point_origin);
if constexpr (point_origin == po._quantity_point_.point_origin)
return qp.quantity_ref_from(point_origin) -
po._quantity_point_.quantity_ref_from(po._quantity_point_.point_origin);
else
return qp.quantity_ref_from(point_origin) -
po.quantity_point.quantity_ref_from(po.quantity_point.point_origin) +
(qp.point_origin - po.quantity_point.point_origin);
po._quantity_point_.quantity_ref_from(po._quantity_point_.point_origin) +
(qp.point_origin - po._quantity_point_.point_origin);
}
}

template<PointOrigin PO1, std::derived_from<quantity_point> QP>
requires QuantityPointOf<quantity_point, PO1{}> &&
ReferenceOf<std::remove_const_t<decltype(reference)>, PO1::quantity_spec>
ReferenceOf<std::remove_const_t<decltype(reference)>, PO1::_quantity_spec_>
[[nodiscard]] friend constexpr Quantity auto operator-(PO1 po, const QP& qp)
{
return -(qp - po);
Expand Down
8 changes: 4 additions & 4 deletions src/core/include/mp-units/framework/quantity_point_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ concept PointOrigin = detail::TagType<T> && std::derived_from<T, detail::point_o
* Satisfied by all quantity point origins that are defined using a provided quantity specification.
*/
MP_UNITS_EXPORT template<typename T, auto QS>
concept PointOriginFor = PointOrigin<T> && QuantitySpecOf<MP_UNITS_REMOVE_CONST(decltype(QS)), T::quantity_spec>;
concept PointOriginFor = PointOrigin<T> && QuantitySpecOf<MP_UNITS_REMOVE_CONST(decltype(QS)), T::_quantity_spec_>;

MP_UNITS_EXPORT template<Reference auto R, PointOriginFor<get_quantity_spec(R)> auto PO,
RepresentationOf<get_quantity_spec(R).character> Rep>
Expand All @@ -100,11 +100,11 @@ template<PointOrigin PO1, PointOrigin PO2>
return po1 == po2;
else if constexpr (is_derived_from_specialization_of_v<PO1, relative_point_origin> &&
is_derived_from_specialization_of_v<PO2, relative_point_origin>)
return po1.absolute_point_origin == po2.absolute_point_origin;
return po1._absolute_point_origin_ == po2._absolute_point_origin_;
else if constexpr (is_derived_from_specialization_of_v<PO1, relative_point_origin>)
return po1.absolute_point_origin == po2;
return po1._absolute_point_origin_ == po2;
else if constexpr (is_derived_from_specialization_of_v<PO2, relative_point_origin>)
return po1 == po2.absolute_point_origin;
return po1 == po2._absolute_point_origin_;
else
return false;
}
Expand Down
Loading

0 comments on commit 5810420

Please sign in to comment.