Cadabra
Computer algebra system for field theory problems
ProgressMonitor.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <string>
5 #include <chrono>
6 #include <stack>
7 #include <map>
8 #include <vector>
9 #include <functional>
10 
16 
18  public:
19  ProgressMonitor(std::function<void(const std::string&, int, int)> report = nullptr, int report_level = 2);
20  virtual ~ProgressMonitor();
21 
28  void group(std::string name = "", int total = 0, int level = -1);
29 
34  void progress(); // Increment current step by 1
35  void progress(int n);
36  void progress(int n, int total);
37 
39 
40  void message(const std::string&);
41 
43 
44  void print() const;
45 
47  std::function<void(const std::string&, int, int)> report;
48 
49  // Level above which to report progress updates and not only add to totals
51 
54 
55  class Total {
56  public:
57  Total();
58 
59  std::string name;
60  size_t call_count;
61  std::chrono::milliseconds time_spent;
63  std::vector<std::string> messages;
64 
65  long time_spent_as_long() const;
66 
67  bool operator==(const Total& other) const;
68 
69  std::string str() const;
70  };
71 
72  std::vector<Total> totals() const;
73 
74  private:
82 
83  class Block {
84  public:
85  Block(const std::string& name, int level);
86 
87  std::string name;
88  std::chrono::milliseconds started;
90  std::vector<std::string> messages;
91  int level;
92  };
93 
94  std::stack<Block> call_stack;
95  std::map<std::string, Total> call_totals;
96  };
97 
98 
100 {
101 public:
102  ScopedProgressGroup(ProgressMonitor* pm, const std::string& name, int total = 0, int level = -1);
104 
105  void progress();
106  void progress(int n);
107  void progress(int n, int total);
108 
109 private:
111 };
ProgressMonitor::message
void message(const std::string &)
Log out-of-band messages to the current block.
Definition: ProgressMonitor.cc:113
ProgressMonitor::Block::Block
Block(const std::string &name, int level)
Definition: ProgressMonitor.cc:17
ProgressMonitor::group
void group(std::string name="", int total=0, int level=-1)
Start a new named group, or close the innermost one in case the name argument is empty.
Definition: ProgressMonitor.cc:39
ProgressMonitor::report_level
int report_level
Definition: ProgressMonitor.hh:50
ScopedProgressGroup::ScopedProgressGroup
ScopedProgressGroup(ProgressMonitor *pm, const std::string &name, int total=0, int level=-1)
Definition: ProgressMonitor.cc:154
ProgressMonitor::Total::messages
std::vector< std::string > messages
Definition: ProgressMonitor.hh:63
ProgressMonitor::Block::started
std::chrono::milliseconds started
Definition: ProgressMonitor.hh:88
ProgressMonitor::Total::str
std::string str() const
Definition: ProgressMonitor.cc:28
ProgressMonitor::ProgressMonitor
ProgressMonitor(std::function< void(const std::string &, int, int)> report=nullptr, int report_level=2)
Definition: ProgressMonitor.cc:7
ProgressMonitor::~ProgressMonitor
virtual ~ProgressMonitor()
Definition: ProgressMonitor.cc:13
ProgressMonitor::Block
A single element of the nested group call stack.
Definition: ProgressMonitor.hh:83
ProgressMonitor::totals
std::vector< Total > totals() const
Definition: ProgressMonitor.cc:136
ProgressMonitor::Block::step
int step
Definition: ProgressMonitor.hh:89
ProgressMonitor::call_totals
std::map< std::string, Total > call_totals
Definition: ProgressMonitor.hh:95
ScopedProgressGroup::~ScopedProgressGroup
~ScopedProgressGroup()
Definition: ProgressMonitor.cc:161
ProgressMonitor
Definition: ProgressMonitor.hh:17
ProgressMonitor::Block::total_steps
int total_steps
Definition: ProgressMonitor.hh:89
ProgressMonitor::Total::call_count
size_t call_count
Definition: ProgressMonitor.hh:60
ProgressMonitor::Block::name
std::string name
Definition: ProgressMonitor.hh:87
ProgressMonitor::Total::operator==
bool operator==(const Total &other) const
Definition: ProgressMonitor.cc:145
ProgressMonitor::Block::messages
std::vector< std::string > messages
Definition: ProgressMonitor.hh:90
ProgressMonitor::call_stack
std::stack< Block > call_stack
Definition: ProgressMonitor.hh:94
ProgressMonitor::progress
void progress()
Set the progress of the current top-level block to be n out of total steps.
Definition: ProgressMonitor.cc:91
ProgressMonitor::Total::time_spent_as_long
long time_spent_as_long() const
Definition: ProgressMonitor.cc:118
ScopedProgressGroup::pm
ProgressMonitor * pm
Definition: ProgressMonitor.hh:110
ProgressMonitor::report
std::function< void(const std::string &, int, int)> report
Callback for reporting a progress update.
Definition: ProgressMonitor.hh:47
ProgressMonitor::Total::name
std::string name
Definition: ProgressMonitor.hh:59
ScopedProgressGroup::progress
void progress()
Definition: ProgressMonitor.cc:167
ProgressMonitor::print
void print() const
Generate debug output on cerr.
Definition: ProgressMonitor.cc:123
ProgressMonitor::Total::Total
Total()
Definition: ProgressMonitor.cc:23
ProgressMonitor::Total
Object to accumulate total time and call counts for a particular named execution group.
Definition: ProgressMonitor.hh:55
ProgressMonitor::Total::total_steps
int total_steps
Definition: ProgressMonitor.hh:62
ScopedProgressGroup
Definition: ProgressMonitor.hh:99
ProgressMonitor::Block::level
int level
Definition: ProgressMonitor.hh:91
ProgressMonitor::Total::time_spent
std::chrono::milliseconds time_spent
Definition: ProgressMonitor.hh:61