123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #ifndef BOOST_PROGRESS_HPP
- #define BOOST_PROGRESS_HPP
- #include <boost/timer.hpp>
- #include <boost/noncopyable.hpp>
- #include <boost/cstdint.hpp> // for uintmax_t
- #include <iostream> // for ostream, cout, etc
- #include <string> // for string
- namespace boost {
- class progress_timer : public timer, private noncopyable
- {
-
- public:
- explicit progress_timer( std::ostream & os = std::cout )
-
- : timer(), noncopyable(), m_os(os) {}
- ~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\n"
- << std::endl;
- m_os.flags( old_flags );
- m_os.precision( old_prec );
- }
- catch (...) {}
- }
- private:
- std::ostream & m_os;
- };
- class progress_display : private noncopyable
- {
- public:
- explicit progress_display( unsigned long expected_count_,
- std::ostream & os = std::cout,
- const std::string & s1 = "\n",
- const std::string & s2 = "",
- const std::string & s3 = "" )
-
- : noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }
- void restart( unsigned long expected_count_ )
-
-
- {
- _count = _next_tic_count = _tic = 0;
- _expected_count = expected_count_;
- m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
- << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
- << std::endl
- << m_s3;
- if ( !_expected_count ) _expected_count = 1;
- }
- unsigned long operator+=( unsigned long increment )
-
-
-
- {
- if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
- return _count;
- }
- unsigned long operator++() { return operator+=( 1 ); }
- unsigned long count() const { return _count; }
- unsigned long expected_count() const { return _expected_count; }
- private:
- std::ostream & m_os;
- const std::string m_s1;
- const std::string m_s2;
- const std::string m_s3;
- unsigned long _count, _expected_count, _next_tic_count;
- unsigned int _tic;
- void display_tic()
- {
-
-
-
- unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count)
- / static_cast<double>(_expected_count)) * 50.0);
- do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
- _next_tic_count =
- static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count));
- if ( _count == _expected_count ) {
- if ( _tic < 51 ) m_os << '*';
- m_os << std::endl;
- }
- }
- };
- }
- #endif
|