BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ProgressHandler Class Reference

Maintains information about progress of a computation. More...

Public Types

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

Public Member Functions

 ProgressHandler ()
 
 ProgressHandler (const ProgressHandler &other)
 
bool alive ()
 
void incrementDone (size_t ticks_done)
 Increments number of completed computation steps (ticks). More...
 
void reset ()
 
void setExpectedNTicks (size_t n)
 
void subscribe (ProgressHandler::Callback_t callback)
 

Private Member Functions

bool defaultMonitorExec (int)
 

Private Attributes

size_t m_completed_nticks
 
bool m_continuation_flag
 
size_t m_expected_nticks
 
Callback_t m_inform
 

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

Member Typedef Documentation

◆ Callback_t

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

Definition at line 37 of file ProgressHandler.h.

Constructor & Destructor Documentation

◆ ProgressHandler() [1/2]

ProgressHandler::ProgressHandler ( )
inline

Definition at line 39 of file ProgressHandler.h.

41  {
42  }
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 43 of file ProgressHandler.h.

44  : m_inform(other.m_inform) // not clear whether we want multiple copies of this
47  {
48  }

Member Function Documentation

◆ alive()

bool ProgressHandler::alive ( )
inline

◆ 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 50 of file ProgressHandler.h.

51  {
53  m_continuation_flag = true;
54  }

References m_completed_nticks, and m_continuation_flag.

Referenced by ISimulation::runSimulation().

◆ setExpectedNTicks()

void ProgressHandler::setExpectedNTicks ( size_t  n)
inline

Definition at line 55 of file ProgressHandler.h.

55 { m_expected_nticks = n; }

References m_expected_nticks.

Referenced by ISimulation::runSimulation().

◆ 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 ISimulation::setTerminalProgressMonitor(), and ISimulation::subscribe().

Member Data Documentation

◆ m_completed_nticks

size_t ProgressHandler::m_completed_nticks
private

Definition at line 62 of file ProgressHandler.h.

Referenced by incrementDone(), and reset().

◆ m_continuation_flag

bool ProgressHandler::m_continuation_flag
private

Definition at line 63 of file ProgressHandler.h.

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

◆ m_expected_nticks

size_t ProgressHandler::m_expected_nticks
private

Definition at line 61 of file ProgressHandler.h.

Referenced by incrementDone(), and setExpectedNTicks().

◆ m_inform

Callback_t ProgressHandler::m_inform
private

Definition at line 60 of file ProgressHandler.h.

Referenced by incrementDone(), and subscribe().


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