feat: unit text output support added

Resolves #422
This commit is contained in:
Mateusz Pusz
2024-01-23 21:19:45 +01:00
parent 1817687ca2
commit 1e43758b54

View File

@@ -31,6 +31,12 @@ namespace mp_units {
namespace detail { namespace detail {
template<typename CharT, class Traits, Unit U>
void to_stream(std::basic_ostream<CharT, Traits>& os, U u)
{
unit_symbol_to<CharT>(std::ostream_iterator<CharT>(os), u);
}
template<typename CharT, class Traits, auto R, typename Rep> template<typename CharT, class Traits, auto R, typename Rep>
void to_stream(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q) void to_stream(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
{ {
@@ -40,11 +46,28 @@ void to_stream(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
else else
os << q.numerical_value_ref_in(q.unit); os << q.numerical_value_ref_in(q.unit);
if constexpr (space_before_unit_symbol<get_unit(R)>) os << " "; if constexpr (space_before_unit_symbol<get_unit(R)>) os << " ";
unit_symbol_to<CharT>(std::ostream_iterator<CharT>(os), get_unit(R)); to_stream(os, get_unit(R));
} }
} // namespace detail } // namespace detail
template<typename CharT, typename Traits, Unit U>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, U u)
{
if (os.width()) {
// std::setw() applies to the whole uni output so it has to be first put into std::string
std::basic_ostringstream<CharT, Traits> oss;
oss.flags(os.flags());
oss.imbue(os.getloc());
oss.precision(os.precision());
detail::to_stream(oss, u);
return os << std::move(oss).str();
}
detail::to_stream(os, u);
return os;
}
template<typename CharT, typename Traits, auto R, typename Rep> template<typename CharT, typename Traits, auto R, typename Rep>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q) std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
requires requires { os << q.numerical_value_ref_in(q.unit); } requires requires { os << q.numerical_value_ref_in(q.unit); }