BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FitObserver.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Core/Fitting/FitObserver.h
6 //! @brief Defines class FitObserver.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #ifndef BORNAGAIN_CORE_FITTING_FITOBSERVER_H
16 #define BORNAGAIN_CORE_FITTING_FITOBSERVER_H
17 
18 #include "Core/Fitting/FitTypes.h"
19 #include <functional>
20 #include <vector>
21 
22 //! Contains collection of observers and call them at specified intervals.
23 //! Each observer will be called at first iteration and every-nth iterations.
24 
25 template <class T> class FitObserver
26 {
27 public:
28  using observer_t = std::function<void(const T&)>;
30 
31  //! Adds observer to the list.
32  //! @param every_nth: An observer function will be called every_nth iterations.
33  //! @param observer: Observer function to be called.
34  void addObserver(int every_nth, observer_t observer);
35 
36  //! Notifies all observers at their personally specified intervals.
37  //! @param data: The data which will be passed to the observer.
38  void notify(const T& data);
39 
40  void notify_all(const T& data);
41 
42 private:
44  {
45  public:
47  ObserverData(int every_nth, observer_t observer)
48  : m_every_nth(every_nth), m_observer(observer)
49  {
50  }
53  };
54 
55  bool need_notify(int every_nth);
56 
57  std::vector<ObserverData> m_observers;
58  int m_notify_count; //! Total number of notify calls
59 };
60 
61 template <class T> FitObserver<T>::FitObserver() : m_notify_count(0) {}
62 
63 template <class T>
64 void FitObserver<T>::addObserver(int every_nth, typename FitObserver::observer_t observer)
65 {
66  m_observers.push_back(ObserverData(every_nth, observer));
67 }
68 
69 template <class T> void FitObserver<T>::notify(const T& data)
70 {
71  for (const auto& observer : m_observers) {
72  if (need_notify(observer.m_every_nth))
73  observer.m_observer(data);
74  }
75 
76  m_notify_count++;
77 }
78 
79 template <class T> void FitObserver<T>::notify_all(const T& data)
80 {
81  for (const auto& observer : m_observers)
82  observer.m_observer(data);
83 
84  m_notify_count++;
85 }
86 
87 template <class T> bool FitObserver<T>::need_notify(int every_nth)
88 {
89  return m_notify_count == 0 || m_notify_count % every_nth == 0;
90 }
91 
92 #endif // BORNAGAIN_CORE_FITTING_FITOBSERVER_H
Defines common types for fitting library.
ObserverData(int every_nth, observer_t observer)
Definition: FitObserver.h:47
Contains collection of observers and call them at specified intervals.
Definition: FitObserver.h:26
std::vector< ObserverData > m_observers
Definition: FitObserver.h:57
std::function< void(const T &)> observer_t
Definition: FitObserver.h:28
void notify(const T &data)
Notifies all observers at their personally specified intervals.
Definition: FitObserver.h:69
void notify_all(const T &data)
Definition: FitObserver.h:79
void addObserver(int every_nth, observer_t observer)
Adds observer to the list.
Definition: FitObserver.h:64
bool need_notify(int every_nth)
Definition: FitObserver.h:87
int m_notify_count
Definition: FitObserver.h:58