1 #ifndef ROSE_Progress_H
2 #define ROSE_Progress_H
4 #include <rosePublicConfig.h>
5 #include <boost/chrono.hpp>
6 #include <boost/thread.hpp>
7 #include <Rose/Constants.h>
8 #include <rose_isnan.h>
9 #include <Sawyer/SharedPointer.h>
10 #include <Sawyer/Stopwatch.h>
11 #include <Sawyer/Synchronization.h>
186 : completion(0.0), maximum(1.0) {}
189 explicit Report(
double completion,
double maximum = 1.0)
190 : completion(completion), maximum(maximum) {}
193 Report(
const std::string &name,
double completion,
double maximum = 1.0)
194 : name(name), completion(completion), maximum(maximum) {}
201 mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
202 #if SAWYER_MULTI_THREADED
203 mutable SAWYER_THREAD_TRAITS::ConditionVariable cv_;
205 std::vector<Report> reports_;
206 size_t reportNumber_;
239 void update(
double completion,
double maximum = 1.0);
240 void update(
const Report&);
258 Report
push(
double completion,
double maximum = 1.0);
259 Report
push(
const Report&);
277 void pop(
double completion,
double maximum = 1.0);
278 void pop(
const Report&);
302 void finished(
double completion,
double maximum = 1.0);
325 std::pair<Report,
double >
reportLatest(
const std::string &nameSeparator =
".")
const;
348 template<
class Functor>
349 bool reportRegularly(boost::chrono::milliseconds interval, Functor f,
const std::string &nameSeparator =
".")
const {
351 #if SAWYER_MULTI_THREADED && !defined(ROSE_USE_CMAKE)
353 std::pair<Report,
double > rpt =
reportLatest(nameSeparator);
354 if (!f(rpt.first, rpt.second))
358 boost::this_thread::sleep_for(interval);
385 template<
class Functor>
386 bool reportChanges(boost::chrono::milliseconds limit, Functor f,
const std::string &nameSeparator =
".")
const {
387 #if SAWYER_MULTI_THREADED && !defined(ROSE_USE_CMAKE) // makes no sense for single threaded. Does not compile with cmake
389 size_t seen = TERMINATING - 1;
391 SAWYER_THREAD_TRAITS::UniqueLock lock(mutex_);
392 while (reportNumber_ == seen)
394 ASSERT_forbid(reports_.empty());
395 Report report = reports_.back();
396 report.name = reportNameNS(nameSeparator);
397 seen = reportNumber_;
402 if (TERMINATING == seen)
404 boost::this_thread::sleep_for(limit);
412 std::string reportNameNS(
const std::string &nameSeparator)
const;
444 : progress_(progress) {
447 if (!rose_isnan(afterCompletion))
456 if (!rose_isnan(afterCompletion))
463 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(-1)
Invalid array index.
Sawyer::SharedPointer< Progress > Ptr
Progress objects are reference counted.
double maximum
Maximum value for completion.