Boost logo

Boost :

From: Pavel Chikulaev (pavel.chikulaev_at_[hidden])
Date: 2005-04-09 11:03:27


Hi all,

I'm just happened to use boost::progress_timer for the first time,
and was shocked by strange behavior of the progress_timer dtor:
Surprisingly, it puts two newlines in the stream ('\n', then endl).

So, I've got question - it is a bug or it is a feature?

Using current implementation I just can't get the output look like:
the first block took 2.34 s
the second block took 5.67 s

IMO this's a bug, and it should be fixed.

Also I would like to be able to specify that no newlines in dtor are
needed at all. This is very useful for stringstreams for example.

Here's the fixed progress_timer:

class progress_timer : public timer, private noncopyable
{
public:
explicit
progress_timer(std::ostream & os = std::cout, bool putnewline = true)
     : m_os(os), m_putnewline(putnewline) {}
  ~progress_timer()
  {
    try
    {
      std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
                                                   std::istream::floatfield );
      std::streamsize old_prec = m_os.precision( 2 );
      m_os << elapsed() << " s";
      if (m_putnewline) m_os << std::endl;
      m_os.flags( old_flags );
      m_os.precision( old_prec );
    }
    catch (...) {}
  }
 private:
  std::ostream & m_os;
  bool m_putnewline;
};
  

--
Pavel Chikulaev

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk