aimstil  5.0.5
progress_indicator.h
Go to the documentation of this file.
1 #ifndef TIL_PROGRESS_INDICATOR_H_
2 #define TIL_PROGRESS_INDICATOR_H_
3 
4 // includes from STL
5 #include <ctime>
6 #include <iostream>
7 
8 // includes from Linux
9 //#include <sys/time.h> // gettimeofday
10 
11 namespace til
12 {
13 
14 
15  namespace callback
16  {
17 
18  //----------------------------------------------------------------------------------------------
19 
20 
22  struct PI_Print
23  {
24  void operator()(std::size_t current, std::size_t total)
25  {
26  std::cout << 100 * current / total << "%..." << std::flush;
27  }
28  };
29 
30 
31  //----------------------------------------------------------------------------------------------
32 
33  //------------//
34  // PI_Timer //
35  //------------//
36 
38  template < typename TPICallback >
39  class PI_Timer
40  {
41  public: // constructors
42 
44  : m_start()
45  , m_finish()
46  {
47  this->setDelay(2.0);
48  this->init();
49  }
50 
51  explicit PI_Timer(double delay)
52  : m_delay(delay)
53  , m_start()
54  , m_finish()
55  { this->init(); }
56 
57  public: // initialization
58 
59  void init()
60  {
61  m_active = false;
62  }
63 
64  public: // set & get
65 
67  void setDelay(double s) { m_delay = s * CLOCKS_PER_SEC; }
69  TPICallback & callback() { return m_callback; }
70 
71  public: // operators
72 
73  void operator()(std::size_t current, std::size_t total)
74  {
75  if (current == 0)
76  {
77  //gettimeofday(&start, &tz);
78  m_start = std::clock();
79  return;
80  }
81  else if (current == 1)
82  {
83  //gettimeofday(&finish, &tz);
84  //double t = (finish.tv_sec-start.tv_sec) * 1000000L + (finish.tv_usec-start.tv_usec);
85  m_finish = std::clock();
86  double t = double(m_finish - m_start);
87  if (t > m_delay)
88  {
89  m_active = true;
90  }
91  }
92 
93  if (m_active)
94  {
95  m_callback(current, total);
96  }
97  }
98 
99  private: // data, input
100 
101  double m_delay;
102  TPICallback m_callback;
103 
104  private: // data, internal;
105 
106  //struct timeval m_start, m_finish;
107  //struct timezone m_tz;
108  std::clock_t m_start, m_finish;
109  bool m_active;
110  };
111  } // namespace callback
112 
113 
114  //----------------------------------------------------------------------------------------------
115 
116 
117  //---------------------//
118  // ProgressIndicator //
119  //---------------------//
120 
124  template < typename TPICallback >
126  {
127  public: // constructors
128 
130  ProgressIndicator(std::size_t parts, std::size_t total);
131 
132  public: // initialization
133 
134  void init();
135 
136  public: //set & get
137 
138  std::size_t & total() { return m_total; }
139  std::size_t & parts() { return m_parts; }
140  TPICallback & callback() { return m_callback; }
141 
142  public: // operators
143 
145  void operator()(std::size_t i);
146 
147  private: // functions
148  void nextPart();
149 
150  private: // data, input
151  std::size_t m_total;
152  std::size_t m_parts;
153  TPICallback m_callback;
154 
155  private: // data, internal
156  double m_mark;
157  std::size_t m_currentpart;
158  };
159 
160 
162 
163 
164  //----------------------------------------------------------------------------------------------
165 
166  //---------------//
167  // NoIndicator //
168  //---------------//
169 
171  struct NoIndicator
172  {
173  void operator()(std::size_t) {}
174  };
175 
176 } // namespace til
177 
178 // package include
179 #include "progress_indicator.tpp"
180 
181 #endif /*PROGRESS_INDICATOR_H_*/
void operator()(std::size_t)
ProgressIndicator callback that prints percentage if computation is taking time.
void setDelay(double s)
Set delay, in seconds.
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
TPICallback & callback()
Access callback object.
ProgressIndicator< callback::PI_Timer< callback::PI_Print > > DelayedProgressIndicator
void operator()(std::size_t current, std::size_t total)
A simple base class used to give progression feedback.
Empty indicator class.
void operator()(std::size_t current, std::size_t total)
ProgressIndicator callback that simply prints percentage.