--- progress.hpp 2004-07-25 14:00:07.000000000 +0200 +++ /cygdrive/c/projects/boost/include/boost-1_33/boost/progress.hpp 2006-02-13 14:06:53.856875000 +0100 @@ -7,6 +7,8 @@ // See http://www.boost.org/libs/timer for documentation. // Revision History +// 13 Feb 06 Made it possible to customize output from progress_timer through +// user-supplied function object (Daniel Lidstrom). // 1 Dec 01 Add leading progress display strings (suggested by Toon Knapen) // 20 May 01 Introduce several static_casts<> to eliminate warning messages // (Fixed by Beman, reported by Herve Bronnimann) @@ -22,8 +24,10 @@ #include #include // for noncopyable #include // for uintmax_t +#include // for function #include // for ostream, cout, etc #include // for string +#include // for stringstream namespace boost { @@ -36,9 +40,27 @@ { public: - explicit progress_timer( std::ostream & os = std::cout ) + + // This is the default formatter. It shows the value with two decimals and appends " s". + struct default_si_formatter + { + std::string operator()(double s) const + { + // use istream instead of ios_base to workaround GNU problem (Greg Chicares) + std::stringstream stream; + stream.setf( std::istream::fixed, std::istream::floatfield ); + stream.precision( 2 ); + stream << s << " s" // "s" is System International d'Unités std + << std::endl; + return stream.str(); + } + }; + + explicit progress_timer( std::ostream & os = std::cout, + boost::function formatter = default_si_formatter() ) // os is hint; implementation may ignore, particularly in embedded systems - : m_os(os) {} + : m_os(os), m_formatter(formatter) + {} ~progress_timer() { // A) Throwing an exception from a destructor is a Bad Thing. @@ -47,14 +69,7 @@ // Therefore, wrap the I/O in a try block, catch and ignore all exceptions. try { - // use istream instead of ios_base to workaround GNU problem (Greg Chicares) - 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\n" // "s" is System International d'Unités std - << std::endl; - m_os.flags( old_flags ); - m_os.precision( old_prec ); + m_os << m_formatter(elapsed()); } catch (...) {} // eat any exceptions @@ -62,6 +77,7 @@ private: std::ostream & m_os; + boost::function m_formatter; };