Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:01:19

0001 
0002 // Copyright Beman Dawes 1994-99.
0003 // Copyright Peter Dimov 2019.
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/timer for documentation.
0008 
0009 #ifndef BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED
0010 #define BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED
0011 
0012 #include <iostream>           // for ostream, cout, etc
0013 #include <string>             // for string
0014 
0015 namespace boost {
0016 namespace timer {
0017 
0018 //  progress_display  --------------------------------------------------------//
0019 
0020 //  progress_display displays an appropriate indication of
0021 //  progress at an appropriate place in an appropriate form.
0022 
0023 class progress_display
0024 {
0025  private:
0026 
0027   progress_display( progress_display const& );
0028   progress_display& operator=( progress_display const& );
0029 
0030  public:
0031   explicit progress_display( unsigned long expected_count_,
0032                              std::ostream & os = std::cout,
0033                              const std::string & s1 = "\n", //leading strings
0034                              const std::string & s2 = "",
0035                              const std::string & s3 = "" )
0036    // os is hint; implementation may ignore, particularly in embedded systems
0037    : m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }
0038 
0039   void           restart( unsigned long expected_count_ )
0040   //  Effects: display appropriate scale
0041   //  Postconditions: count()==0, expected_count()==expected_count_
0042   {
0043     _count = _next_tic_count = _tic = 0;
0044     _expected_count = expected_count_;
0045 
0046     m_os << m_s1 << "0%   10   20   30   40   50   60   70   80   90   100%\n"
0047          << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
0048          << std::endl  // endl implies flush, which ensures display
0049          << m_s3;
0050     if ( !_expected_count ) _expected_count = 1;  // prevent divide by zero
0051   } // restart
0052 
0053   unsigned long  operator+=( unsigned long increment )
0054   //  Effects: Display appropriate progress tic if needed.
0055   //  Postconditions: count()== original count() + increment
0056   //  Returns: count().
0057   {
0058     if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
0059     return _count;
0060   }
0061 
0062   unsigned long  operator++()           { return operator+=( 1 ); }
0063   unsigned long  count() const          { return _count; }
0064   unsigned long  expected_count() const { return _expected_count; }
0065 
0066   private:
0067   std::ostream &     m_os;  // may not be present in all imps
0068   const std::string  m_s1;  // string is more general, safer than
0069   const std::string  m_s2;  //  const char *, and efficiency or size are
0070   const std::string  m_s3;  //  not issues
0071 
0072   unsigned long _count, _expected_count, _next_tic_count;
0073   unsigned int  _tic;
0074   void display_tic()
0075   {
0076     // use of floating point ensures that both large and small counts
0077     // work correctly.  static_cast<>() is also used several places
0078     // to suppress spurious compiler warnings.
0079     unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count)
0080         / static_cast<double>(_expected_count)) * 50.0);
0081     do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
0082     _next_tic_count =
0083       static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count));
0084     if ( _count == _expected_count ) {
0085       if ( _tic < 51 ) m_os << '*';
0086       m_os << std::endl;
0087       }
0088   } // display_tic
0089 };
0090 
0091 } // namespace timer
0092 } // namespace boost
0093 
0094 #endif  // BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED