Progress.cpp
Go to the documentation of this file.
1 
28  /*
29  * ProgressBar.cpp
30  *
31  * Created on: 14.01.2011
32  * Author: Thomas Wiemann
33  */
34 
35 
36 #include "lvr2/io/Progress.hpp"
37 
38 #include <sstream>
39 #include <iostream>
40 
41 using std::stringstream;
42 using std::cout;
43 using std::endl;
44 using std::flush;
45 
46 namespace lvr2
47 {
48 
51 
52 ProgressBar::ProgressBar(size_t max_val, string prefix)
53 {
54  m_prefix = prefix;
55  m_maxVal = max_val;
56  m_currentVal = 0;
57  m_percent = 0;
58 
59  if(m_titleCallback)
60  {
61  // Remove time brackets
62  unsigned index;
63  index = prefix.find_last_of("]");
64  m_titleCallback(prefix.substr(index+1));
65  }
66 }
67 
69 {
70 
71 }
72 
74 {
75  m_progressCallback = ptr;
76 }
77 
79 {
80  m_titleCallback = ptr;
81 }
82 
84 {
85  boost::mutex::scoped_lock lock(m_mutex);
86 
87  m_currentVal++;
88  short difference = (short)((float)m_currentVal/m_maxVal * 100 - m_percent);
89  if (difference < 1)
90  {
91  return;
92  }
93 
94  while (difference >= 1)
95  {
96  m_percent++;
97  difference--;
98  print_bar();
99 
101  {
103  }
104  }
105 
106 }
107 
109 {
110  boost::mutex::scoped_lock lock(m_mutex);
111 
112  m_currentVal+= n;
113  short difference = (short)((float)m_currentVal/m_maxVal * 100 - m_percent);
114  if (difference < 1)
115  {
116  return;
117  }
118 
119  while (difference >= 1)
120  {
121  m_percent++;
122  difference--;
123  print_bar();
124 
126  {
128  }
129  }
130 
131 }
132 
134 {
135  cout << "\r" << m_prefix << " " << m_percent << "%" << flush;
136 }
137 
138 ProgressCounter::ProgressCounter(int stepVal, string prefix)
139 {
140  m_prefix = prefix;
141  m_stepVal = stepVal;
142  m_currentVal = 0;
143 }
144 
146 {
147  boost::mutex::scoped_lock lock(m_mutex);
148  m_currentVal++;
149  if(m_currentVal % m_stepVal == 0)
150  {
151  print_progress();
152  }
153 }
154 
156 {
157  cout << "\r" << m_prefix << " " << m_currentVal << flush;
158 }
159 
162 
163 PacmanProgressBar::PacmanProgressBar(size_t max_val, string prefix, size_t bar_length)
164 :
165  m_prefix(prefix)
166  ,m_bar_length(bar_length)
167 {
168  m_maxVal = max_val;
169  m_currentVal = 0;
170  m_percent = 0;
171 
172  if(m_titleCallback)
173  {
174  // Remove time brackets
175  unsigned index;
176  index = prefix.find_last_of("]");
177  m_titleCallback(prefix.substr(index+1));
178  }
179 }
180 
182 {
183 
184 }
185 
187 {
188  m_progressCallback = ptr;
189 }
190 
192 {
193  m_titleCallback = ptr;
194 }
195 
197 {
198  boost::mutex::scoped_lock lock(m_mutex);
199 
200  m_currentVal++;
201  short difference = (short)((float)m_currentVal/m_maxVal * 100 - m_percent);
202 
203  if (difference < 1)
204  {
205  return;
206  }
207 
208  while (difference >= 1)
209  {
210  m_percent++;
211  difference--;
212  print_bar();
213 
215  {
217  }
218  }
219 
220 }
221 
223 {
224  int char_idx = static_cast<float>(m_percent)/100.0 * (m_bar_length);
225 
226  cout << "\r" << m_prefix << " " << m_percent << "%" << " | ";
227 
228  for(size_t i=0; i< char_idx; i++)
229  {
230  cout << " ";
231  }
232 
233  if(char_idx % 2 == 0)
234  {
235  cout << "ᗧ";
236  }else{
237  cout << "O";
238  }
239 
240  for(size_t i=char_idx; i < m_bar_length; i++)
241  {
242  if(i%2 == 0)
243  {
244  cout << " ";
245  } else {
246  cout << "•";
247  }
248  }
249 
250  cout << " | " << flush;
251 }
252 
253 } // namespace lvr
254 
string m_prefix
The prefix string.
Definition: Progress.hpp:115
static PacmanProgressTitleCallbackPtr m_titleCallback
Definition: Progress.hpp:267
void(* PacmanProgressCallbackPtr)(int)
A class to manage progress information output for process where the number of performed operations is...
Definition: Progress.hpp:199
int m_percent
The current progress in percent.
Definition: Progress.hpp:127
void(* ProgressCallbackPtr)(int)
A class to manage progress information output for process where the number of performed operations is...
Definition: Progress.hpp:65
PacmanProgressBar(size_t max_val, string prefix="", size_t bar_length=60)
Ctor.
Definition: Progress.cpp:163
size_t m_maxVal
The number of iterations.
Definition: Progress.hpp:118
static PacmanProgressCallbackPtr m_progressCallback
Definition: Progress.hpp:266
size_t m_currentVal
The current counter.
Definition: Progress.hpp:121
ProgressBar(size_t max_val, string prefix="")
Ctor.
Definition: Progress.cpp:52
void print_bar()
Prints the output.
Definition: Progress.cpp:133
string m_prefix
The prefix string.
Definition: Progress.hpp:243
void(* ProgressTitleCallbackPtr)(string)
Definition: Progress.hpp:66
size_t m_maxVal
The number of iterations.
Definition: Progress.hpp:246
virtual ~ProgressBar()
Definition: Progress.cpp:68
void operator+=(size_t n)
Increases the counter of performed by n.
Definition: Progress.cpp:108
size_t m_currentVal
The current counter.
Definition: Progress.hpp:249
void(* PacmanProgressTitleCallbackPtr)(string)
Definition: Progress.hpp:200
static ProgressCallbackPtr m_progressCallback
Definition: Progress.hpp:135
static ProgressTitleCallbackPtr m_titleCallback
Definition: Progress.hpp:136
static void setProgressCallback(ProgressCallbackPtr)
Registers a callback that is called with the new value when the percentage of the progress changed...
Definition: Progress.cpp:73
int m_percent
The current progress in percent.
Definition: Progress.hpp:255
void operator++()
Increases the counter of performed iterations.
Definition: Progress.cpp:196
static void setProgressCallback(ProgressCallbackPtr)
Registers a callback that is called with the new value when the percentage of the progress changed...
Definition: Progress.cpp:186
static void setProgressTitleCallback(ProgressTitleCallbackPtr)
Registers a callback that is called when a new progress instance is created.
Definition: Progress.cpp:78
void print_bar()
Prints the output.
Definition: Progress.cpp:222
void operator++()
Increases the counter of performed iterations.
Definition: Progress.cpp:83
void print_progress()
Prints the current state.
Definition: Progress.cpp:155
boost::mutex m_mutex
A mutex object for counter increment (for parallel executions)
Definition: Progress.hpp:252
boost::mutex m_mutex
A mutex object for counter increment (for parallel executions)
Definition: Progress.hpp:124
ProgressCounter(int stepVal, string prefix="")
Definition: Progress.cpp:138
virtual ~PacmanProgressBar()
Definition: Progress.cpp:181
static void setProgressTitleCallback(ProgressTitleCallbackPtr)
Registers a callback that is called when a new progress instance is created.
Definition: Progress.cpp:191


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:08