-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprogress_writer.h
80 lines (65 loc) · 1.48 KB
/
progress_writer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef PROGRESS_WRITER_H
#define PROGRESS_WRITER_H
#include <boost/optional.hpp>
#include <functional>
#include <string>
#include <vector>
#include <iostream>
class threaded_progress_writer {
private:
std::vector<int> ps_;
std::string item_;
mutable bool ended_;
bool silent_;
public:
boost::optional<std::function<void(float)>> application_progress_callback;
threaded_progress_writer(const std::string& item, int n, bool silent=false)
: item_(item), ended_(false), silent_(silent)
{
ps_.resize(n);
}
~threaded_progress_writer() {
end();
}
void operator()(int n, int i);
void end() const {
if (silent_) return;
if (!ended_) {
std::cerr << std::endl;
ended_ = true;
}
}
};
class progress_writer {
private:
std::string item_;
mutable bool ended_;
bool silent_;
public:
boost::optional<std::function<void(float)>> application_progress_callback;
progress_writer() : silent_(true) {}
progress_writer(const std::string& item, bool silent = false)
: item_(item), ended_(false), silent_(silent) {}
~progress_writer() {
end();
}
void operator()(int i) const {
if (!silent_) {
std::cerr << "\r" << item_ << " " << i;
std::cerr.flush();
if (application_progress_callback) {
(*application_progress_callback)((float)i / 100.f);
}
}
}
void end() const {
if (!silent_ && !ended_) {
std::cerr << std::endl;
ended_ = true;
}
}
threaded_progress_writer thread(int n) {
return threaded_progress_writer(item_, n, silent_);
}
};
#endif