openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
progress.hpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
12 #ifndef _MT_PROGRESS_H
13 #define _MT_PROGRESS_H
14 
15 #include <string>
16 
22 template<class T>
23 class progress
24 {
25  protected:
27  std::string _msg;
28 
29  public:
30  progress(T num_next, const char* msg) :
31  _num_next(num_next),
32  _msg(msg)
33  {}
34  virtual ~progress()
35  {}
36 
37  virtual void next() = 0;
38  virtual void increment(T inc) = 0;
39  virtual void finish() = 0;
40 };
41 
47 template<class T>
48 class progress_percent : public progress<T>
49 {
50  private:
51  T _counter;
52  T _threshold;
53  std::string _blank;
54 
60  void display(T percentage)
61  {
62  const std::string & msg = progress<T>::_msg;
63 
64  printf("\r%s%s%d %c", msg.c_str(), _blank.c_str(), int(percentage), '%');
65  fflush(stdout);
66  }
67 
71  void evaluate()
72  {
73  const T & num_next = progress<T>::_num_next;
74  T prog = T(float(_counter) / float(num_next) * 100.0f);
75 
76  if(prog > _threshold)
77  {
78  _threshold = prog;
79  this->display(prog);
80  }
81  }
82 
83  public:
84  progress_percent(T num_next, const char* msg, T start = 60) :
85  progress<T>(num_next, msg),
86  _counter(0),
87  _threshold(0)
88  {
89  int size = start - progress<T>::_msg.size();
90  if(size < 0) size = 0;
91 
92  _blank.assign(size, ' ');
93  this->display(0);
94  }
95 
97  void next()
98  {
99  _counter++;
100  this->evaluate();
101  }
102 
104  void increment(T inc)
105  {
106  _counter += inc;
107  this->evaluate();
108  }
109 
111  void finish()
112  {
113  this->display(100);
114  printf("\n");
115  }
116 };
117 
118 
124 template<class T>
125 class progress_bar : public progress<T>
126 {
127  private:
128  T _counter;
129  T _threshold;
130 
131  const T _bar_len;
132  const char _bar_char;
133 
134  std::string _bar;
135  std::string _blank_a;
136  std::string _blank_b;
137 
143  void display(T nbars)
144  {
145  const std::string & msg = progress<T>::_msg;
146  if(nbars > _bar_len) nbars = _bar_len;
147 
148  _bar.assign(nbars, _bar_char);
149  _blank_b.assign(_bar_len - nbars, ' ');
150 
151  printf("\r%s%s[%s%s]", msg.c_str(), _blank_a.c_str(), _bar.c_str(), _blank_b.c_str());
152  fflush(stdout);
153  }
154 
158  void evaluate()
159  {
160  const T & num_next = progress<T>::_num_next;
161  T prog = T(float(_counter) / float(num_next) * float(_bar_len));
162 
163  if(prog > _threshold)
164  {
165  _threshold = prog;
166  this->display(prog);
167  }
168  }
169 
170  public:
180  progress_bar(T num_next, const char* msg, T bar_start = 60, T bar_len = 20, char bar_char = '=') :
181  progress<T>(num_next, msg),
182  _counter(0),
183  _threshold(0),
184  _bar_len(bar_len),
185  _bar_char(bar_char)
186  {
187  int size = bar_start - progress<T>::_msg.size();
188  if(size < 0) size = 0;
189 
190  _blank_a.assign(size, ' ');
191  this->display(0);
192  }
193 
195  void next()
196  {
197  _counter++;
198  this->evaluate();
199  }
200 
202  void increment(T inc)
203  {
204  _counter += inc;
205  this->evaluate();
206  }
207 
209  void finish()
210  {
211  this->display(_bar_len);
212  printf("\n");
213  }
214 };
215 
216 #endif
217 
Display progress as a bar.
Definition: progress.hpp:126
void next()
increment progress counter by one
Definition: progress.hpp:195
progress_bar(T num_next, const char *msg, T bar_start=60, T bar_len=20, char bar_char='=')
Constructor.
Definition: progress.hpp:180
void finish()
finish up progress display
Definition: progress.hpp:209
void increment(T inc)
increment progress counter by a custom value
Definition: progress.hpp:202
Display progress as a percentage.
Definition: progress.hpp:49
progress_percent(T num_next, const char *msg, T start=60)
Definition: progress.hpp:84
void increment(T inc)
increment progress counter by a custom value
Definition: progress.hpp:104
void next()
increment progress counter by one
Definition: progress.hpp:97
void finish()
finish up progress display
Definition: progress.hpp:111
Base class for tracking progress.
Definition: progress.hpp:24
virtual void increment(T inc)=0
virtual ~progress()
Definition: progress.hpp:34
T _num_next
number of times next will be triggered
Definition: progress.hpp:26
virtual void finish()=0
progress(T num_next, const char *msg)
Definition: progress.hpp:30
virtual void next()=0
std::string _msg
message that will be displayed before progress information.
Definition: progress.hpp:27