progress.hpp 5.9 KB

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