LOOS  v2.3.2
ProgressCounters.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2009, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 
25 #if !defined(LOOS_PROGRESSCOUNTERS_HPP)
26 #define LOOS_PROGRESSCOUNTERS_HPP
27 
28 
29 #include <iostream>
30 #include <string>
31 #include <stdexcept>
32 
33 #include <vector>
34 
35 #include <loos_defs.hpp>
36 #include <timer.hpp>
37 
38 namespace loos {
39 
40  class SimpleCounter;
41 
43  public:
44  virtual ~AbstractObserver() { }
45 
46  virtual void start(SimpleCounter*) =0;
47  virtual void finish(SimpleCounter*) =0;
48  virtual void update(SimpleCounter*) =0;
49  };
50 
51 
53 
66  class SimpleCounter {
67  typedef AbstractObserver ObsT;
68  typedef std::vector<ObsT*> Observers;
69  public:
70 
71  SimpleCounter() : count_(0) { }
72  virtual ~SimpleCounter() { }
73 
74  void attach(ObsT*);
75  void detach(ObsT*);
76 
78  virtual void notify(void);
79 
81  virtual void finish(void);
82 
84  virtual void start(void);
85 
87  uint count(void) const;
88 
90  virtual double elapsed(void);
91 
93  virtual uint remaining(void);
94 
96  virtual double timeRemaining(void);
97 
99  virtual double fractionComplete(void);
100 
101  protected:
102  uint count_;
103  Timer<WallTimer> timer_;
104  Observers observers;
105  };
106 
107 
109 
115  public:
116  EstimatingCounter(const uint n) : expected(n) { }
117 
119  void setExpected(uint);
120 
122  uint remaining(void);
123 
126  double timeRemaining(void);
127 
129  double fractionComplete(void);
130 
131  protected:
132  uint expected;
133  };
134 
135 
138  public:
139  bool operator()(SimpleCounter*) { return(true); }
140  };
141 
142 
144 
154  template<class Trigger = TriggerAlways, class Counter = SimpleCounter>
155  class ProgressCounter : public Counter {
156  public:
157  ProgressCounter(const Trigger& t) : trig(t) { }
158  ProgressCounter(const Trigger& t, const Counter& c) : Counter(c), trig(t) { }
159 
161  void setTrigger(const Trigger& t) { trig = t; }
162 
165  void update(void) {
166  ++Counter::count_;
167  if (trig(this))
168  Counter::notify();
169  }
170 
171  private:
172  Trigger trig;
173  };
174 
175 
176 
177  // ------------------------------------------------------------------------------------------
178 
181 
189  public:
190  BasicProgress() : os_(std::cerr), prefix_("Progress - "), msg_("."), suffix_(" done!\n") { }
191  BasicProgress(std::ostream& os, const std::string& prefix, const std::string& msg, const std::string& suffix) :
192  os_(os), prefix_(prefix), msg_(msg), suffix_(suffix) { }
193 
194  BasicProgress(const std::string& prefix, const std::string& msg, const std::string& suffix) :
195  os_(std::cerr), prefix_(prefix), msg_(msg), suffix_(suffix) { }
196 
197  virtual void start(SimpleCounter*);
198  virtual void update(SimpleCounter*);
199  virtual void finish(SimpleCounter*);
200 
201  protected:
202  std::ostream& os_;
203  std::string prefix_, msg_, suffix_;
204  };
205 
206 
207 
210  public:
211  PercentProgress() : BasicProgress("Progress:\n", "complete", "") { }
212  PercentProgress(std::ostream& os, const std::string& prefix, const std::string& msg, const std::string& suffix) :
213  BasicProgress(os, prefix, msg, suffix) { }
214  PercentProgress(const std::string& prefix, const std::string& msg, const std::string& suffix) :
215  BasicProgress(std::cerr, prefix, msg, suffix) { }
216 
217  void update(SimpleCounter*);
218  void finish(SimpleCounter*);
219  };
220 
221 
224 
233  public:
234  PercentProgressWithTime() : BasicProgress("Progress:\n", "complete", "") { }
235  PercentProgressWithTime(std::ostream& os, const std::string& prefix, const std::string& msg, const std::string& suffix) :
236  BasicProgress(os, prefix, msg, suffix) { }
237  PercentProgressWithTime(const std::string& prefix, const std::string& msg, const std::string& suffix) :
238  BasicProgress(std::cerr, prefix, msg, suffix) { }
239 
240  void update(SimpleCounter*);
241  void finish(SimpleCounter*);
242  };
243 
244 
245 
246 }
247 
248 
249 
250 #endif
virtual void finish(void)
Notify observers that we've finished with our calculation.
void setExpected(uint)
Alter the expected count.
The progress counter front-end.
virtual double elapsed(void)
Total elapsed wall-time.
Provide feedback by percent-complete.
virtual double fractionComplete(void)
Percent complete (if applicable)
virtual void start(void)
Notify observers that we're starting a calculation.
virtual uint remaining(void)
Remaining iterations (if applicable)
double fractionComplete(void)
Returns the percent completed so far.
Basic progress counter object, defining the interface...
virtual double timeRemaining(void)
Remaining time (estimated, again if applicable)
void setTrigger(const Trigger &t)
Change the trigger.
uint count(void) const
Number of iterations we've seen so far.
This is a simple "trigger" for use as a default.
virtual void notify(void)
Notify observers that an update should occur.
Class for tracking time.
Definition: timer.hpp:80
Namespace for most things not already encapsulated within a class.
A progress counter that can estimate how much time is left.
uint remaining(void)
Returns the number of iterations left.