Chris@16
|
1 // boost progress.hpp header file ------------------------------------------//
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Beman Dawes 1994-99. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // See http://www.boost.org/libs/timer for documentation.
|
Chris@16
|
8
|
Chris@16
|
9 // Revision History
|
Chris@16
|
10 // 1 Dec 01 Add leading progress display strings (suggested by Toon Knapen)
|
Chris@16
|
11 // 20 May 01 Introduce several static_casts<> to eliminate warning messages
|
Chris@16
|
12 // (Fixed by Beman, reported by Herve Bronnimann)
|
Chris@16
|
13 // 12 Jan 01 Change to inline implementation to allow use without library
|
Chris@16
|
14 // builds. See docs for more rationale. (Beman Dawes)
|
Chris@16
|
15 // 22 Jul 99 Name changed to .hpp
|
Chris@16
|
16 // 16 Jul 99 Second beta
|
Chris@16
|
17 // 6 Jul 99 Initial boost version
|
Chris@16
|
18
|
Chris@16
|
19 #ifndef BOOST_PROGRESS_HPP
|
Chris@16
|
20 #define BOOST_PROGRESS_HPP
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/timer.hpp>
|
Chris@101
|
23 #include <boost/noncopyable.hpp>
|
Chris@16
|
24 #include <boost/cstdint.hpp> // for uintmax_t
|
Chris@16
|
25 #include <iostream> // for ostream, cout, etc
|
Chris@16
|
26 #include <string> // for string
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost {
|
Chris@16
|
29
|
Chris@16
|
30 // progress_timer ----------------------------------------------------------//
|
Chris@16
|
31
|
Chris@16
|
32 // A progress_timer behaves like a timer except that the destructor displays
|
Chris@16
|
33 // an elapsed time message at an appropriate place in an appropriate form.
|
Chris@16
|
34
|
Chris@16
|
35 class progress_timer : public timer, private noncopyable
|
Chris@16
|
36 {
|
Chris@16
|
37
|
Chris@16
|
38 public:
|
Chris@16
|
39 explicit progress_timer( std::ostream & os = std::cout )
|
Chris@16
|
40 // os is hint; implementation may ignore, particularly in embedded systems
|
Chris@16
|
41 : timer(), noncopyable(), m_os(os) {}
|
Chris@16
|
42 ~progress_timer()
|
Chris@16
|
43 {
|
Chris@16
|
44 // A) Throwing an exception from a destructor is a Bad Thing.
|
Chris@16
|
45 // B) The progress_timer destructor does output which may throw.
|
Chris@16
|
46 // C) A progress_timer is usually not critical to the application.
|
Chris@16
|
47 // Therefore, wrap the I/O in a try block, catch and ignore all exceptions.
|
Chris@16
|
48 try
|
Chris@16
|
49 {
|
Chris@16
|
50 // use istream instead of ios_base to workaround GNU problem (Greg Chicares)
|
Chris@16
|
51 std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
|
Chris@16
|
52 std::istream::floatfield );
|
Chris@16
|
53 std::streamsize old_prec = m_os.precision( 2 );
|
Chris@16
|
54 m_os << elapsed() << " s\n" // "s" is System International d'Unites std
|
Chris@16
|
55 << std::endl;
|
Chris@16
|
56 m_os.flags( old_flags );
|
Chris@16
|
57 m_os.precision( old_prec );
|
Chris@16
|
58 }
|
Chris@16
|
59
|
Chris@16
|
60 catch (...) {} // eat any exceptions
|
Chris@16
|
61 } // ~progress_timer
|
Chris@16
|
62
|
Chris@16
|
63 private:
|
Chris@16
|
64 std::ostream & m_os;
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67
|
Chris@16
|
68 // progress_display --------------------------------------------------------//
|
Chris@16
|
69
|
Chris@16
|
70 // progress_display displays an appropriate indication of
|
Chris@16
|
71 // progress at an appropriate place in an appropriate form.
|
Chris@16
|
72
|
Chris@16
|
73 // NOTE: (Jan 12, 2001) Tried to change unsigned long to boost::uintmax_t, but
|
Chris@16
|
74 // found some compilers couldn't handle the required conversion to double.
|
Chris@16
|
75 // Reverted to unsigned long until the compilers catch up.
|
Chris@16
|
76
|
Chris@16
|
77 class progress_display : private noncopyable
|
Chris@16
|
78 {
|
Chris@16
|
79 public:
|
Chris@101
|
80 explicit progress_display( unsigned long expected_count_,
|
Chris@16
|
81 std::ostream & os = std::cout,
|
Chris@16
|
82 const std::string & s1 = "\n", //leading strings
|
Chris@16
|
83 const std::string & s2 = "",
|
Chris@16
|
84 const std::string & s3 = "" )
|
Chris@16
|
85 // os is hint; implementation may ignore, particularly in embedded systems
|
Chris@101
|
86 : noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }
|
Chris@16
|
87
|
Chris@101
|
88 void restart( unsigned long expected_count_ )
|
Chris@16
|
89 // Effects: display appropriate scale
|
Chris@101
|
90 // Postconditions: count()==0, expected_count()==expected_count_
|
Chris@16
|
91 {
|
Chris@16
|
92 _count = _next_tic_count = _tic = 0;
|
Chris@101
|
93 _expected_count = expected_count_;
|
Chris@16
|
94
|
Chris@16
|
95 m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
|
Chris@16
|
96 << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
|
Chris@16
|
97 << std::endl // endl implies flush, which ensures display
|
Chris@16
|
98 << m_s3;
|
Chris@16
|
99 if ( !_expected_count ) _expected_count = 1; // prevent divide by zero
|
Chris@16
|
100 } // restart
|
Chris@16
|
101
|
Chris@16
|
102 unsigned long operator+=( unsigned long increment )
|
Chris@16
|
103 // Effects: Display appropriate progress tic if needed.
|
Chris@16
|
104 // Postconditions: count()== original count() + increment
|
Chris@16
|
105 // Returns: count().
|
Chris@16
|
106 {
|
Chris@16
|
107 if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
|
Chris@16
|
108 return _count;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 unsigned long operator++() { return operator+=( 1 ); }
|
Chris@16
|
112 unsigned long count() const { return _count; }
|
Chris@16
|
113 unsigned long expected_count() const { return _expected_count; }
|
Chris@16
|
114
|
Chris@16
|
115 private:
|
Chris@16
|
116 std::ostream & m_os; // may not be present in all imps
|
Chris@16
|
117 const std::string m_s1; // string is more general, safer than
|
Chris@16
|
118 const std::string m_s2; // const char *, and efficiency or size are
|
Chris@16
|
119 const std::string m_s3; // not issues
|
Chris@16
|
120
|
Chris@16
|
121 unsigned long _count, _expected_count, _next_tic_count;
|
Chris@16
|
122 unsigned int _tic;
|
Chris@16
|
123 void display_tic()
|
Chris@16
|
124 {
|
Chris@16
|
125 // use of floating point ensures that both large and small counts
|
Chris@16
|
126 // work correctly. static_cast<>() is also used several places
|
Chris@16
|
127 // to suppress spurious compiler warnings.
|
Chris@16
|
128 unsigned int tics_needed =
|
Chris@16
|
129 static_cast<unsigned int>(
|
Chris@16
|
130 (static_cast<double>(_count)/_expected_count)*50.0 );
|
Chris@16
|
131 do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
|
Chris@16
|
132 _next_tic_count =
|
Chris@16
|
133 static_cast<unsigned long>((_tic/50.0)*_expected_count);
|
Chris@16
|
134 if ( _count == _expected_count ) {
|
Chris@16
|
135 if ( _tic < 51 ) m_os << '*';
|
Chris@16
|
136 m_os << std::endl;
|
Chris@16
|
137 }
|
Chris@16
|
138 } // display_tic
|
Chris@16
|
139 };
|
Chris@16
|
140
|
Chris@16
|
141 } // namespace boost
|
Chris@16
|
142
|
Chris@16
|
143 #endif // BOOST_PROGRESS_HPP
|