openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
progress.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
27 #ifndef _MT_PROGRESS_H
28 #define _MT_PROGRESS_H
29 
30 #include <string>
31 
37 template<class T>
38 class progress
39 {
40  protected:
42  std::string _msg;
43 
44  public:
45  progress(T num_next, const char* msg) :
46  _num_next(num_next),
47  _msg(msg)
48  {}
49  virtual ~progress()
50  {}
51 
52  virtual void next() = 0;
53  virtual void increment(T inc) = 0;
54  virtual void finish() = 0;
55 };
56 
62 template<class T>
63 class progress_percent : public progress<T>
64 {
65  private:
66  T _counter;
67  T _threshold;
68  std::string _blank;
69 
75  void display(T percentage)
76  {
77  const std::string & msg = progress<T>::_msg;
78 
79  printf("\r%s%s%d %c", msg.c_str(), _blank.c_str(), int(percentage), '%');
80  fflush(stdout);
81  }
82 
86  void evaluate()
87  {
88  const T & num_next = progress<T>::_num_next;
89  T prog = T(float(_counter) / float(num_next) * 100.0f);
90 
91  if(prog > _threshold)
92  {
93  _threshold = prog;
94  this->display(prog);
95  }
96  }
97 
98  public:
99  progress_percent(T num_next, const char* msg, T start = 60) :
100  progress<T>(num_next, msg),
101  _counter(0),
102  _threshold(0)
103  {
104  int size = start - progress<T>::_msg.size();
105  if(size < 0) size = 0;
106 
107  _blank.assign(size, ' ');
108  this->display(0);
109  }
110 
112  void next()
113  {
114  _counter++;
115  this->evaluate();
116  }
117 
119  void increment(T inc)
120  {
121  _counter += inc;
122  this->evaluate();
123  }
124 
126  void finish()
127  {
128  this->display(100);
129  printf("\n");
130  }
131 };
132 
133 
139 template<class T>
140 class progress_bar : public progress<T>
141 {
142  private:
143  T _counter;
144  T _threshold;
145 
146  const T _bar_len;
147  const char _bar_char;
148 
149  std::string _bar;
150  std::string _blank_a;
151  std::string _blank_b;
152 
158  void display(T nbars)
159  {
160  const std::string & msg = progress<T>::_msg;
161  if(nbars > _bar_len) nbars = _bar_len;
162 
163  _bar.assign(nbars, _bar_char);
164  _blank_b.assign(_bar_len - nbars, ' ');
165 
166  printf("\r%s%s[%s%s]", msg.c_str(), _blank_a.c_str(), _bar.c_str(), _blank_b.c_str());
167  fflush(stdout);
168  }
169 
173  void evaluate()
174  {
175  const T & num_next = progress<T>::_num_next;
176  T prog = T(float(_counter) / float(num_next) * float(_bar_len));
177 
178  if(prog > _threshold)
179  {
180  _threshold = prog;
181  this->display(prog);
182  }
183  }
184 
185  public:
195  progress_bar(T num_next, const char* msg, T bar_start = 60, T bar_len = 20, char bar_char = '=') :
196  progress<T>(num_next, msg),
197  _counter(0),
198  _threshold(0),
199  _bar_len(bar_len),
200  _bar_char(bar_char)
201  {
202  int size = bar_start - progress<T>::_msg.size();
203  if(size < 0) size = 0;
204 
205  _blank_a.assign(size, ' ');
206  this->display(0);
207  }
208 
210  void next()
211  {
212  _counter++;
213  this->evaluate();
214  }
215 
217  void increment(T inc)
218  {
219  _counter += inc;
220  this->evaluate();
221  }
222 
224  void finish()
225  {
226  this->display(_bar_len);
227  printf("\n");
228  }
229 };
230 
231 #endif
232 
virtual ~progress()
Definition: progress.hpp:49
Display progress as a percentage.
Definition: progress.hpp:63
void finish()
finish up progress display
Definition: progress.hpp:126
void increment(T inc)
increment progress counter by a custom value
Definition: progress.hpp:119
Base class for tracking progress.
Definition: progress.hpp:38
virtual void next()=0
T _num_next
number of times next will be triggered
Definition: progress.hpp:41
Display progress as a bar.
Definition: progress.hpp:140
virtual void finish()=0
progress_percent(T num_next, const char *msg, T start=60)
Definition: progress.hpp:99
void next()
increment progress counter by one
Definition: progress.hpp:210
std::string _msg
message that will be displayed before progress information.
Definition: progress.hpp:42
void next()
increment progress counter by one
Definition: progress.hpp:112
virtual void increment(T inc)=0
progress_bar(T num_next, const char *msg, T bar_start=60, T bar_len=20, char bar_char='=')
Constructor.
Definition: progress.hpp:195
progress(T num_next, const char *msg)
Definition: progress.hpp:45
void finish()
finish up progress display
Definition: progress.hpp:224
void increment(T inc)
increment progress counter by a custom value
Definition: progress.hpp:217