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 #include <boost/config/header_deprecated.hpp> 23 BOOST_HEADER_DEPRECATED( "the facilities in <boost/timer/timer.hpp> or <boost/timer/progress_display.hpp>" ) 24 25 #include <boost/timer.hpp> 26 #include <boost/noncopyable.hpp> 27 #include <boost/cstdint.hpp> // for uintmax_t 28 #include <iostream> // for ostream, cout, etc 29 #include <string> // for string 30 31 namespace boost { 32 33 // progress_timer ----------------------------------------------------------// 34 35 // A progress_timer behaves like a timer except that the destructor displays 36 // an elapsed time message at an appropriate place in an appropriate form. 37 38 class progress_timer : public timer, private noncopyable 39 { 40 41 public: progress_timer(std::ostream & os=std::cout)42 explicit progress_timer( std::ostream & os = std::cout ) 43 // os is hint; implementation may ignore, particularly in embedded systems 44 : timer(), noncopyable(), m_os(os) {} ~progress_timer()45 ~progress_timer() 46 { 47 // A) Throwing an exception from a destructor is a Bad Thing. 48 // B) The progress_timer destructor does output which may throw. 49 // C) A progress_timer is usually not critical to the application. 50 // Therefore, wrap the I/O in a try block, catch and ignore all exceptions. 51 try 52 { 53 // use istream instead of ios_base to workaround GNU problem (Greg Chicares) 54 std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed, 55 std::istream::floatfield ); 56 std::streamsize old_prec = m_os.precision( 2 ); 57 m_os << elapsed() << " s\n" // "s" is System International d'Unites std 58 << std::endl; 59 m_os.flags( old_flags ); 60 m_os.precision( old_prec ); 61 } 62 63 catch (...) {} // eat any exceptions 64 } // ~progress_timer 65 66 private: 67 std::ostream & m_os; 68 }; 69 70 71 // progress_display --------------------------------------------------------// 72 73 // progress_display displays an appropriate indication of 74 // progress at an appropriate place in an appropriate form. 75 76 // NOTE: (Jan 12, 2001) Tried to change unsigned long to boost::uintmax_t, but 77 // found some compilers couldn't handle the required conversion to double. 78 // Reverted to unsigned long until the compilers catch up. 79 80 class progress_display : private noncopyable 81 { 82 public: progress_display(unsigned long expected_count_,std::ostream & os=std::cout,const std::string & s1="\\n",const std::string & s2="",const std::string & s3="")83 explicit progress_display( unsigned long expected_count_, 84 std::ostream & os = std::cout, 85 const std::string & s1 = "\n", //leading strings 86 const std::string & s2 = "", 87 const std::string & s3 = "" ) 88 // os is hint; implementation may ignore, particularly in embedded systems 89 : noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); } 90 restart(unsigned long expected_count_)91 void restart( unsigned long expected_count_ ) 92 // Effects: display appropriate scale 93 // Postconditions: count()==0, expected_count()==expected_count_ 94 { 95 _count = _next_tic_count = _tic = 0; 96 _expected_count = expected_count_; 97 98 m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n" 99 << m_s2 << "|----|----|----|----|----|----|----|----|----|----|" 100 << std::endl // endl implies flush, which ensures display 101 << m_s3; 102 if ( !_expected_count ) _expected_count = 1; // prevent divide by zero 103 } // restart 104 operator +=(unsigned long increment)105 unsigned long operator+=( unsigned long increment ) 106 // Effects: Display appropriate progress tic if needed. 107 // Postconditions: count()== original count() + increment 108 // Returns: count(). 109 { 110 if ( (_count += increment) >= _next_tic_count ) { display_tic(); } 111 return _count; 112 } 113 operator ++()114 unsigned long operator++() { return operator+=( 1 ); } count() const115 unsigned long count() const { return _count; } expected_count() const116 unsigned long expected_count() const { return _expected_count; } 117 118 private: 119 std::ostream & m_os; // may not be present in all imps 120 const std::string m_s1; // string is more general, safer than 121 const std::string m_s2; // const char *, and efficiency or size are 122 const std::string m_s3; // not issues 123 124 unsigned long _count, _expected_count, _next_tic_count; 125 unsigned int _tic; display_tic()126 void display_tic() 127 { 128 // use of floating point ensures that both large and small counts 129 // work correctly. static_cast<>() is also used several places 130 // to suppress spurious compiler warnings. 131 unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count) 132 / static_cast<double>(_expected_count)) * 50.0); 133 do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed ); 134 _next_tic_count = 135 static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count)); 136 if ( _count == _expected_count ) { 137 if ( _tic < 51 ) m_os << '*'; 138 m_os << std::endl; 139 } 140 } // display_tic 141 }; 142 143 } // namespace boost 144 145 #endif // BOOST_PROGRESS_HPP 146