BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ProgressHandler Class Reference
Collaboration diagram for ProgressHandler:

Public Types

typedef std::function< bool(size_t)> Callback_t
 

Public Member Functions

 ProgressHandler ()
 
 ProgressHandler (const ProgressHandler &other)
 
void subscribe (ProgressHandler::Callback_t callback)
 
void reset ()
 
void setExpectedNTicks (size_t n)
 
void incrementDone (size_t ticks_done)
 
bool alive ()
 

Private Member Functions

bool defaultMonitorExec (int)
 

Private Attributes

Callback_t m_inform
 
size_t m_expected_nticks
 
size_t m_completed_nticks
 
bool m_continuation_flag
 

Detailed 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 30 of file ProgressHandler.h.

Member Typedef Documentation

◆ Callback_t

typedef std::function<bool(size_t)> ProgressHandler::Callback_t

Definition at line 33 of file ProgressHandler.h.

Constructor & Destructor Documentation

◆ ProgressHandler() [1/2]

ProgressHandler::ProgressHandler ( )
inline

Definition at line 35 of file ProgressHandler.h.

37  {
38  }
size_t m_completed_nticks
Callback_t m_inform
size_t m_expected_nticks

◆ ProgressHandler() [2/2]

ProgressHandler::ProgressHandler ( const ProgressHandler other)
inline

Definition at line 39 of file ProgressHandler.h.

40  : m_inform(other.m_inform) // not clear whether we want multiple copies of this
41  ,
43  {
44  }

Member Function Documentation

◆ subscribe()

void ProgressHandler::subscribe ( ProgressHandler::Callback_t  callback)

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.

Referenced by Simulation::setTerminalProgressMonitor(), and Simulation::subscribe().

◆ reset()

void ProgressHandler::reset ( )
inline

Definition at line 46 of file ProgressHandler.h.

47  {
49  m_continuation_flag = true;
50  }

References m_completed_nticks, and m_continuation_flag.

Referenced by Simulation::runSimulation().

◆ setExpectedNTicks()

void ProgressHandler::setExpectedNTicks ( size_t  n)
inline

Definition at line 51 of file ProgressHandler.h.

51 { m_expected_nticks = n; }

References m_expected_nticks.

Referenced by Simulation::runSimulation().

◆ 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().

◆ alive()

bool ProgressHandler::alive ( )
inline

◆ defaultMonitorExec()

bool ProgressHandler::defaultMonitorExec ( int  )
private

Member Data Documentation

◆ m_inform

Callback_t ProgressHandler::m_inform
private

Definition at line 56 of file ProgressHandler.h.

Referenced by incrementDone(), and subscribe().

◆ m_expected_nticks

size_t ProgressHandler::m_expected_nticks
private

Definition at line 57 of file ProgressHandler.h.

Referenced by incrementDone(), and setExpectedNTicks().

◆ m_completed_nticks

size_t ProgressHandler::m_completed_nticks
private

Definition at line 58 of file ProgressHandler.h.

Referenced by incrementDone(), and reset().

◆ m_continuation_flag

bool ProgressHandler::m_continuation_flag
private

Definition at line 59 of file ProgressHandler.h.

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


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