LocalSignal.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_Registry_LocalSignal_h
2 #define art_Framework_Services_Registry_LocalSignal_h
3 
4 ////////////////////////////////////////////////////////////////////////
5 // LocalSignal.h
6 //
7 // Define a wrapper for local 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 // Multi-threaded behavior:
12 //
13 // watch...() access should be sequential only by construction since
14 // watch() calls are made from service constructors. Other calling
15 // origins are not supported.
16 //
17 // invoke...() access should be only from within the correct schedule's
18 // context so lock-free access to the correct signal is automatic and
19 // safe.
20 ////////////////////////////////////////////////////////////////////////
25 
26 #include <deque>
27 #include <functional>
28 
29 namespace art {
30  template <detail::SignalResponseType, typename ResultType, typename... Args>
31  class LocalSignal;
32 
33  template <detail::SignalResponseType STYPE,
34  typename ResultType,
35  typename... Args>
36  class LocalSignal<STYPE, ResultType(Args...)> {
37  public:
38  // Typedefs
39  using slot_type = std::function<ResultType(Args...)>;
40  using result_type = ResultType;
41 
42  private:
43  // Required for derivative alias below.
44  using ContainerType_ = std::vector<std::deque<slot_type>>;
45 
46  public:
48 
49  // Constructor.
50  LocalSignal(size_t nSchedules);
51 
52  // 1. Free function or functor (or pre-bound member function).
53  void watch(ScheduleID sID, std::function<ResultType(Args...)> slot);
54  // 2. Non-const member function.
55  template <typename T>
56  void watch(ScheduleID sID, ResultType (T::*slot)(Args...), T& t);
57  // 3. Const member function.
58  template <typename T>
59  void watch(ScheduleID sID,
60  ResultType (T::*slot)(Args...) const,
61  T const& t);
62 
63  // 1. Free function or functor (or pre-bound member function).
64  void watchAll(std::function<ResultType(Args...)> slot);
65  // 2. Non-const member function.
66  template <typename T>
67  void watchAll(ResultType (T::*slot)(Args...), T& t);
68  // 3. Const member function.
69  template <typename T>
70  void watchAll(ResultType (T::*slot)(Args...) const, T const& t);
71 
72  void invoke(ScheduleID sID, Args&&... args) const; // Discard ResultType.
73 
74  void clear(ScheduleID sID);
75  void clearAll();
76 
77  private:
79  };
80 
81  template <detail::SignalResponseType STYPE,
82  typename ResultType,
83  typename... Args>
84  LocalSignal<STYPE, ResultType(Args...)>::LocalSignal(size_t nSchedules)
85  : signals_(nSchedules)
86  {}
87 
88  // 1.
89  template <detail::SignalResponseType STYPE,
90  typename ResultType,
91  typename... Args>
92  void
93  LocalSignal<STYPE, ResultType(Args...)>::watch(
94  ScheduleID sID,
95  std::function<ResultType(Args...)> slot)
96  {
97  detail::connect_to_signal<STYPE>(signals_.at(sID.id()), slot);
98  }
99 
100  // 2.
101  template <detail::SignalResponseType STYPE,
102  typename ResultType,
103  typename... Args>
104  template <typename T>
105  void
106  LocalSignal<STYPE, ResultType(Args...)>::watch(ScheduleID sID,
107  ResultType (T::*slot)(Args...),
108  T& t)
109  {
110  watch(sID, detail::makeWatchFunc(slot, t));
111  }
112 
113  // 3.
114  template <detail::SignalResponseType STYPE,
115  typename ResultType,
116  typename... Args>
117  template <typename T>
118  void
119  LocalSignal<STYPE, ResultType(Args...)>::watch(ScheduleID sID,
120  ResultType (T::*slot)(Args...)
121  const,
122  T const& t)
123  {
124  watch(sID, detail::makeWatchFunc(slot, t));
125  }
126 
127  // 1.
128  template <detail::SignalResponseType STYPE,
129  typename ResultType,
130  typename... Args>
131  void
132  LocalSignal<STYPE, ResultType(Args...)>::watchAll(
133  std::function<ResultType(Args...)> slot)
134  {
135  for (auto& signal : signals_) {
136  detail::connect_to_signal<STYPE>(signal, slot);
137  }
138  }
139 
140  // 2.
141  template <detail::SignalResponseType STYPE,
142  typename ResultType,
143  typename... Args>
144  template <typename T>
145  void
146  LocalSignal<STYPE, ResultType(Args...)>::watchAll(
147  ResultType (T::*slot)(Args...),
148  T& t)
149  {
150  watchAll(detail::makeWatchFunc(slot, t));
151  }
152 
153  // 3.
154  template <detail::SignalResponseType STYPE,
155  typename ResultType,
156  typename... Args>
157  template <typename T>
158  void
159  LocalSignal<STYPE, ResultType(Args...)>::watchAll(
160  ResultType (T::*slot)(Args...) const,
161  T const& t)
162  {
163  watchAll(detail::makeWatchFunc(slot, t));
164  }
165 
166  template <detail::SignalResponseType STYPE,
167  typename ResultType,
168  typename... Args>
169  void
170  LocalSignal<STYPE, ResultType(Args...)>::invoke(ScheduleID sID,
171  Args&&... args) const
172  {
173  for (auto f : signals_.at(sID.id())) {
174  f(std::forward<Args>(args)...);
175  }
176  }
177 
178  template <detail::SignalResponseType STYPE,
179  typename ResultType,
180  typename... Args>
181  void
182  LocalSignal<STYPE, ResultType(Args...)>::clear(ScheduleID const sID)
183  {
184  signals_.at(sID.id()).clear();
185  }
186 
187  template <detail::SignalResponseType STYPE,
188  typename ResultType,
189  typename... Args>
190  void
191  LocalSignal<STYPE, ResultType(Args...)>::clearAll()
192  {
193  for (auto& signal : signals_) {
194  signal.clear();
195  }
196  }
197 
198 } // art
199 #endif /* art_Framework_Services_Registry_LocalSignal_h */
200 
201 // Local Variables:
202 // mode: c++
203 // End:
std::vector< std::deque< slot_type >> ContainerType_
Definition: LocalSignal.h:44
vector< vector< double > > clear
std::function< ResultType(Args...)> makeWatchFunc(ResultType(T::*slot)(Args...), T &t)
Definition: makeWatchFunc.h:16
constexpr id_type id() const
Definition: ScheduleID.h:70
typename ContainerType_::size_type size_type
Definition: LocalSignal.h:47
std::function< ResultType(Args...)> slot_type
Definition: LocalSignal.h:39
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:11
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