BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FitObserver< T > Class Template Reference

Contains collection of observers and call them at specified intervals. More...

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
 

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

Constructor & Destructor Documentation

◆ FitObserver()

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

Definition at line 64 of file FitObserver.h.

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

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

68 {
69  m_observers.push_back(ObserverData(every_nth, observer));
70 }
std::vector< ObserverData > m_observers
Definition: FitObserver.h:60

Referenced by FitStatus::addObserver().

◆ need_notify()

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

Definition at line 90 of file FitObserver.h.

91 {
92  return m_notify_count == 0 || m_notify_count % every_nth == 0;
93 }

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

73 {
74  for (const auto& observer : m_observers) {
75  if (need_notify(observer.m_every_nth))
76  observer.m_observer(data);
77  }
78 
80 }
bool need_notify(int every_nth)
Definition: FitObserver.h:90

Referenced by FitStatus::update().

◆ notify_all()

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

Definition at line 82 of file FitObserver.h.

83 {
84  for (const auto& observer : m_observers)
85  observer.m_observer(data);
86 
88 }

Referenced by FitStatus::finalize().

Member Data Documentation

◆ m_notify_count

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

Definition at line 61 of file FitObserver.h.

◆ m_observers

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

Definition at line 60 of file FitObserver.h.


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