BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ProgressHandler Class Reference

Description

Maintains information about progress of a computation.

Owner is the computation, which periodically calls the thread-safe function incrementDone(..). An application (GUI or script) may subscribe(..) to be informed about progress. It is then periodically called back by inform(..). The return value of inform(..) can be used to request termination of the computation.

Definition at line 34 of file ProgressHandler.h.

Public Types

using Callback_t = std::function< bool(size_t)>
 

Public Member Functions

 ProgressHandler ()
 
 ProgressHandler (const ProgressHandler &other)
 
bool alive ()
 
void incrementDone (size_t ticks_done)
 Increments number of completed computation steps (ticks). Performs callback (method m_inform) to inform the subscriber about the state of the computation and to obtain as return value a flag that indicates whether to continue the computation. More...
 
void reset ()
 
void setExpectedNTicks (size_t n)
 
void subscribe (ProgressHandler::Callback_t inform)
 

Private Member Functions

bool defaultMonitorExec (int)
 

Private Attributes

size_t m_completed_nticks {0}
 
bool m_continuation_flag {true}
 
size_t m_expected_nticks {0}
 
Callback_t m_inform
 

Member Typedef Documentation

◆ Callback_t

using ProgressHandler::Callback_t = std::function<bool(size_t)>

Definition at line 36 of file ProgressHandler.h.

Constructor & Destructor Documentation

◆ ProgressHandler() [1/2]

ProgressHandler::ProgressHandler ( )
inline

Definition at line 38 of file ProgressHandler.h.

39  : m_inform(nullptr)
40  {
41  }
Callback_t m_inform

◆ ProgressHandler() [2/2]

ProgressHandler::ProgressHandler ( const ProgressHandler other)
inline

Definition at line 42 of file ProgressHandler.h.

43  : m_inform(other.m_inform) // not clear whether we want multiple copies of this
46  {
47  }
size_t m_completed_nticks
size_t m_expected_nticks

Member Function Documentation

◆ alive()

bool ProgressHandler::alive ( )
inline

Definition at line 56 of file ProgressHandler.h.

56 { return m_continuation_flag; }

References m_continuation_flag.

Referenced by IComputation::compute(), and DWBAComputation::runProtected().

◆ defaultMonitorExec()

bool ProgressHandler::defaultMonitorExec ( int  )
private

◆ incrementDone()

void ProgressHandler::incrementDone ( size_t  ticks_done)

Increments number of completed computation steps (ticks). Performs callback (method m_inform) to inform the subscriber about the state of the computation and to obtain as return value a flag that indicates whether to continue the computation.

Definition at line 31 of file ProgressHandler.cpp.

32 {
33  static std::mutex single_mutex;
34  std::unique_lock<std::mutex> single_lock(single_mutex);
35 
36  m_completed_nticks += ticks_done;
39 
40  int percentage_done = (int)(100. * m_completed_nticks / m_expected_nticks);
41  // fractional part is discarded, which is fine here:
42  // the value 100 is only returned if everything is done
43 
44  m_continuation_flag = (!m_inform || m_inform(percentage_done)) && m_continuation_flag;
45 }

References m_completed_nticks, m_continuation_flag, m_expected_nticks, and m_inform.

Referenced by DelayedProgressCounter::stepProgress().

◆ reset()

void ProgressHandler::reset ( )
inline

Definition at line 49 of file ProgressHandler.h.

50  {
52  m_continuation_flag = true;
53  }

References m_completed_nticks, and m_continuation_flag.

◆ setExpectedNTicks()

void ProgressHandler::setExpectedNTicks ( size_t  n)
inline

Definition at line 54 of file ProgressHandler.h.

54 { m_expected_nticks = n; }

References m_expected_nticks.

◆ subscribe()

void ProgressHandler::subscribe ( ProgressHandler::Callback_t  inform)

Definition at line 19 of file ProgressHandler.cpp.

20 {
21  if (m_inform)
22  throw std::runtime_error("Invalid call of ProgressHandler::subscribe: "
23  "currently, no more than one subscriber is allowed");
24  m_inform = inform;
25 }

References m_inform.

Member Data Documentation

◆ m_completed_nticks

size_t ProgressHandler::m_completed_nticks {0}
private

Definition at line 61 of file ProgressHandler.h.

Referenced by incrementDone(), and reset().

◆ m_continuation_flag

bool ProgressHandler::m_continuation_flag {true}
private

Definition at line 62 of file ProgressHandler.h.

Referenced by alive(), incrementDone(), and reset().

◆ m_expected_nticks

size_t ProgressHandler::m_expected_nticks {0}
private

Definition at line 60 of file ProgressHandler.h.

Referenced by incrementDone(), and setExpectedNTicks().

◆ m_inform

Callback_t ProgressHandler::m_inform
private

Definition at line 59 of file ProgressHandler.h.

Referenced by incrementDone(), and subscribe().


The documentation for this class was generated from the following files: