Chris@16: // boost progress.hpp header file ------------------------------------------// Chris@16: Chris@16: // Copyright Beman Dawes 1994-99. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/timer for documentation. Chris@16: Chris@16: // Revision History Chris@16: // 1 Dec 01 Add leading progress display strings (suggested by Toon Knapen) Chris@16: // 20 May 01 Introduce several static_casts<> to eliminate warning messages Chris@16: // (Fixed by Beman, reported by Herve Bronnimann) Chris@16: // 12 Jan 01 Change to inline implementation to allow use without library Chris@16: // builds. See docs for more rationale. (Beman Dawes) Chris@16: // 22 Jul 99 Name changed to .hpp Chris@16: // 16 Jul 99 Second beta Chris@16: // 6 Jul 99 Initial boost version Chris@16: Chris@16: #ifndef BOOST_PROGRESS_HPP Chris@16: #define BOOST_PROGRESS_HPP Chris@16: Chris@16: #include Chris@101: #include Chris@16: #include // for uintmax_t Chris@16: #include // for ostream, cout, etc Chris@16: #include // for string Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: // progress_timer ----------------------------------------------------------// Chris@16: Chris@16: // A progress_timer behaves like a timer except that the destructor displays Chris@16: // an elapsed time message at an appropriate place in an appropriate form. Chris@16: Chris@16: class progress_timer : public timer, private noncopyable Chris@16: { Chris@16: Chris@16: public: Chris@16: explicit progress_timer( std::ostream & os = std::cout ) Chris@16: // os is hint; implementation may ignore, particularly in embedded systems Chris@16: : timer(), noncopyable(), m_os(os) {} Chris@16: ~progress_timer() Chris@16: { Chris@16: // A) Throwing an exception from a destructor is a Bad Thing. Chris@16: // B) The progress_timer destructor does output which may throw. Chris@16: // C) A progress_timer is usually not critical to the application. Chris@16: // Therefore, wrap the I/O in a try block, catch and ignore all exceptions. Chris@16: try Chris@16: { Chris@16: // use istream instead of ios_base to workaround GNU problem (Greg Chicares) Chris@16: std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed, Chris@16: std::istream::floatfield ); Chris@16: std::streamsize old_prec = m_os.precision( 2 ); Chris@16: m_os << elapsed() << " s\n" // "s" is System International d'Unites std Chris@16: << std::endl; Chris@16: m_os.flags( old_flags ); Chris@16: m_os.precision( old_prec ); Chris@16: } Chris@16: Chris@16: catch (...) {} // eat any exceptions Chris@16: } // ~progress_timer Chris@16: Chris@16: private: Chris@16: std::ostream & m_os; Chris@16: }; Chris@16: Chris@16: Chris@16: // progress_display --------------------------------------------------------// Chris@16: Chris@16: // progress_display displays an appropriate indication of Chris@16: // progress at an appropriate place in an appropriate form. Chris@16: Chris@16: // NOTE: (Jan 12, 2001) Tried to change unsigned long to boost::uintmax_t, but Chris@16: // found some compilers couldn't handle the required conversion to double. Chris@16: // Reverted to unsigned long until the compilers catch up. Chris@16: Chris@16: class progress_display : private noncopyable Chris@16: { Chris@16: public: Chris@101: explicit progress_display( unsigned long expected_count_, Chris@16: std::ostream & os = std::cout, Chris@16: const std::string & s1 = "\n", //leading strings Chris@16: const std::string & s2 = "", Chris@16: const std::string & s3 = "" ) Chris@16: // os is hint; implementation may ignore, particularly in embedded systems Chris@101: : noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); } Chris@16: Chris@101: void restart( unsigned long expected_count_ ) Chris@16: // Effects: display appropriate scale Chris@101: // Postconditions: count()==0, expected_count()==expected_count_ Chris@16: { Chris@16: _count = _next_tic_count = _tic = 0; Chris@101: _expected_count = expected_count_; Chris@16: Chris@16: m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n" Chris@16: << m_s2 << "|----|----|----|----|----|----|----|----|----|----|" Chris@16: << std::endl // endl implies flush, which ensures display Chris@16: << m_s3; Chris@16: if ( !_expected_count ) _expected_count = 1; // prevent divide by zero Chris@16: } // restart Chris@16: Chris@16: unsigned long operator+=( unsigned long increment ) Chris@16: // Effects: Display appropriate progress tic if needed. Chris@16: // Postconditions: count()== original count() + increment Chris@16: // Returns: count(). Chris@16: { Chris@16: if ( (_count += increment) >= _next_tic_count ) { display_tic(); } Chris@16: return _count; Chris@16: } Chris@16: Chris@16: unsigned long operator++() { return operator+=( 1 ); } Chris@16: unsigned long count() const { return _count; } Chris@16: unsigned long expected_count() const { return _expected_count; } Chris@16: Chris@16: private: Chris@16: std::ostream & m_os; // may not be present in all imps Chris@16: const std::string m_s1; // string is more general, safer than Chris@16: const std::string m_s2; // const char *, and efficiency or size are Chris@16: const std::string m_s3; // not issues Chris@16: Chris@16: unsigned long _count, _expected_count, _next_tic_count; Chris@16: unsigned int _tic; Chris@16: void display_tic() Chris@16: { Chris@16: // use of floating point ensures that both large and small counts Chris@16: // work correctly. static_cast<>() is also used several places Chris@16: // to suppress spurious compiler warnings. Chris@16: unsigned int tics_needed = Chris@16: static_cast( Chris@16: (static_cast(_count)/_expected_count)*50.0 ); Chris@16: do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed ); Chris@16: _next_tic_count = Chris@16: static_cast((_tic/50.0)*_expected_count); Chris@16: if ( _count == _expected_count ) { Chris@16: if ( _tic < 51 ) m_os << '*'; Chris@16: m_os << std::endl; Chris@16: } Chris@16: } // display_tic Chris@16: }; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_PROGRESS_HPP