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 
4 ////////////////////////////////////////////////////////////////////////
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 //
11 ////////////////////////////////////////////////////////////////////////
12 
16 
17 #include <deque>
18 #include <functional>
19 
20 namespace art {
21  template <detail::SignalResponseType, typename ResultType, typename... Args>
22  class GlobalSignal;
23 
24  // Only supported template definition is a partial specialization on
25  // a function type--i.e. and instantiation of GlobalSignal must look
26  // like (e.g.):
27  // GlobalSignal<LIFO, void(std::string const&)> s;
28  template <detail::SignalResponseType SRTYPE,
29  typename ResultType,
30  typename... Args>
31  class GlobalSignal<SRTYPE, ResultType(Args...)> {
32  using tuple_type = std::tuple<Args...>;
33 
34  public:
35  using slot_type = std::function<ResultType(Args...)>;
36  using result_type = ResultType;
37  template <std::size_t I>
38  using slot_argument_type = std::tuple_element_t<I, tuple_type>;
39 
40  // 1. Free function or functor (or pre-bound member function).
41  void watch(std::function<ResultType(Args...)> slot);
42  // 2a. Non-const member function.
43  template <typename T>
44  void watch(ResultType (T::*slot)(Args...), T& t);
45  // 2b. Non-const member function (legacy).
46  template <typename T>
47  void watch(T* t, ResultType (T::*slot)(Args...));
48  // 3a. Const member function.
49  template <typename T>
50  void watch(ResultType (T::*slot)(Args...) const, T const& t);
51  // 3b. Const member function (legacy).
52  template <typename T>
53  void watch(T const* t, ResultType (T::*slot)(Args...) const);
54 
55  void invoke(Args const&... args) const; // Discard ResultType.
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 const&... args) const
129  {
130  for (auto f : signal_) {
131  f(args...);
132  }
133  }
134 
135 } // namespace art
136 #endif /* art_Framework_Services_Registry_GlobalSignal_h */
137 
138 // Local Variables:
139 // mode: c++
140 // End:
std::function< ResultType(Args...)> makeWatchFunc(ResultType(T::*slot)(Args...), T &t)
Definition: makeWatchFunc.h:16
std::function< ResultType(Args...)> slot_type
Definition: GlobalSignal.h:35
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
double T
Definition: Xdiff_gwt.C:5
std::tuple_element_t< I, tuple_type > slot_argument_type
Definition: GlobalSignal.h:38