Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / timer / progress_display.hpp
1
2 // Copyright Beman Dawes 1994-99.
3 // Copyright Peter Dimov 2019.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/timer for documentation.
8
9 #ifndef BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED
10 #define BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED
11
12 #include <boost/noncopyable.hpp>
13 #include <iostream>           // for ostream, cout, etc
14 #include <string>             // for string
15
16 namespace boost {
17 namespace timer {
18
19 //  progress_display  --------------------------------------------------------//
20
21 //  progress_display displays an appropriate indication of
22 //  progress at an appropriate place in an appropriate form.
23
24 class progress_display : private noncopyable
25 {
26  public:
27   explicit progress_display( unsigned long expected_count_,
28                              std::ostream & os = std::cout,
29                              const std::string & s1 = "\n", //leading strings
30                              const std::string & s2 = "",
31                              const std::string & s3 = "" )
32    // os is hint; implementation may ignore, particularly in embedded systems
33    : noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }
34
35   void           restart( unsigned long expected_count_ )
36   //  Effects: display appropriate scale
37   //  Postconditions: count()==0, expected_count()==expected_count_
38   {
39     _count = _next_tic_count = _tic = 0;
40     _expected_count = expected_count_;
41
42     m_os << m_s1 << "0%   10   20   30   40   50   60   70   80   90   100%\n"
43          << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
44          << std::endl  // endl implies flush, which ensures display
45          << m_s3;
46     if ( !_expected_count ) _expected_count = 1;  // prevent divide by zero
47   } // restart
48
49   unsigned long  operator+=( unsigned long increment )
50   //  Effects: Display appropriate progress tic if needed.
51   //  Postconditions: count()== original count() + increment
52   //  Returns: count().
53   {
54     if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
55     return _count;
56   }
57
58   unsigned long  operator++()           { return operator+=( 1 ); }
59   unsigned long  count() const          { return _count; }
60   unsigned long  expected_count() const { return _expected_count; }
61
62   private:
63   std::ostream &     m_os;  // may not be present in all imps
64   const std::string  m_s1;  // string is more general, safer than
65   const std::string  m_s2;  //  const char *, and efficiency or size are
66   const std::string  m_s3;  //  not issues
67
68   unsigned long _count, _expected_count, _next_tic_count;
69   unsigned int  _tic;
70   void display_tic()
71   {
72     // use of floating point ensures that both large and small counts
73     // work correctly.  static_cast<>() is also used several places
74     // to suppress spurious compiler warnings.
75     unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count)
76         / static_cast<double>(_expected_count)) * 50.0);
77     do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
78     _next_tic_count =
79       static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count));
80     if ( _count == _expected_count ) {
81       if ( _tic < 51 ) m_os << '*';
82       m_os << std::endl;
83       }
84   } // display_tic
85 };
86
87 } // namespace timer
88 } // namespace boost
89
90 #endif  // BOOST_TIMER_PROGRESS_DISPLAY_HPP_INCLUDED