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