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