refactor: has_unit_symbol() added

This commit is contained in:
Mateusz Pusz
2023-06-13 14:52:14 +03:00
parent 16ad71a31a
commit 9ab8136c98
3 changed files with 11 additions and 5 deletions

View File

@ -396,7 +396,7 @@ private:
if (begin == end || *begin == '}') {
// default format should print value followed by the unit separated with 1 space
out = mp_units::detail::format_units_quantity_value<CharT>(out, q.number(), specs.rep, ctx.locale());
if constexpr (!std::derived_from<decltype(get_unit(Reference)), mp_units::derived_unit<>>) {
if constexpr (mp_units::detail::has_unit_symbol(get_unit(Reference))) {
*out++ = CharT(' ');
out = unit_symbol_to<CharT>(out, get_unit(Reference));
}

View File

@ -35,7 +35,7 @@ template<typename CharT, class Traits, auto R, typename Rep>
void to_stream(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
{
os << q.number();
if constexpr (!std::derived_from<decltype(get_unit(R)), derived_unit<>>) {
if constexpr (has_unit_symbol(get_unit(R))) {
os << " ";
unit_symbol_to<CharT>(std::ostream_iterator<CharT>(os), get_unit(R));
}

View File

@ -684,6 +684,12 @@ constexpr Out print_separator(Out out, unit_symbol_formatting fmt)
return out;
}
template<Unit U>
[[nodiscard]] consteval bool has_unit_symbol(U)
{
return !std::derived_from<U, derived_unit<>>;
}
template<typename CharT, std::output_iterator<CharT> Out, Unit U>
requires requires { U::symbol; }
constexpr Out unit_symbol_impl(Out out, U, unit_symbol_formatting fmt, bool negative_power)
@ -706,11 +712,11 @@ constexpr Out unit_symbol_impl(Out out, const scaled_unit<M, U>& u, unit_symbol_
constexpr auto mag_txt = magnitude_text<M>();
out = copy<CharT>(mag_txt, fmt.encoding, out);
if constexpr (std::derived_from<std::remove_const_t<decltype(u.reference_unit)>, derived_unit<>>)
return out;
else {
if constexpr (has_unit_symbol(u.reference_unit)) {
*out++ = ' ';
return unit_symbol_impl<CharT>(out, u.reference_unit, fmt, negative_power);
} else {
return out;
}
}
}