Ticket #7261: io.hpp.diff

File io.hpp.diff, 1.8 KB (added by Paul A. Bristow, 10 years ago)

Unified diff for units/boost/io.hpp

  • .hpp

    old new  
    10411041template<class Char, class Traits, class Unit, class T>
    10421042inline std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, const quantity<Unit, T>& q)
    10431043{
     1044    std::ostringstream oss;
     1045    // if os.width() > 0 then need to build a single string of the quantity
     1046    // with the width specified using ostringstream
     1047    // (otherwise, outputing value and unit (perhaps autoprefixed to km)
     1048    // severally would overflow the width).
     1049    // else output directly to ostream os.
     1050
    10441051    if (units::get_autoprefix(os) == autoprefix_none)
    10451052    {
    1046         os << q.value() << ' ' << Unit();
     1053      ((os.width()> 0) ? oss : os) << q.value() << ' ' << Unit();
    10471054    }
    10481055    else if (units::get_autoprefix(os) == autoprefix_engineering)
    10491056    {
    1050         detail::maybe_print_prefixed<detail::engineering_prefixes>(os, q, detail::test_norm(autoprefix_norm(q.value())));
     1057        detail::maybe_print_prefixed<detail::engineering_prefixes>(((os.width() > 0) ? oss : os), q, detail::test_norm(autoprefix_norm(q.value())));
    10511058    }
    10521059    else if (units::get_autoprefix(os) == autoprefix_binary)
    10531060    {
    1054         detail::maybe_print_prefixed<detail::binary_prefixes>(os, q, detail::test_norm(autoprefix_norm(q.value())));
     1061        detail::maybe_print_prefixed<detail::binary_prefixes>(((os.width() > 0) ? oss : os), q, detail::test_norm(autoprefix_norm(q.value())));
    10551062    }
    10561063    else
    10571064    {
    10581065        assert(!"Autoprefixing must be one of: no_prefix, engineering_prefix, binary_prefix");
    10591066    }
     1067    if (os.width() > 0)
     1068    {
     1069      os << oss.str();  // Would be "" string if oss not used.
     1070    }
     1071
    10601072    return(os);
    10611073}
    10621074