summaryrefslogtreecommitdiff
path: root/klm/util/ersatz_progress.hh
blob: ea6c3bb921b6d67691619234d723d13faf0b5a98 (plain)
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
#ifndef UTIL_ERSATZ_PROGRESS__
#define UTIL_ERSATZ_PROGRESS__

#include <iosfwd>
#include <string>

// Ersatz version of boost::progress so core language model doesn't depend on
// boost.  Also adds option to print nothing.  

namespace util {
class ErsatzProgress {
  public:
    // No output.  
    ErsatzProgress();

    // Null means no output.  The null value is useful for passing along the ostream pointer from another caller.   
    ErsatzProgress(std::ostream *to, const std::string &message, std::size_t complete);

    ~ErsatzProgress();

    ErsatzProgress &operator++() {
      if (++current_ == next_) Milestone();
      return *this;
    }

    ErsatzProgress &operator+=(std::size_t amount) {
      if ((current_ += amount) >= next_) Milestone();
      return *this;
    }

    void Set(std::size_t to) {
      if ((current_ = to) >= next_) Milestone();
      Milestone();
    }

  private:
    void Milestone();

    std::size_t current_, next_, complete_;
    unsigned char stones_written_;
    std::ostream *out_;

    // noncopyable
    ErsatzProgress(const ErsatzProgress &other);
    ErsatzProgress &operator=(const ErsatzProgress &other);
};

} // namespace util

#endif // UTIL_ERSATZ_PROGRESS__