BornAgain  1.19.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 reflection and scattering
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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_CORE_FITTING_FITOBSERVER_H
21 #define BORNAGAIN_CORE_FITTING_FITOBSERVER_H
22 
23 #include "Core/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> class FitObserver {
31 public:
32  using observer_t = std::function<void(const T&)>;
34 
35  //! Adds observer to the list.
36  //! @param every_nth: An observer function will be called every_nth iterations.
37  //! @param observer: Observer function to be called.
38  void addObserver(int every_nth, observer_t observer);
39 
40  //! Notifies all observers at their personally specified intervals.
41  //! @param data: The data which will be passed to the observer.
42  void notify(const T& data);
43 
44  void notify_all(const T& data);
45 
46 private:
47  class ObserverData {
48  public:
50  ObserverData(int every_nth, observer_t observer)
51  : m_every_nth(every_nth), m_observer(observer)
52  {
53  }
56  };
57 
58  bool need_notify(int every_nth);
59 
60  std::vector<ObserverData> m_observers;
61  int m_notify_count; //! Total number of notify calls
62 };
63 
64 template <class T> FitObserver<T>::FitObserver() : m_notify_count(0) {}
65 
66 template <class T>
67 void FitObserver<T>::addObserver(int every_nth, typename FitObserver::observer_t observer)
68 {
69  m_observers.push_back(ObserverData(every_nth, observer));
70 }
71 
72 template <class T> void FitObserver<T>::notify(const T& data)
73 {
74  for (const auto& observer : m_observers) {
75  if (need_notify(observer.m_every_nth))
76  observer.m_observer(data);
77  }
78 
79  m_notify_count++;
80 }
81 
82 template <class T> void FitObserver<T>::notify_all(const T& data)
83 {
84  for (const auto& observer : m_observers)
85  observer.m_observer(data);
86 
87  m_notify_count++;
88 }
89 
90 template <class T> bool FitObserver<T>::need_notify(int every_nth)
91 {
92  return m_notify_count == 0 || m_notify_count % every_nth == 0;
93 }
94 
95 #endif // BORNAGAIN_CORE_FITTING_FITOBSERVER_H
96 #endif // USER_API
Defines common types for fitting library.
ObserverData(int every_nth, observer_t observer)
Definition: FitObserver.h:50
Contains collection of observers and call them at specified intervals.
Definition: FitObserver.h:30
std::vector< ObserverData > m_observers
Definition: FitObserver.h:60
std::function< void(const T &)> observer_t
Definition: FitObserver.h:32
void notify(const T &data)
Notifies all observers at their personally specified intervals.
Definition: FitObserver.h:72
void notify_all(const T &data)
Definition: FitObserver.h:82
void addObserver(int every_nth, observer_t observer)
Adds observer to the list.
Definition: FitObserver.h:67
bool need_notify(int every_nth)
Definition: FitObserver.h:90
int m_notify_count
Definition: FitObserver.h:61