Skip to content
This repository was archived by the owner on Aug 19, 2021. It is now read-only.

Commit f66a5f3

Browse files
committed
Added convenience event functions to the EventQueue class
Before: Event<> e = Event<>(&queue, func); After: Event<> e = queue.event(func); Unfortunately, this technique only works for full bindings since the event class does not recognize the actual parameter types for functions. As such, the queue.event function can only return "Event<>".
1 parent 7223e12 commit f66a5f3

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

Event.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Event<> {
4141
* callback acts as the target for the event and is executed in the
4242
* context of the event queue's dispatch loop once posted.
4343
*
44+
* @param q Event queue to dispatch on
4445
* @param f Function to execute when the event is dispatched
4546
* @param a0..a4 Arguments to pass to the callback
4647
*/
@@ -1181,6 +1182,36 @@ class Event {
11811182
};
11821183

11831184

1185+
template <typename F>
1186+
Event<> EventQueue::event(F f) {
1187+
return Event<>(this, f);
11841188
}
11851189

1186-
#endif
1190+
template <typename F, typename A0>
1191+
Event<> EventQueue::event(F f, A0 a0) {
1192+
return Event<>(this, f, a0);
1193+
}
1194+
1195+
template <typename F, typename A0, typename A1>
1196+
Event<> EventQueue::event(F f, A0 a0, A1 a1) {
1197+
return Event<>(this, f, a0, a1);
1198+
}
1199+
1200+
template <typename F, typename A0, typename A1, typename A2>
1201+
Event<> EventQueue::event(F f, A0 a0, A1 a1, A2 a2) {
1202+
return Event<>(this, f, a0, a1, a2);
1203+
}
1204+
1205+
template <typename F, typename A0, typename A1, typename A2, typename A3>
1206+
Event<> EventQueue::event(F f, A0 a0, A1 a1, A2 a2, A3 a3) {
1207+
return Event<>(this, f, a0, a1, a2, a3);
1208+
}
1209+
1210+
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
1211+
Event<> EventQueue::event(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
1212+
return Event<>(this, f, a0, a1, a2, a3, a4);
1213+
}
1214+
1215+
}
1216+
1217+
#endif

EventQueue.h

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ namespace events {
3636
*/
3737
#define EVENTS_QUEUE_SIZE (32*EVENTS_EVENT_SIZE)
3838

39+
// Predeclared classes
40+
template <typename A0, typename A1, typename A2, typename A3, typename A4>
41+
class Event;
42+
3943

4044
/** EventQueue
4145
*
@@ -314,6 +318,40 @@ class EventQueue {
314318
return call_every(ms, context50<F, A0, A1, A2, A3, A4>(f, a0, a1, a2, a3, a4));
315319
}
316320

321+
/** Event creation
322+
*
323+
* Constructs an event bound to the specified event queue. The specified
324+
* callback acts as the target for the event and is executed in the
325+
* context of the event queue's dispatch loop once posted.
326+
*
327+
* @param f Function to execute when the event is dispatched
328+
* @param a0..a4 Arguments to pass to the callback
329+
* @return Event that will dispatch on the specific queue
330+
*/
331+
template <typename F>
332+
Event<void, void, void, void, void>
333+
event(F f);
334+
335+
template <typename F, typename A0>
336+
Event<void, void, void, void, void>
337+
event(F f, A0 a0);
338+
339+
template <typename F, typename A0, typename A1>
340+
Event<void, void, void, void, void>
341+
event(F f, A0 a0, A1 a1);
342+
343+
template <typename F, typename A0, typename A1, typename A2>
344+
Event<void, void, void, void, void>
345+
event(F f, A0 a0, A1 a1, A2 a2);
346+
347+
template <typename F, typename A0, typename A1, typename A2, typename A3>
348+
Event<void, void, void, void, void>
349+
event(F f, A0 a0, A1 a1, A2 a2, A3 a3);
350+
351+
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
352+
Event<void, void, void, void, void>
353+
event(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4);
354+
317355
protected:
318356
template <typename A0, typename A1, typename A2, typename A3, typename A4>
319357
friend class Event;
@@ -717,7 +755,16 @@ class EventQueue {
717755
};
718756
};
719757

758+
}
759+
760+
761+
// Include event class here to workaround cyclic dependencies
762+
// between Event and EventQueue
763+
//#include "Event.h"
764+
765+
766+
namespace events {
720767

721768
}
722769

723-
#endif
770+
#endif

TESTS/events/queue/main.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,28 @@ void count5(unsigned a0, unsigned a1, unsigned a2, unsigned a3, unsigned a5) {
150150
counter += a0 + a1 + a2 + a3 + a5;
151151
}
152152

153+
void count4(unsigned a0, unsigned a1, unsigned a2, unsigned a3) {
154+
counter += a0 + a1 + a2 + a3;
155+
}
156+
157+
void count3(unsigned a0, unsigned a1, unsigned a2) {
158+
counter += a0 + a1 + a2;
159+
}
160+
161+
void count2(unsigned a0, unsigned a1) {
162+
counter += a0 + a1;
163+
}
164+
165+
void count1(unsigned a0) {
166+
counter += a0;
167+
}
168+
169+
void count0() {
170+
counter += 0;
171+
}
172+
153173
void event_class_test() {
174+
counter = 0;
154175
EventQueue queue(2048);
155176

156177
Event<int, int, int, int, int> e5(&queue, count5);
@@ -172,6 +193,29 @@ void event_class_test() {
172193
TEST_ASSERT_EQUAL(counter, 30);
173194
}
174195

196+
void event_class_helper_test() {
197+
counter = 0;
198+
EventQueue queue(2048);
199+
200+
Event<> e5 = queue.event(count5, 1, 1, 1, 1, 1);
201+
Event<> e4 = queue.event(count4, 1, 1, 1, 1);
202+
Event<> e3 = queue.event(count3, 1, 1, 1);
203+
Event<> e2 = queue.event(count2, 1, 1);
204+
Event<> e1 = queue.event(count1, 1);
205+
Event<> e0 = queue.event(count0);
206+
207+
e5.post();
208+
e4.post();
209+
e3.post();
210+
e2.post();
211+
e1.post();
212+
e0.post();
213+
214+
queue.dispatch(0);
215+
216+
TEST_ASSERT_EQUAL(counter, 15);
217+
}
218+
175219

176220
// Test setup
177221
utest::v1::status_t test_setup(const size_t number_of_cases) {
@@ -195,6 +239,7 @@ const Case cases[] = {
195239

196240
Case("Testing event cancel 1", cancel_test1<20>),
197241
Case("Testing the event class", event_class_test),
242+
Case("Testing the event class helpers", event_class_helper_test),
198243
};
199244

200245
Specification specification(test_setup, cases);

0 commit comments

Comments
 (0)