Skip to content

Commit e341562

Browse files
authored
Migrate to C++11 variadic templates (#93)
Signed-off-by: Christopher Wecht <cwecht@mailbox.org>
1 parent 4c9392a commit e341562

10 files changed

Lines changed: 337 additions & 1132 deletions

‎include/message_filters/signal9.hpp‎

Lines changed: 35 additions & 276 deletions
Original file line numberDiff line numberDiff line change
@@ -43,349 +43,108 @@
4343
namespace message_filters
4444
{
4545

46-
template<typename M0, typename M1, typename M2, typename M3, typename M4, typename M5,
47-
typename M6, typename M7, typename M8>
46+
template<typename ... Ms>
4847
class CallbackHelper9
4948
{
5049
public:
51-
typedef MessageEvent<M0 const> M0Event;
52-
typedef MessageEvent<M1 const> M1Event;
53-
typedef MessageEvent<M2 const> M2Event;
54-
typedef MessageEvent<M3 const> M3Event;
55-
typedef MessageEvent<M4 const> M4Event;
56-
typedef MessageEvent<M5 const> M5Event;
57-
typedef MessageEvent<M6 const> M6Event;
58-
typedef MessageEvent<M7 const> M7Event;
59-
typedef MessageEvent<M8 const> M8Event;
60-
6150
virtual ~CallbackHelper9() {}
6251

63-
virtual void call(
64-
bool nonconst_force_copy, const M0Event & e0, const M1Event & e1,
65-
const M2Event & e2, const M3Event & e3, const M4Event & e4, const M5Event & e5,
66-
const M6Event & e6, const M7Event & e7, const M8Event & e8) = 0;
52+
virtual void call(bool nonconst_force_copy, const MessageEvent<Ms const> & ... es) = 0;
6753

6854
typedef std::shared_ptr<CallbackHelper9> Ptr;
6955
};
7056

71-
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5,
72-
typename P6, typename P7, typename P8>
57+
template<typename ... Ps>
7358
class CallbackHelper9T
74-
: public CallbackHelper9<typename ParameterAdapter<P0>::Message,
75-
typename ParameterAdapter<P1>::Message,
76-
typename ParameterAdapter<P2>::Message,
77-
typename ParameterAdapter<P3>::Message,
78-
typename ParameterAdapter<P4>::Message,
79-
typename ParameterAdapter<P5>::Message,
80-
typename ParameterAdapter<P6>::Message,
81-
typename ParameterAdapter<P7>::Message,
82-
typename ParameterAdapter<P8>::Message>
59+
: public CallbackHelper9<typename ParameterAdapter<Ps>::Message...>
8360
{
84-
private:
85-
typedef ParameterAdapter<P0> A0;
86-
typedef ParameterAdapter<P1> A1;
87-
typedef ParameterAdapter<P2> A2;
88-
typedef ParameterAdapter<P3> A3;
89-
typedef ParameterAdapter<P4> A4;
90-
typedef ParameterAdapter<P5> A5;
91-
typedef ParameterAdapter<P6> A6;
92-
typedef ParameterAdapter<P7> A7;
93-
typedef ParameterAdapter<P8> A8;
94-
typedef typename A0::Event M0Event;
95-
typedef typename A1::Event M1Event;
96-
typedef typename A2::Event M2Event;
97-
typedef typename A3::Event M3Event;
98-
typedef typename A4::Event M4Event;
99-
typedef typename A5::Event M5Event;
100-
typedef typename A6::Event M6Event;
101-
typedef typename A7::Event M7Event;
102-
typedef typename A8::Event M8Event;
103-
10461
public:
105-
typedef std::function<void (typename A0::Parameter, typename A1::Parameter,
106-
typename A2::Parameter, typename A3::Parameter, typename A4::Parameter,
107-
typename A5::Parameter, typename A6::Parameter, typename A7::Parameter,
108-
typename A8::Parameter)> Callback;
62+
typedef std::function<void (typename ParameterAdapter<Ps>::Parameter...)> Callback;
10963

11064
CallbackHelper9T(const Callback & cb) // NOLINT(runtime/explicit)
11165
: callback_(cb)
11266
{
11367
}
11468

115-
virtual void call(
116-
bool nonconst_force_copy, const M0Event & e0, const M1Event & e1,
117-
const M2Event & e2, const M3Event & e3, const M4Event & e4, const M5Event & e5,
118-
const M6Event & e6, const M7Event & e7, const M8Event & e8)
69+
virtual void call(bool nonconst_force_copy, const typename ParameterAdapter<Ps>::Event &... es)
11970
{
120-
M0Event my_e0(e0, nonconst_force_copy || e0.nonConstWillCopy());
121-
M1Event my_e1(e1, nonconst_force_copy || e0.nonConstWillCopy());
122-
M2Event my_e2(e2, nonconst_force_copy || e0.nonConstWillCopy());
123-
M3Event my_e3(e3, nonconst_force_copy || e0.nonConstWillCopy());
124-
M4Event my_e4(e4, nonconst_force_copy || e0.nonConstWillCopy());
125-
M5Event my_e5(e5, nonconst_force_copy || e0.nonConstWillCopy());
126-
M6Event my_e6(e6, nonconst_force_copy || e0.nonConstWillCopy());
127-
M7Event my_e7(e7, nonconst_force_copy || e0.nonConstWillCopy());
128-
M8Event my_e8(e8, nonconst_force_copy || e0.nonConstWillCopy());
129-
callback_(
130-
A0::getParameter(e0),
131-
A1::getParameter(e1),
132-
A2::getParameter(e2),
133-
A3::getParameter(e3),
134-
A4::getParameter(e4),
135-
A5::getParameter(e5),
136-
A6::getParameter(e6),
137-
A7::getParameter(e7),
138-
A8::getParameter(e8));
71+
auto my_es{std::make_tuple(
72+
typename ParameterAdapter<Ps>::Event(
73+
es,
74+
nonconst_force_copy || es.nonConstWillCopy())...)};
75+
callback_(ParameterAdapter<Ps>::getParameter(es)...);
13976
}
14077

14178
private:
14279
Callback callback_;
14380
};
14481

145-
template<typename M0, typename M1, typename M2, typename M3, typename M4, typename M5,
146-
typename M6, typename M7, typename M8>
82+
template<typename ... Ms>
14783
class Signal9
14884
{
149-
typedef std::shared_ptr<CallbackHelper9<M0, M1, M2, M3, M4, M5, M6, M7, M8>> CallbackHelper9Ptr;
85+
typedef std::shared_ptr<CallbackHelper9<Ms...>> CallbackHelper9Ptr;
15086
typedef std::vector<CallbackHelper9Ptr> V_CallbackHelper9;
15187

15288
public:
153-
typedef MessageEvent<M0 const> M0Event;
154-
typedef MessageEvent<M1 const> M1Event;
155-
typedef MessageEvent<M2 const> M2Event;
156-
typedef MessageEvent<M3 const> M3Event;
157-
typedef MessageEvent<M4 const> M4Event;
158-
typedef MessageEvent<M5 const> M5Event;
159-
typedef MessageEvent<M6 const> M6Event;
160-
typedef MessageEvent<M7 const> M7Event;
161-
typedef MessageEvent<M8 const> M8Event;
162-
typedef std::shared_ptr<M0 const> M0ConstPtr;
163-
typedef std::shared_ptr<M1 const> M1ConstPtr;
164-
typedef std::shared_ptr<M2 const> M2ConstPtr;
165-
typedef std::shared_ptr<M3 const> M3ConstPtr;
166-
typedef std::shared_ptr<M4 const> M4ConstPtr;
167-
typedef std::shared_ptr<M5 const> M5ConstPtr;
168-
typedef std::shared_ptr<M6 const> M6ConstPtr;
169-
typedef std::shared_ptr<M7 const> M7ConstPtr;
170-
typedef std::shared_ptr<M8 const> M8ConstPtr;
17189
typedef const std::shared_ptr<NullType const> & NullP;
17290

173-
174-
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5,
175-
typename P6, typename P7, typename P8>
176-
Connection addCallback(const std::function<void(P0, P1, P2, P3, P4, P5, P6, P7, P8)> & callback)
91+
template<typename ... Ps>
92+
Connection addCallback(const std::function<void(Ps...)> & callback)
17793
{
178-
CallbackHelper9T<P0, P1, P2, P3, P4, P5, P6, P7, P8> * helper =
179-
new CallbackHelper9T<P0, P1, P2, P3, P4, P5, P6, P7, P8>(callback);
94+
CallbackHelper9T<Ps...> * helper = new CallbackHelper9T<Ps...>(callback);
18095

18196
std::lock_guard<std::mutex> lock(mutex_);
18297
callbacks_.push_back(CallbackHelper9Ptr(helper));
18398
return Connection(std::bind(&Signal9::removeCallback, this, callbacks_.back()));
18499
}
185100

186-
template<typename P0, typename P1>
187-
Connection addCallback(void (* callback)(P0, P1))
188-
{
189-
return addCallback(
190-
std::function<void(P0, P1, NullP, NullP, NullP, NullP, NullP, NullP,
191-
NullP)>(
192-
std::bind(
193-
callback, std::placeholders::_1, std::placeholders::_2)));
194-
}
195-
196-
template<typename P0, typename P1, typename P2>
197-
Connection addCallback(void (* callback)(P0, P1, P2))
101+
template<typename ... Ps>
102+
Connection addCallback(void (* callback)(Ps...))
198103
{
199104
return addCallback(
200-
std::function<void(P0, P1, P2, NullP, NullP, NullP, NullP, NullP,
201-
NullP)>(
202-
std::bind(
203-
callback, std::placeholders::_1, std::placeholders::_2,
204-
std::placeholders::_3)));
105+
std::function<void(Ps...)>(
106+
[callback](auto &&... args) {
107+
callback(args ...);
108+
}));
205109
}
206110

207-
template<typename P0, typename P1, typename P2, typename P3>
208-
Connection addCallback(void (* callback)(P0, P1, P2, P3))
111+
template<typename T, typename ... Ps>
112+
Connection addCallback(void (T::* callback)(Ps...), T * t)
209113
{
210114
return addCallback(
211-
std::function<void(P0, P1, P2, P3, NullP, NullP, NullP, NullP, NullP)>(
212-
std::bind(
213-
callback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
214-
std::placeholders::_4)));
215-
}
216-
217-
template<typename P0, typename P1, typename P2, typename P3, typename P4>
218-
Connection addCallback(void (* callback)(P0, P1, P2, P3, P4))
219-
{
220-
return addCallback(
221-
std::function<void(P0, P1, P2, P3, P4, NullP, NullP, NullP, NullP)>(
222-
std::bind(
223-
callback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
224-
std::placeholders::_4, std::placeholders::_5)));
225-
}
226-
227-
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
228-
Connection addCallback(void (* callback)(P0, P1, P2, P3, P4, P5))
229-
{
230-
return addCallback(
231-
std::function<void(P0, P1, P2, P3, P4, P5, NullP, NullP, NullP)>(
232-
std::bind(
233-
callback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
234-
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6)));
235-
}
236-
237-
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5,
238-
typename P6>
239-
Connection addCallback(void (* callback)(P0, P1, P2, P3, P4, P5, P6))
240-
{
241-
return addCallback(
242-
std::function<void(P0, P1, P2, P3, P4, P5, P6, NullP, NullP)>(
243-
std::bind(
244-
callback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
245-
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6,
246-
std::placeholders::_7)));
247-
}
248-
249-
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5,
250-
typename P6, typename P7>
251-
Connection addCallback(void (* callback)(P0, P1, P2, P3, P4, P5, P6, P7))
252-
{
253-
return addCallback(
254-
std::function<void(P0, P1, P2, P3, P4, P5, P6, P7, NullP)>(
255-
std::bind(
256-
callback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
257-
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6,
258-
std::placeholders::_7, std::placeholders::_8)));
259-
}
260-
261-
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5,
262-
typename P6, typename P7, typename P8>
263-
Connection addCallback(void (* callback)(P0, P1, P2, P3, P4, P5, P6, P7, P8))
264-
{
265-
return addCallback(
266-
std::function<void(P0, P1, P2, P3, P4, P5, P6, P7, P8)>(
267-
std::bind(
268-
callback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
269-
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6,
270-
std::placeholders::_7, std::placeholders::_8, std::placeholders::_9)));
271-
}
272-
273-
template<typename T, typename P0, typename P1>
274-
Connection addCallback(void (T::* callback)(P0, P1), T * t)
275-
{
276-
return addCallback(
277-
std::function<void(P0, P1, NullP, NullP, NullP, NullP, NullP, NullP, NullP)>(
278-
std::bind(
279-
callback, t, std::placeholders::_1, std::placeholders::_2)));
280-
}
281-
282-
template<typename T, typename P0, typename P1, typename P2>
283-
Connection addCallback(void (T::* callback)(P0, P1, P2), T * t)
284-
{
285-
return addCallback(
286-
std::function<void(P0, P1, P2, NullP, NullP, NullP, NullP, NullP, NullP)>(
287-
std::bind(
288-
callback, t, std::placeholders::_1, std::placeholders::_2,
289-
std::placeholders::_3)));
290-
}
291-
292-
template<typename T, typename P0, typename P1, typename P2, typename P3>
293-
Connection addCallback(void (T::* callback)(P0, P1, P2, P3), T * t)
294-
{
295-
return addCallback(
296-
std::function<void(P0, P1, P2, P3, NullP, NullP, NullP, NullP, NullP)>(
297-
std::bind(
298-
callback, t, std::placeholders::_1, std::placeholders::_2,
299-
std::placeholders::_3, std::placeholders::_4)));
300-
}
301-
302-
template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
303-
Connection addCallback(void (T::* callback)(P0, P1, P2, P3, P4), T * t)
304-
{
305-
return addCallback(
306-
std::function<void(P0, P1, P2, P3, P4, NullP, NullP, NullP, NullP)>(
307-
std::bind(
308-
callback, t, std::placeholders::_1, std::placeholders::_2,
309-
std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)));
310-
}
311-
312-
template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4,
313-
typename P5>
314-
Connection addCallback(void (T::* callback)(P0, P1, P2, P3, P4, P5), T * t)
315-
{
316-
return addCallback(
317-
std::function<void(P0, P1, P2, P3, P4, P5, NullP, NullP, NullP)>(
318-
std::bind(
319-
callback, t, std::placeholders::_1, std::placeholders::_2,
320-
std::placeholders::_3, std::placeholders::_4, std::placeholders::_5,
321-
std::placeholders::_6)));
322-
}
323-
324-
template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4,
325-
typename P5, typename P6>
326-
Connection addCallback(void (T::* callback)(P0, P1, P2, P3, P4, P5, P6), T * t)
327-
{
328-
return addCallback(
329-
std::function<void(P0, P1, P2, P3, P4, P5, P6, NullP, NullP)>(
330-
std::bind(
331-
callback, t, std::placeholders::_1, std::placeholders::_2,
332-
std::placeholders::_3, std::placeholders::_4, std::placeholders::_5,
333-
std::placeholders::_6, std::placeholders::_7)));
334-
}
335-
336-
template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4,
337-
typename P5, typename P6, typename P7>
338-
Connection addCallback(void (T::* callback)(P0, P1, P2, P3, P4, P5, P6, P7), T * t)
339-
{
340-
return addCallback(
341-
std::function<void(P0, P1, P2, P3, P4, P5, P6, P7, NullP)>(
342-
std::bind(
343-
callback, t, std::placeholders::_1, std::placeholders::_2,
344-
std::placeholders::_3, std::placeholders::_4, std::placeholders::_5,
345-
std::placeholders::_6, std::placeholders::_7, std::placeholders::_8)));
115+
std::function<void(Ps...)>(
116+
[ = ](const Ps &... ps) {
117+
(t->*callback)(ps ...);
118+
}));
346119
}
347120

348121
template<typename C>
349122
Connection addCallback(C & callback)
350123
{
351-
return addCallback<const M0ConstPtr &,
352-
const M1ConstPtr &,
353-
const M2ConstPtr &,
354-
const M3ConstPtr &,
355-
const M4ConstPtr &,
356-
const M5ConstPtr &,
357-
const M6ConstPtr &,
358-
const M7ConstPtr &,
359-
const M8ConstPtr &>(
360-
std::bind(
361-
callback, std::placeholders::_1,
362-
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4,
363-
std::placeholders::_5, std::placeholders::_6, std::placeholders::_7,
364-
std::placeholders::_8, std::placeholders::_9));
124+
return addCallback(
125+
std::function<void(const std::shared_ptr<const Ms> & ...)>(
126+
[callback](const std::shared_ptr<const Ms> & ... msgs) {callback(msgs ...);}));
365127
}
366128

367129
void removeCallback(const CallbackHelper9Ptr & helper)
368130
{
369131
std::lock_guard<std::mutex> lock(mutex_);
370-
typename V_CallbackHelper9::iterator it = std::find(
371-
callbacks_.begin(), callbacks_.end(), helper);
132+
typename V_CallbackHelper9::iterator it =
133+
std::find(callbacks_.begin(), callbacks_.end(), helper);
372134
if (it != callbacks_.end()) {
373135
callbacks_.erase(it);
374136
}
375137
}
376138

377-
void call(
378-
const M0Event & e0, const M1Event & e1, const M2Event & e2, const M3Event & e3,
379-
const M4Event & e4, const M5Event & e5, const M6Event & e6, const M7Event & e7,
380-
const M8Event & e8)
139+
void call(const MessageEvent<Ms const> & ... es)
381140
{
382141
std::lock_guard<std::mutex> lock(mutex_);
383142
bool nonconst_force_copy = callbacks_.size() > 1;
384143
typename V_CallbackHelper9::iterator it = callbacks_.begin();
385144
typename V_CallbackHelper9::iterator end = callbacks_.end();
386145
for (; it != end; ++it) {
387146
const CallbackHelper9Ptr & helper = *it;
388-
helper->call(nonconst_force_copy, e0, e1, e2, e3, e4, e5, e6, e7, e8);
147+
helper->call(nonconst_force_copy, es ...);
389148
}
390149
}
391150

0 commit comments

Comments
 (0)