BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
callbackcontainer.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/signals/callbackcontainer.h
6 //! @brief Defines class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef BORNAGAIN_MVVM_MODEL_MVVM_SIGNALS_CALLBACKCONTAINER_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_SIGNALS_CALLBACKCONTAINER_H
17 
18 #include "mvvm/model_export.h"
20 #include <algorithm>
21 #include <functional>
22 #include <list>
23 
24 namespace ModelView {
25 
26 class SessionItem;
27 class SessionModel;
28 
29 //! Container to hold callbacks in the context of ModelMapper.
30 
31 template <typename T, typename U> class SignalBase {
32 public:
33  SignalBase() = default;
34 
35  void connect(T callback, U client);
36 
37  template <typename... Args> void operator()(Args... args);
38 
39  void remove_client(U client);
40 
41 private:
42  std::list<std::pair<T, U>> m_callbacks;
43 };
44 
45 template <typename T, typename U> void SignalBase<T, U>::connect(T callback, U client)
46 {
47  m_callbacks.push_back(std::make_pair(callback, client));
48 }
49 
50 //! Notify clients using given list of arguments.
51 template <typename T, typename U>
52 template <typename... Args>
53 void SignalBase<T, U>::operator()(Args... args)
54 {
55  for (const auto& f : m_callbacks) {
56  f.first(args...);
57  }
58 }
59 
60 //! Remove client from the list to call back.
61 
62 template <typename T, typename U> void SignalBase<T, U>::remove_client(U client)
63 {
64  m_callbacks.remove_if(
65  [client](const std::pair<T, U>& x) -> bool { return (x.second == client ? true : false); });
66 }
67 
68 //! Callback container for specific client type.
69 
70 template <typename T> class Signal : public SignalBase<T, Callbacks::slot_t> {
71 };
72 
73 } // namespace ModelView
74 
75 #endif // BORNAGAIN_MVVM_MODEL_MVVM_SIGNALS_CALLBACKCONTAINER_H
Defines class CLASS?
Container to hold callbacks in the context of ModelMapper.
void remove_client(U client)
Remove client from the list to call back.
void operator()(Args... args)
Notify clients using given list of arguments.
void connect(T callback, U client)
std::list< std::pair< T, U > > m_callbacks
Callback container for specific client type.
materialitems.h Collection of materials to populate MaterialModel.