BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
FitObserver.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sim/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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_SIM_FITTING_FITOBSERVER_H
21 #define BORNAGAIN_SIM_FITTING_FITOBSERVER_H
22 
23 #include "Sim/Fitting/FitTypes.h"
24 #include <functional>
25 #include <vector>
26 
27 //! Contains collection of observers and call them at specified intervals.
28 //! Each observer will be called at first iteration and every-nth iterations.
29 
30 template <class T>
31 class FitObserver {
32 public:
33  using observer_t = std::function<void(const T&)>;
35 
36  //! Adds observer to the list.
37  //! @param every_nth: An observer function will be called every_nth iterations.
38  //! @param observer: Observer function to be called.
39  void addObserver(int every_nth, observer_t&& observer);
40 
41  //! Notifies all observers at their personally specified intervals.
42  //! @param data: The data which will be passed to the observer.
43  void notify(const T& data);
44 
45  void notify_all(const T& data);
46 
47 private:
48  class ObserverData {
49  public:
51  : m_every_nth(0)
52  {
53  }
54  ObserverData(int every_nth, observer_t observer)
55  : m_every_nth(every_nth)
56  , m_observer(observer)
57  {
58  }
61  };
62 
63  bool need_notify(int every_nth);
64 
65  std::vector<ObserverData> m_observers;
66  int m_notify_count; //! Total number of notify calls
67 };
68 
69 template <class T>
71  : m_notify_count(0)
72 {
73 }
74 
75 template <class T>
77 {
78  m_observers.emplace_back(ObserverData(every_nth, observer));
79 }
80 
81 template <class T>
82 void FitObserver<T>::notify(const T& data)
83 {
84  for (const auto& observer : m_observers) {
85  if (need_notify(observer.m_every_nth))
86  observer.m_observer(data);
87  }
88 
89  m_notify_count++;
90 }
91 
92 template <class T>
93 void FitObserver<T>::notify_all(const T& data)
94 {
95  for (const auto& observer : m_observers)
96  observer.m_observer(data);
97 
98  m_notify_count++;
99 }
100 
101 template <class T>
102 bool FitObserver<T>::need_notify(int every_nth)
103 {
104  return m_notify_count == 0 || m_notify_count % every_nth == 0;
105 }
106 
107 #endif // BORNAGAIN_SIM_FITTING_FITOBSERVER_H
108 #endif // USER_API
Defines common types for fitting library.
ObserverData(int every_nth, observer_t observer)
Definition: FitObserver.h:54
Contains collection of observers and call them at specified intervals. Each observer will be called a...
Definition: FitObserver.h:31
std::vector< ObserverData > m_observers
Definition: FitObserver.h:65
std::function< void(const T &)> observer_t
Definition: FitObserver.h:33
void notify(const T &data)
Notifies all observers at their personally specified intervals.
Definition: FitObserver.h:82
void addObserver(int every_nth, observer_t &&observer)
Adds observer to the list.
Definition: FitObserver.h:76
void notify_all(const T &data)
Definition: FitObserver.h:93
bool need_notify(int every_nth)
Definition: FitObserver.h:102
int m_notify_count
Definition: FitObserver.h:66