progress_display.hpp 3.4 KB

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