LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GlobalSignal.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_Registry_GlobalSignal_h
2 #define art_Framework_Services_Registry_GlobalSignal_h
3 
5 // GlobalSignal.h
6 //
7 // Define a wrapper for global signals. The watch(...) functions are for
8 // users wishing to register for callbacks; the invoke() and clear()
9 // functions are intended to be called only by art code.
10 //
12 
15 
16 #include <deque>
17 #include <functional>
18 
19 namespace art {
20  template <detail::SignalResponseType, typename ResultType, typename... Args>
21  class GlobalSignal;
22 
23  // Only supported template definition is a partial specialization on
24  // a function type--i.e. and instantiation of GlobalSignal must look
25  // like (e.g.):
26  // GlobalSignal<LIFO, void(std::string const&)> s;
27  template <detail::SignalResponseType SRTYPE,
28  typename ResultType,
29  typename... Args>
30  class GlobalSignal<SRTYPE, ResultType(Args...)> {
31  using tuple_type = std::tuple<Args...>;
32 
33  public:
34  using slot_type = std::function<ResultType(Args...)>;
35  using result_type = ResultType;
36  template <std::size_t I>
37  using slot_argument_type = std::tuple_element_t<I, tuple_type>;
38 
39  // 1. Free function or functor (or pre-bound member function).
40  void watch(std::function<ResultType(Args...)> slot);
41  // 2a. Non-const member function.
42  template <typename T>
43  void watch(ResultType (T::*slot)(Args...), T& t);
44  // 2b. Non-const member function (legacy).
45  template <typename T>
46  void watch(T* t, ResultType (T::*slot)(Args...));
47  // 3a. Const member function.
48  template <typename T>
49  void watch(ResultType (T::*slot)(Args...) const, T const& t);
50  // 3b. Const member function (legacy).
51  template <typename T>
52  void watch(T const* t, ResultType (T::*slot)(Args...) const);
53 
54  void invoke(Args const&... args) const; // Discard ResultType.
55 
56  private:
57  std::deque<slot_type> signal_;
58  };
59 
60  // 1.
61  template <detail::SignalResponseType SRTYPE,
62  typename ResultType,
63  typename... Args>
64  void
65  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
66  std::function<ResultType(Args...)> slot)
67  {
68  detail::connect_to_signal<SRTYPE>(signal_, slot);
69  }
70 
71  // 2a.
72  template <detail::SignalResponseType SRTYPE,
73  typename ResultType,
74  typename... Args>
75  template <typename T>
76  void
77  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
78  ResultType (T::*slot)(Args...),
79  T& t)
80  {
81  watch(detail::makeWatchFunc(slot, t));
82  }
83 
84  // 2b.
85  template <detail::SignalResponseType SRTYPE,
86  typename ResultType,
87  typename... Args>
88  template <typename T>
89  void
90  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
91  T* t,
92  ResultType (T::*slot)(Args...))
93  {
94  watch(detail::makeWatchFunc(slot, *t));
95  }
96 
97  // 3a.
98  template <detail::SignalResponseType SRTYPE,
99  typename ResultType,
100  typename... Args>
101  template <typename T>
102  void
103  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
104  ResultType (T::*slot)(Args...) const,
105  T const& t)
106  {
107  watch(detail::makeWatchFunc(slot, t));
108  }
109 
110  // 3b.
111  template <detail::SignalResponseType SRTYPE,
112  typename ResultType,
113  typename... Args>
114  template <typename T>
115  void
116  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
117  T const* t,
118  ResultType (T::*slot)(Args...) const)
119  {
120  watch(detail::makeWatchFunc(slot, *t));
121  }
122 
123  template <detail::SignalResponseType SRTYPE,
124  typename ResultType,
125  typename... Args>
126  void
127  GlobalSignal<SRTYPE, ResultType(Args...)>::invoke(Args const&... args) const
128  {
129  for (auto f : signal_) {
130  f(args...);
131  }
132  }
133 
134 } // namespace art
135 #endif /* art_Framework_Services_Registry_GlobalSignal_h */
136 
137 // Local Variables:
138 // mode: c++
139 // End:
std::function< ResultType(Args...)> makeWatchFunc(ResultType(T::*slot)(Args...), T &t)
Definition: makeWatchFunc.h:16
TFile f
Definition: plotHisto.C:6
std::function< ResultType(Args...)> slot_type
Definition: GlobalSignal.h:34
Definition: MVAAlg.h:12
std::tuple_element_t< I, tuple_type > slot_argument_type
Definition: GlobalSignal.h:37