File indexing completed on 2025-01-30 10:01:19
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED
0010 #define BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED
0011
0012 #include <iostream> // for ostream, cout, etc
0013 #include <string> // for string
0014
0015 namespace boost {
0016 namespace timer {
0017
0018
0019
0020
0021
0022
0023 class progress_display
0024 {
0025 private:
0026
0027 progress_display( progress_display const& );
0028 progress_display& operator=( progress_display const& );
0029
0030 public:
0031 explicit progress_display( unsigned long expected_count_,
0032 std::ostream & os = std::cout,
0033 const std::string & s1 = "\n",
0034 const std::string & s2 = "",
0035 const std::string & s3 = "" )
0036
0037 : m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }
0038
0039 void restart( unsigned long expected_count_ )
0040
0041
0042 {
0043 _count = _next_tic_count = _tic = 0;
0044 _expected_count = expected_count_;
0045
0046 m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
0047 << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
0048 << std::endl
0049 << m_s3;
0050 if ( !_expected_count ) _expected_count = 1;
0051 }
0052
0053 unsigned long operator+=( unsigned long increment )
0054
0055
0056
0057 {
0058 if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
0059 return _count;
0060 }
0061
0062 unsigned long operator++() { return operator+=( 1 ); }
0063 unsigned long count() const { return _count; }
0064 unsigned long expected_count() const { return _expected_count; }
0065
0066 private:
0067 std::ostream & m_os;
0068 const std::string m_s1;
0069 const std::string m_s2;
0070 const std::string m_s3;
0071
0072 unsigned long _count, _expected_count, _next_tic_count;
0073 unsigned int _tic;
0074 void display_tic()
0075 {
0076
0077
0078
0079 unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count)
0080 / static_cast<double>(_expected_count)) * 50.0);
0081 do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
0082 _next_tic_count =
0083 static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count));
0084 if ( _count == _expected_count ) {
0085 if ( _tic < 51 ) m_os << '*';
0086 m_os << std::endl;
0087 }
0088 }
0089 };
0090
0091 }
0092 }
0093
0094 #endif