BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
FitObserver< T > Class Template Reference

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 31 of file FitObserver.h.

Collaboration diagram for FitObserver< T >:
[legend]

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)
 Adds observer to the list. More...
 
void notify (const T &data)
 Notifies all observers at their personally specified intervals. More...
 
void notify_all (const T &data)
 

Private Member Functions

bool need_notify (int every_nth)
 

Private Attributes

int m_notify_count
 
std::vector< ObserverDatam_observers
 

Member Typedef Documentation

◆ observer_t

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

Definition at line 33 of file FitObserver.h.

Constructor & Destructor Documentation

◆ FitObserver()

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

Definition at line 70 of file FitObserver.h.

71  : m_notify_count(0)
72 {
73 }
int m_notify_count
Definition: FitObserver.h:66

Member Function Documentation

◆ addObserver()

template<class T >
void FitObserver< T >::addObserver ( int  every_nth,
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 76 of file FitObserver.h.

77 {
78  m_observers.emplace_back(ObserverData(every_nth, observer));
79 }
std::vector< ObserverData > m_observers
Definition: FitObserver.h:65

Referenced by FitStatus::addObserver().

◆ need_notify()

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

Definition at line 102 of file FitObserver.h.

103 {
104  return m_notify_count == 0 || m_notify_count % every_nth == 0;
105 }

◆ 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 82 of file FitObserver.h.

83 {
84  for (const auto& observer : m_observers) {
85  if (need_notify(observer.m_every_nth))
86  observer.m_observer(data);
87  }
88 
90 }
bool need_notify(int every_nth)
Definition: FitObserver.h:102

Referenced by FitStatus::update().

◆ notify_all()

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

Definition at line 93 of file FitObserver.h.

94 {
95  for (const auto& observer : m_observers)
96  observer.m_observer(data);
97 
99 }

Referenced by FitStatus::finalize().

Member Data Documentation

◆ m_notify_count

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

Definition at line 66 of file FitObserver.h.

◆ m_observers

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

Definition at line 65 of file FitObserver.h.


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