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