Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/progress.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //  boost progress.hpp header file  ------------------------------------------//
0002 
0003 //  Copyright Beman Dawes 1994-99.  Distributed under the Boost
0004 //  Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 //  See http://www.boost.org/libs/timer for documentation.
0008 
0009 //  Revision History
0010 //   1 Dec 01  Add leading progress display strings (suggested by Toon Knapen)
0011 //  20 May 01  Introduce several static_casts<> to eliminate warning messages
0012 //             (Fixed by Beman, reported by Herve Bronnimann)
0013 //  12 Jan 01  Change to inline implementation to allow use without library
0014 //             builds. See docs for more rationale. (Beman Dawes) 
0015 //  22 Jul 99  Name changed to .hpp
0016 //  16 Jul 99  Second beta
0017 //   6 Jul 99  Initial boost version
0018 
0019 #ifndef BOOST_PROGRESS_HPP
0020 #define BOOST_PROGRESS_HPP
0021 
0022 #if !defined(BOOST_TIMER_ENABLE_DEPRECATED)
0023 # error This header is deprecated and will be removed. (You can define BOOST_TIMER_ENABLE_DEPRECATED to suppress this error.)
0024 #endif
0025 
0026 #include <boost/config/header_deprecated.hpp>
0027 BOOST_HEADER_DEPRECATED( "the facilities in <boost/timer/timer.hpp> or <boost/timer/progress_display.hpp>" )
0028 
0029 #include <boost/timer.hpp>
0030 #include <boost/cstdint.hpp>  // for uintmax_t
0031 #include <iostream>           // for ostream, cout, etc
0032 #include <string>             // for string
0033 
0034 namespace boost {
0035 
0036 //  progress_timer  ----------------------------------------------------------//
0037 
0038 //  A progress_timer behaves like a timer except that the destructor displays
0039 //  an elapsed time message at an appropriate place in an appropriate form.
0040 
0041 class progress_timer : public timer
0042 {
0043  private:
0044 
0045   progress_timer( progress_timer const& );
0046   progress_timer& operator=( progress_timer const& );
0047 
0048  public:
0049   explicit progress_timer( std::ostream & os = std::cout )
0050      // os is hint; implementation may ignore, particularly in embedded systems
0051      : timer(), m_os(os) {}
0052   ~progress_timer()
0053   {
0054   //  A) Throwing an exception from a destructor is a Bad Thing.
0055   //  B) The progress_timer destructor does output which may throw.
0056   //  C) A progress_timer is usually not critical to the application.
0057   //  Therefore, wrap the I/O in a try block, catch and ignore all exceptions.
0058     try
0059     {
0060       // use istream instead of ios_base to workaround GNU problem (Greg Chicares)
0061       std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
0062                                                    std::istream::floatfield );
0063       std::streamsize old_prec = m_os.precision( 2 );
0064       m_os << elapsed() << " s\n" // "s" is System International d'Unites std
0065                         << std::endl;
0066       m_os.flags( old_flags );
0067       m_os.precision( old_prec );
0068     }
0069 
0070     catch (...) {} // eat any exceptions
0071   } // ~progress_timer
0072 
0073  private:
0074   std::ostream & m_os;
0075 };
0076 
0077 
0078 //  progress_display  --------------------------------------------------------//
0079 
0080 //  progress_display displays an appropriate indication of 
0081 //  progress at an appropriate place in an appropriate form.
0082 
0083 // NOTE: (Jan 12, 2001) Tried to change unsigned long to boost::uintmax_t, but
0084 // found some compilers couldn't handle the required conversion to double.
0085 // Reverted to unsigned long until the compilers catch up. 
0086 
0087 class progress_display
0088 {
0089  private:
0090 
0091   progress_display( progress_display const& );
0092   progress_display& operator=( progress_display const& );
0093 
0094  public:
0095   explicit progress_display( unsigned long expected_count_,
0096                              std::ostream & os = std::cout,
0097                              const std::string & s1 = "\n", //leading strings
0098                              const std::string & s2 = "",
0099                              const std::string & s3 = "" )
0100    // os is hint; implementation may ignore, particularly in embedded systems
0101    : m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }
0102 
0103   void           restart( unsigned long expected_count_ )
0104   //  Effects: display appropriate scale
0105   //  Postconditions: count()==0, expected_count()==expected_count_
0106   {
0107     _count = _next_tic_count = _tic = 0;
0108     _expected_count = expected_count_;
0109 
0110     m_os << m_s1 << "0%   10   20   30   40   50   60   70   80   90   100%\n"
0111          << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
0112          << std::endl  // endl implies flush, which ensures display
0113          << m_s3;
0114     if ( !_expected_count ) _expected_count = 1;  // prevent divide by zero
0115   } // restart
0116 
0117   unsigned long  operator+=( unsigned long increment )
0118   //  Effects: Display appropriate progress tic if needed.
0119   //  Postconditions: count()== original count() + increment
0120   //  Returns: count().
0121   {
0122     if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
0123     return _count;
0124   }
0125 
0126   unsigned long  operator++()           { return operator+=( 1 ); }
0127   unsigned long  count() const          { return _count; }
0128   unsigned long  expected_count() const { return _expected_count; }
0129 
0130   private:
0131   std::ostream &     m_os;  // may not be present in all imps
0132   const std::string  m_s1;  // string is more general, safer than 
0133   const std::string  m_s2;  //  const char *, and efficiency or size are
0134   const std::string  m_s3;  //  not issues
0135 
0136   unsigned long _count, _expected_count, _next_tic_count;
0137   unsigned int  _tic;
0138   void display_tic()
0139   {
0140     // use of floating point ensures that both large and small counts
0141     // work correctly.  static_cast<>() is also used several places
0142     // to suppress spurious compiler warnings. 
0143     unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count)
0144         / static_cast<double>(_expected_count)) * 50.0);
0145     do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
0146     _next_tic_count = 
0147       static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count));
0148     if ( _count == _expected_count ) {
0149       if ( _tic < 51 ) m_os << '*';
0150       m_os << std::endl;
0151       }
0152   } // display_tic
0153 };
0154 
0155 } // namespace boost
0156 
0157 #endif  // BOOST_PROGRESS_HPP