ModErn Text Analysis
META Enumerates Textual Applications
progress.h
Go to the documentation of this file.
1 
10 #ifndef META_UTIL_PROGRESS_H_
11 #define META_UTIL_PROGRESS_H_
12 
13 #include <atomic>
14 #include <chrono>
15 #include <condition_variable>
16 #include <mutex>
17 #include <string>
18 #include <thread>
19 
20 #include "meta/config.h"
21 #include "meta/util/string_view.h"
22 
23 namespace meta
24 {
25 namespace printing
26 {
27 
34 class progress
35 {
36  public:
47  progress(const std::string& prefix, uint64_t length, int interval = 500);
48 
54  void print_endline(bool endline);
55 
60  ~progress();
61 
68  void operator()(uint64_t iter);
69 
73  void end();
74 
78  static void clear();
79 
80  private:
81  void print();
82  void progress_thread();
83 
85  std::thread thread_;
87  std::mutex mutex_;
89  std::condition_variable cond_var_;
91  std::string output_;
93  const std::size_t prefix_len_;
95  const std::chrono::steady_clock::time_point start_;
97  std::atomic<uint64_t> iter_;
99  const uint64_t length_;
101  const int interval_;
103  bool endline_;
104 };
105 
111 {
112  public:
113  null_progress(util::string_view prefix, uint64_t length, int interval = 500)
114  {
115  (void)prefix;
116  (void)length;
117  (void)interval;
118  }
119 
120  void operator()(uint64_t iter)
121  {
122  (void)iter;
123  }
124 };
125 
127 {
128  using type = progress;
129 };
130 
132 {
133  using type = null_progress;
134 };
135 }
136 }
137 #endif
void operator()(uint64_t iter)
Updates the progress indicator.
Definition: progress.cpp:89
progress(const std::string &prefix, uint64_t length, int interval=500)
Constructs a progress reporter with the given prefix and iteration length.
Definition: progress.cpp:21
uint64_t length(const std::string &str)
Definition: utf.cpp:125
Definition: progress.h:126
std::mutex mutex_
The mutex for the condition variable.
Definition: progress.h:87
std::condition_variable cond_var_
The condition variable used by the background thread for sleeping.
Definition: progress.h:89
const uint64_t length_
The total number of iterations.
Definition: progress.h:99
std::string output_
The output line.
Definition: progress.h:91
A non-owning reference to a string.
Definition: string_view.h:51
const int interval_
The length of time, in milliseconds, to wait between updates.
Definition: progress.h:101
~progress()
Destroys this progress reporter.
Definition: progress.cpp:111
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retrieval, data mining, and other applications of text processing.
Definition: analyzer.h:25
void end()
Marks the progress indicator as having finished.
Definition: progress.cpp:94
Class adhering to the progress API that can be substituted for it when no progress output is desired...
Definition: progress.h:110
static void clear()
Clears the last line the progress bar wrote.
Definition: progress.cpp:106
std::thread thread_
The background thread for printing progress updates.
Definition: progress.h:85
bool endline_
Whether or not we should print an endline when done.
Definition: progress.h:103
void print_endline(bool endline)
Sets whether or not an endline should be printed at completion.
Definition: progress.cpp:84
const std::size_t prefix_len_
The length of the prefix.
Definition: progress.h:93
std::atomic< uint64_t > iter_
The current iteration number.
Definition: progress.h:97
Simple class for reporting progress of lengthy operations.
Definition: progress.h:34
const std::chrono::steady_clock::time_point start_
The start time of the job.
Definition: progress.h:95
Definition: progress.h:131