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