Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mpusz/mp-units
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Nov 4, 2024
2 parents 21e0f9b + f461cf4 commit 614ecb5
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 140 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
22 changes: 11 additions & 11 deletions src/core/include/mp-units/framework/quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ concept SameValueAs = (equivalent(get_unit(R1), get_unit(R2))) && std::convertib
template<typename T>
using quantity_like_type = quantity<quantity_like_traits<T>::reference, typename quantity_like_traits<T>::rep>;

template<typename T, typename U>
concept Forwarding = std::derived_from<std::remove_cvref_t<T>, U>;
template<typename T, typename U, typename TT = std::remove_reference_t<T>>
concept Mutable = (!std::is_const_v<TT>) && std::derived_from<TT, U>;

} // namespace detail

Expand Down Expand Up @@ -338,7 +338,7 @@ class quantity {
return ::mp_units::quantity{-numerical_value_is_an_implementation_detail_, reference};
}

template<detail::Forwarding<quantity> Q>
template<detail::Mutable<quantity> Q>
friend constexpr decltype(auto) operator++(Q&& q)
requires requires(rep v) {
{ ++v } -> std::same_as<rep&>;
Expand All @@ -356,7 +356,7 @@ class quantity {
return ::mp_units::quantity{numerical_value_is_an_implementation_detail_++, reference};
}

template<detail::Forwarding<quantity> Q>
template<detail::Mutable<quantity> Q>
friend constexpr decltype(auto) operator--(Q&& q)
requires requires(rep v) {
{ --v } -> std::same_as<rep&>;
Expand All @@ -375,7 +375,7 @@ class quantity {
}

// compound assignment operators
template<detail::Forwarding<quantity> Q, auto R2, typename Rep2>
template<detail::Mutable<quantity> Q, auto R2, typename Rep2>
requires detail::QuantityConvertibleTo<quantity<R2, Rep2>, quantity> && requires(rep a, Rep2 b) {
{ a += b } -> std::same_as<rep&>;
}
Expand All @@ -388,7 +388,7 @@ class quantity {
return std::forward<Q>(lhs);
}

template<detail::Forwarding<quantity> Q, auto R2, typename Rep2>
template<detail::Mutable<quantity> Q, auto R2, typename Rep2>
requires detail::QuantityConvertibleTo<quantity<R2, Rep2>, quantity> && requires(rep a, Rep2 b) {
{ a -= b } -> std::same_as<rep&>;
}
Expand All @@ -401,7 +401,7 @@ class quantity {
return std::forward<Q>(lhs);
}

template<detail::Forwarding<quantity> Q, auto R2, typename Rep2>
template<detail::Mutable<quantity> Q, auto R2, typename Rep2>
requires detail::QuantityConvertibleTo<quantity<R2, Rep2>, quantity> && (!treat_as_floating_point<rep>) &&
requires(rep a, Rep2 b) {
{ a %= b } -> std::same_as<rep&>;
Expand All @@ -417,7 +417,7 @@ class quantity {
return std::forward<Q>(lhs);
}

template<detail::Forwarding<quantity> Q, detail::ValuePreservingTo<Rep> Value>
template<detail::Mutable<quantity> Q, detail::ValuePreservingTo<Rep> Value>
requires(!Quantity<Value>) && requires(rep a, Value b) {
{ a *= b } -> std::same_as<rep&>;
}
Expand All @@ -429,7 +429,7 @@ class quantity {
return std::forward<Q>(lhs);
}

template<detail::Forwarding<quantity> Q1, QuantityOf<dimensionless> Q2>
template<detail::Mutable<quantity> Q1, QuantityOf<dimensionless> Q2>
requires(Q2::unit == ::mp_units::one) && detail::ValuePreservingTo<typename Q2::rep, Rep> &&
requires(rep a, Q2::rep b) {
{ a *= b } -> std::same_as<rep&>;
Expand All @@ -439,7 +439,7 @@ class quantity {
return std::forward<Q1>(lhs) *= rhs.numerical_value_is_an_implementation_detail_;
}

template<detail::Forwarding<quantity> Q, detail::ValuePreservingTo<Rep> Value>
template<detail::Mutable<quantity> Q, detail::ValuePreservingTo<Rep> Value>
requires(!Quantity<Value>) && requires(rep a, Value b) {
{ a /= b } -> std::same_as<rep&>;
}
Expand All @@ -452,7 +452,7 @@ class quantity {
return std::forward<Q>(lhs);
}

template<detail::Forwarding<quantity> Q1, QuantityOf<dimensionless> Q2>
template<detail::Mutable<quantity> Q1, QuantityOf<dimensionless> Q2>
requires(Q2::unit == ::mp_units::one) && detail::ValuePreservingTo<typename Q2::rep, Rep> &&
requires(rep a, Q2::rep b) {
{ a /= b } -> std::same_as<rep&>;
Expand Down
6 changes: 4 additions & 2 deletions src/core/include/mp-units/framework/quantity_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ template<typename T, template<typename> typename Traits>
concept QuantityLikeImpl = requires(const T& qty, const Traits<T>::rep& num) {
{ Traits<T>::to_numerical_value(qty) } -> std::same_as<typename Traits<T>::rep>;
{ Traits<T>::from_numerical_value(num) } -> std::same_as<T>;
{ Traits<T>::explicit_import } -> std::convertible_to<bool>;
{ Traits<T>::explicit_export } -> std::convertible_to<bool>;
requires std::same_as<decltype(Traits<T>::explicit_import), const bool>;
requires std::same_as<decltype(Traits<T>::explicit_export), const bool>;
typename std::bool_constant<Traits<T>::explicit_import>;
typename std::bool_constant<Traits<T>::explicit_export>;
};

} // namespace detail
Expand Down
Loading

0 comments on commit 614ecb5

Please sign in to comment.