progress.hpp 5.6 KB

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