1 #ifndef ROSE_Progress_H
2 #define ROSE_Progress_H
4 #include <Rose/BasicTypes.h>
6 #include <Rose/Constants.h>
8 #include <boost/chrono.hpp>
9 #include <boost/thread.hpp>
10 #include <rose_isnan.h>
11 #include <Sawyer/Stopwatch.h>
12 #include <Sawyer/Synchronization.h>
187 : completion(0.0), maximum(1.0) {}
190 explicit Report(
double completion,
double maximum = 1.0)
191 : completion(completion), maximum(maximum) {}
194 Report(
const std::string &name,
double completion,
double maximum = 1.0)
195 : name(name), completion(completion), maximum(maximum) {}
202 mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
203 #if SAWYER_MULTI_THREADED
204 mutable SAWYER_THREAD_TRAITS::ConditionVariable cv_;
206 std::vector<Report> reports_;
207 size_t reportNumber_;
240 void update(
double completion,
double maximum = 1.0);
241 void update(
const Report&);
259 Report
push(
double completion,
double maximum = 1.0);
260 Report
push(
const Report&);
278 void pop(
double completion,
double maximum = 1.0);
279 void pop(
const Report&);
303 void finished(
double completion,
double maximum = 1.0);
326 std::pair<Report,
double >
reportLatest(
const std::string &nameSeparator =
".")
const;
349 template<
class Functor>
350 bool reportRegularly(boost::chrono::milliseconds interval, Functor f,
const std::string &nameSeparator =
".")
const {
352 #if SAWYER_MULTI_THREADED && !defined(ROSE_USE_CMAKE)
354 std::pair<Report,
double > rpt =
reportLatest(nameSeparator);
355 if (!f(rpt.first, rpt.second))
359 boost::this_thread::sleep_for(interval);
386 template<
class Functor>
387 bool reportChanges(boost::chrono::milliseconds limit, Functor f,
const std::string &nameSeparator =
".")
const {
388 #if SAWYER_MULTI_THREADED && !defined(ROSE_USE_CMAKE) // makes no sense for single threaded. Does not compile with cmake
390 size_t seen = TERMINATING - 1;
392 SAWYER_THREAD_TRAITS::UniqueLock lock(mutex_);
393 while (reportNumber_ == seen)
395 ASSERT_forbid(reports_.empty());
396 Report report = reports_.back();
397 report.name = reportNameNS(nameSeparator);
398 seen = reportNumber_;
403 if (TERMINATING == seen)
405 boost::this_thread::sleep_for(limit);
413 std::string reportNameNS(
const std::string &nameSeparator)
const;
445 : progress_(progress) {
448 if (!rose_isnan(afterCompletion))
457 if (!rose_isnan(afterCompletion))
464 progress_->pop(after_);
Report(const std::string &name, double completion, double maximum=1.0)
Report with name and completion.
A single progress report.
void cancel()
Cancel all cleanup operations.
Report(double completion, double maximum=1.0)
Report completion with default name.
Progress::Ptr progress() const
Progress object being used.
ProgressTask(const std::string &name, double afterCompletion=NAN)
Create progress object for subtask.
std::string name
What is being reported.
ProgressTask(const Progress::Ptr &progress, const std::string &name, double afterCompletion=NAN)
Prepare existing progress object for subtask.
Main namespace for the ROSE library.
void update(double completion, double maximum=1.0)
Make a progress report.
static Ptr instance()
Factory to create a new instance of this class.
std::pair< Report, double > reportLatest(const std::string &nameSeparator=".") const
Latest report and its age in seconds.
bool isFinished() const
Predicate indicating whether the task is finished.
void finished()
Indicate that the task is complete.
Report()
Initial progress report.
Report push()
Push a new progress phase onto the stack.
bool reportRegularly(boost::chrono::milliseconds interval, Functor f, const std::string &nameSeparator=".") const
Invoke the specified function at regular intervals.
void pop()
Pop the top progress phase from the stack.
double completion
Estimated degree of completion.
Base class for reference counted objects.
A general, thread-safe way to report progress made on some task.
bool reportChanges(boost::chrono::milliseconds limit, Functor f, const std::string &nameSeparator=".") const
Invoke the specified function each time the progress changes.
~ProgressTask()
Clean up subtask progress.
const size_t INVALID_INDEX(static_cast< size_t >(-1))
Invalid array index.
ProgressPtr Ptr
Progress objects are reference counted.
double maximum
Maximum value for completion.