progress.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // boost progress.hpp header file ------------------------------------------//
  2. // Copyright Beman Dawes 1994-99. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/timer for documentation.
  6. // Revision History
  7. // 1 Dec 01 Add leading progress display strings (suggested by Toon Knapen)
  8. // 20 May 01 Introduce several static_casts<> to eliminate warning messages
  9. // (Fixed by Beman, reported by Herve Bronnimann)
  10. // 12 Jan 01 Change to inline implementation to allow use without library
  11. // builds. See docs for more rationale. (Beman Dawes)
  12. // 22 Jul 99 Name changed to .hpp
  13. // 16 Jul 99 Second beta
  14. // 6 Jul 99 Initial boost version
  15. #ifndef BOOST_PROGRESS_HPP
  16. #define BOOST_PROGRESS_HPP
  17. #include <boost/timer.hpp>
  18. #include <boost/utility.hpp> // for noncopyable
  19. #include <boost/cstdint.hpp> // for uintmax_t
  20. #include <iostream> // for ostream, cout, etc
  21. #include <string> // for string
  22. namespace boost {
  23. // progress_timer ----------------------------------------------------------//
  24. // A progress_timer behaves like a timer except that the destructor displays
  25. // an elapsed time message at an appropriate place in an appropriate form.
  26. class progress_timer : public timer, private noncopyable
  27. {
  28. public:
  29. explicit progress_timer( std::ostream & os = std::cout )
  30. // os is hint; implementation may ignore, particularly in embedded systems
  31. : m_os(os) {}
  32. ~progress_timer()
  33. {
  34. // A) Throwing an exception from a destructor is a Bad Thing.
  35. // B) The progress_timer destructor does output which may throw.
  36. // C) A progress_timer is usually not critical to the application.
  37. // Therefore, wrap the I/O in a try block, catch and ignore all exceptions.
  38. try
  39. {
  40. // use istream instead of ios_base to workaround GNU problem (Greg Chicares)
  41. std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
  42. std::istream::floatfield );
  43. std::streamsize old_prec = m_os.precision( 2 );
  44. m_os << elapsed() << " s\n" // "s" is System International d'Unites std
  45. << std::endl;
  46. m_os.flags( old_flags );
  47. m_os.precision( old_prec );
  48. }
  49. catch (...) {} // eat any exceptions
  50. } // ~progress_timer
  51. private:
  52. std::ostream & m_os;
  53. };
  54. // progress_display --------------------------------------------------------//
  55. // progress_display displays an appropriate indication of
  56. // progress at an appropriate place in an appropriate form.
  57. // NOTE: (Jan 12, 2001) Tried to change unsigned long to boost::uintmax_t, but
  58. // found some compilers couldn't handle the required conversion to double.
  59. // Reverted to unsigned long until the compilers catch up.
  60. class progress_display : private noncopyable
  61. {
  62. public:
  63. explicit progress_display( unsigned long expected_count,
  64. std::ostream & os = std::cout,
  65. const std::string & s1 = "\n", //leading strings
  66. const std::string & s2 = "",
  67. const std::string & s3 = "" )
  68. // os is hint; implementation may ignore, particularly in embedded systems
  69. : m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count); }
  70. void restart( unsigned long expected_count )
  71. // Effects: display appropriate scale
  72. // Postconditions: count()==0, expected_count()==expected_count
  73. {
  74. _count = _next_tic_count = _tic = 0;
  75. _expected_count = expected_count;
  76. m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
  77. << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
  78. << std::endl // endl implies flush, which ensures display
  79. << m_s3;
  80. if ( !_expected_count ) _expected_count = 1; // prevent divide by zero
  81. } // restart
  82. unsigned long operator+=( unsigned long increment )
  83. // Effects: Display appropriate progress tic if needed.
  84. // Postconditions: count()== original count() + increment
  85. // Returns: count().
  86. {
  87. if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
  88. return _count;
  89. }
  90. unsigned long operator++() { return operator+=( 1 ); }
  91. unsigned long count() const { return _count; }
  92. unsigned long expected_count() const { return _expected_count; }
  93. private:
  94. std::ostream & m_os; // may not be present in all imps
  95. const std::string m_s1; // string is more general, safer than
  96. const std::string m_s2; // const char *, and efficiency or size are
  97. const std::string m_s3; // not issues
  98. unsigned long _count, _expected_count, _next_tic_count;
  99. unsigned int _tic;
  100. void display_tic()
  101. {
  102. // use of floating point ensures that both large and small counts
  103. // work correctly. static_cast<>() is also used several places
  104. // to suppress spurious compiler warnings.
  105. unsigned int tics_needed =
  106. static_cast<unsigned int>(
  107. (static_cast<double>(_count)/_expected_count)*50.0 );
  108. do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
  109. _next_tic_count =
  110. static_cast<unsigned long>((_tic/50.0)*_expected_count);
  111. if ( _count == _expected_count ) {
  112. if ( _tic < 51 ) m_os << '*';
  113. m_os << std::endl;
  114. }
  115. } // display_tic
  116. };
  117. } // namespace boost
  118. #endif // BOOST_PROGRESS_HPP