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