BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FitObserver< T > Class Template Reference
Collaboration diagram for FitObserver< T >:

Classes

class  ObserverData
 

Public Types

using observer_t = std::function< void(const T &)>
 

Public Member Functions

 FitObserver ()
 
void addObserver (int every_nth, observer_t observer)
 
void notify (const T &data)
 
void notify_all (const T &data)
 

Private Member Functions

bool need_notify (int every_nth)
 

Private Attributes

std::vector< ObserverDatam_observers
 
int m_notify_count
 

Detailed Description

template<class T>
class FitObserver< T >

Contains collection of observers and call them at specified intervals.

Each observer will be called at first iteration and every-nth iterations.

Definition at line 25 of file FitObserver.h.

Member Typedef Documentation

◆ observer_t

template<class T >
using FitObserver< T >::observer_t = std::function<void(const T&)>

Definition at line 28 of file FitObserver.h.

Constructor & Destructor Documentation

◆ FitObserver()

template<class T >
FitObserver< T >::FitObserver

Definition at line 61 of file FitObserver.h.

61 : m_notify_count(0) {}
int m_notify_count
Definition: FitObserver.h:58

Member Function Documentation

◆ addObserver()

template<class T >
void FitObserver< T >::addObserver ( int  every_nth,
typename FitObserver< T >::observer_t  observer 
)

Adds observer to the list.

Parameters
every_nthAn observer function will be called every_nth iterations.
observerObserver function to be called.

Definition at line 64 of file FitObserver.h.

65 {
66  m_observers.push_back(ObserverData(every_nth, observer));
67 }
std::vector< ObserverData > m_observers
Definition: FitObserver.h:57

Referenced by FitStatus::addObserver().

◆ notify()

template<class T >
void FitObserver< T >::notify ( const T &  data)

Notifies all observers at their personally specified intervals.

Parameters
dataThe data which will be passed to the observer.

Definition at line 69 of file FitObserver.h.

70 {
71  for (const auto& observer : m_observers) {
72  if (need_notify(observer.m_every_nth))
73  observer.m_observer(data);
74  }
75 
77 }
bool need_notify(int every_nth)
Definition: FitObserver.h:87

Referenced by FitStatus::update().

◆ notify_all()

template<class T >
void FitObserver< T >::notify_all ( const T &  data)

Definition at line 79 of file FitObserver.h.

80 {
81  for (const auto& observer : m_observers)
82  observer.m_observer(data);
83 
85 }

Referenced by FitStatus::finalize().

◆ need_notify()

template<class T >
bool FitObserver< T >::need_notify ( int  every_nth)
private

Definition at line 87 of file FitObserver.h.

88 {
89  return m_notify_count == 0 || m_notify_count % every_nth == 0;
90 }

Member Data Documentation

◆ m_observers

template<class T >
std::vector<ObserverData> FitObserver< T >::m_observers
private

Definition at line 57 of file FitObserver.h.

◆ m_notify_count

template<class T >
int FitObserver< T >::m_notify_count
private

Definition at line 58 of file FitObserver.h.


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