progress_display.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright Beman Dawes 1994-99.
  2. // Copyright Peter Dimov 2019.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // See http://www.boost.org/libs/timer for documentation.
  7. #ifndef BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED
  8. #define BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED
  9. #include <boost/noncopyable.hpp>
  10. #include <iostream> // for ostream, cout, etc
  11. #include <string> // for string
  12. namespace boost {
  13. namespace timer {
  14. // progress_display --------------------------------------------------------//
  15. // progress_display displays an appropriate indication of
  16. // progress at an appropriate place in an appropriate form.
  17. class progress_display : private noncopyable
  18. {
  19. public:
  20. explicit progress_display( unsigned long expected_count_,
  21. std::ostream & os = std::cout,
  22. const std::string & s1 = "\n", //leading strings
  23. const std::string & s2 = "",
  24. const std::string & s3 = "" )
  25. // os is hint; implementation may ignore, particularly in embedded systems
  26. : noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }
  27. void restart( unsigned long expected_count_ )
  28. // Effects: display appropriate scale
  29. // Postconditions: count()==0, expected_count()==expected_count_
  30. {
  31. _count = _next_tic_count = _tic = 0;
  32. _expected_count = expected_count_;
  33. m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
  34. << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
  35. << std::endl // endl implies flush, which ensures display
  36. << m_s3;
  37. if ( !_expected_count ) _expected_count = 1; // prevent divide by zero
  38. } // restart
  39. unsigned long operator+=( unsigned long increment )
  40. // Effects: Display appropriate progress tic if needed.
  41. // Postconditions: count()== original count() + increment
  42. // Returns: count().
  43. {
  44. if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
  45. return _count;
  46. }
  47. unsigned long operator++() { return operator+=( 1 ); }
  48. unsigned long count() const { return _count; }
  49. unsigned long expected_count() const { return _expected_count; }
  50. private:
  51. std::ostream & m_os; // may not be present in all imps
  52. const std::string m_s1; // string is more general, safer than
  53. const std::string m_s2; // const char *, and efficiency or size are
  54. const std::string m_s3; // not issues
  55. unsigned long _count, _expected_count, _next_tic_count;
  56. unsigned int _tic;
  57. void display_tic()
  58. {
  59. // use of floating point ensures that both large and small counts
  60. // work correctly. static_cast<>() is also used several places
  61. // to suppress spurious compiler warnings.
  62. unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count)
  63. / static_cast<double>(_expected_count)) * 50.0);
  64. do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
  65. _next_tic_count =
  66. static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count));
  67. if ( _count == _expected_count ) {
  68. if ( _tic < 51 ) m_os << '*';
  69. m_os << std::endl;
  70. }
  71. } // display_tic
  72. };
  73. } // namespace timer
  74. } // namespace boost
  75. #endif // BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED