LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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 
17 
18 #include <deque>
19 #include <functional>
20 
21 namespace art {
22  template <detail::SignalResponseType, typename ResultType, typename... Args>
23  class GlobalSignal;
24 
25  // Only supported template definition is a partial specialization on
26  // a function type--i.e. and instantiation of GlobalSignal must look
27  // like (e.g.):
28  // GlobalSignal<LIFO, void(std::string const&)> s;
29  template <detail::SignalResponseType SRTYPE,
30  typename ResultType,
31  typename... Args>
32  class GlobalSignal<SRTYPE, ResultType(Args...)> {
33  public:
34  // Typedefs
35  using slot_type = std::function<ResultType(Args...)>;
36  using result_type = ResultType;
37 
38  // 1. Free function or functor (or pre-bound member function).
39  void watch(std::function<ResultType(Args...)> slot);
40  // 2a. Non-const member function.
41  template <typename T>
42  void watch(ResultType (T::*slot)(Args...), T& t);
43  // 2b. Non-const member function (legacy).
44  template <typename T>
45  void watch(T* t, ResultType (T::*slot)(Args...));
46  // 3a. Const member function.
47  template <typename T>
48  void watch(ResultType (T::*slot)(Args...) const, T const& t);
49  // 3b. Const member function (legacy).
50  template <typename T>
51  void watch(T const* t, ResultType (T::*slot)(Args...) const);
52 
53  void invoke(Args&&... args) const; // Discard ResultType.
54 
55  void clear();
56 
57  private:
58  std::deque<slot_type> signal_;
59  };
60 
61  // 1.
62  template <detail::SignalResponseType SRTYPE,
63  typename ResultType,
64  typename... Args>
65  void
66  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
67  std::function<ResultType(Args...)> slot)
68  {
69  detail::connect_to_signal<SRTYPE>(signal_, slot);
70  }
71 
72  // 2a.
73  template <detail::SignalResponseType SRTYPE,
74  typename ResultType,
75  typename... Args>
76  template <typename T>
77  void
78  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
79  ResultType (T::*slot)(Args...),
80  T& t)
81  {
82  watch(detail::makeWatchFunc(slot, t));
83  }
84 
85  // 2b.
86  template <detail::SignalResponseType SRTYPE,
87  typename ResultType,
88  typename... Args>
89  template <typename T>
90  void
91  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
92  T* t,
93  ResultType (T::*slot)(Args...))
94  {
95  watch(detail::makeWatchFunc(slot, *t));
96  }
97 
98  // 3a.
99  template <detail::SignalResponseType SRTYPE,
100  typename ResultType,
101  typename... Args>
102  template <typename T>
103  void
104  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
105  ResultType (T::*slot)(Args...) const,
106  T const& t)
107  {
108  watch(detail::makeWatchFunc(slot, t));
109  }
110 
111  // 3b.
112  template <detail::SignalResponseType SRTYPE,
113  typename ResultType,
114  typename... Args>
115  template <typename T>
116  void
117  GlobalSignal<SRTYPE, ResultType(Args...)>::watch(
118  T const* t,
119  ResultType (T::*slot)(Args...) const)
120  {
121  watch(detail::makeWatchFunc(slot, *t));
122  }
123 
124  template <detail::SignalResponseType SRTYPE,
125  typename ResultType,
126  typename... Args>
127  void
128  GlobalSignal<SRTYPE, ResultType(Args...)>::invoke(Args&&... args) const
129  {
130  for (auto f : signal_) {
131  f(std::forward<Args>(args)...);
132  }
133  }
134 
135  template <detail::SignalResponseType SRTYPE,
136  typename ResultType,
137  typename... Args>
138  void
139  GlobalSignal<SRTYPE, ResultType(Args...)>::clear()
140  {
141  signal_.clear();
142  }
143 }
144 #endif /* art_Framework_Services_Registry_GlobalSignal_h */
145 
146 // Local Variables:
147 // mode: c++
148 // 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:35
HLT enums.
vec_iX clear()