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

Commit

Permalink
Added convenience event functions to the EventQueue class
Browse files Browse the repository at this point in the history
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<>".
  • Loading branch information
geky committed Sep 23, 2016
1 parent 7223e12 commit f66a5f3
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
33 changes: 32 additions & 1 deletion Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Event<> {
* callback acts as the target for the event and is executed in the
* context of the event queue's dispatch loop once posted.
*
* @param q Event queue to dispatch on
* @param f Function to execute when the event is dispatched
* @param a0..a4 Arguments to pass to the callback
*/
Expand Down Expand Up @@ -1181,6 +1182,36 @@ class Event {
};


template <typename F>
Event<> EventQueue::event(F f) {
return Event<>(this, f);
}

#endif
template <typename F, typename A0>
Event<> EventQueue::event(F f, A0 a0) {
return Event<>(this, f, a0);
}

template <typename F, typename A0, typename A1>
Event<> EventQueue::event(F f, A0 a0, A1 a1) {
return Event<>(this, f, a0, a1);
}

template <typename F, typename A0, typename A1, typename A2>
Event<> EventQueue::event(F f, A0 a0, A1 a1, A2 a2) {
return Event<>(this, f, a0, a1, a2);
}

template <typename F, typename A0, typename A1, typename A2, typename A3>
Event<> EventQueue::event(F f, A0 a0, A1 a1, A2 a2, A3 a3) {
return Event<>(this, f, a0, a1, a2, a3);
}

template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
Event<> EventQueue::event(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
return Event<>(this, f, a0, a1, a2, a3, a4);
}

}

#endif
49 changes: 48 additions & 1 deletion EventQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ namespace events {
*/
#define EVENTS_QUEUE_SIZE (32*EVENTS_EVENT_SIZE)

// Predeclared classes
template <typename A0, typename A1, typename A2, typename A3, typename A4>
class Event;


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

/** Event creation
*
* Constructs an event bound to the specified event queue. The specified
* callback acts as the target for the event and is executed in the
* context of the event queue's dispatch loop once posted.
*
* @param f Function to execute when the event is dispatched
* @param a0..a4 Arguments to pass to the callback
* @return Event that will dispatch on the specific queue
*/
template <typename F>
Event<void, void, void, void, void>
event(F f);

template <typename F, typename A0>
Event<void, void, void, void, void>
event(F f, A0 a0);

template <typename F, typename A0, typename A1>
Event<void, void, void, void, void>
event(F f, A0 a0, A1 a1);

template <typename F, typename A0, typename A1, typename A2>
Event<void, void, void, void, void>
event(F f, A0 a0, A1 a1, A2 a2);

template <typename F, typename A0, typename A1, typename A2, typename A3>
Event<void, void, void, void, void>
event(F f, A0 a0, A1 a1, A2 a2, A3 a3);

template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
Event<void, void, void, void, void>
event(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4);

protected:
template <typename A0, typename A1, typename A2, typename A3, typename A4>
friend class Event;
Expand Down Expand Up @@ -717,7 +755,16 @@ class EventQueue {
};
};

}


// Include event class here to workaround cyclic dependencies
// between Event and EventQueue
//#include "Event.h"


namespace events {

}

#endif
#endif
45 changes: 45 additions & 0 deletions TESTS/events/queue/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,28 @@ void count5(unsigned a0, unsigned a1, unsigned a2, unsigned a3, unsigned a5) {
counter += a0 + a1 + a2 + a3 + a5;
}

void count4(unsigned a0, unsigned a1, unsigned a2, unsigned a3) {
counter += a0 + a1 + a2 + a3;
}

void count3(unsigned a0, unsigned a1, unsigned a2) {
counter += a0 + a1 + a2;
}

void count2(unsigned a0, unsigned a1) {
counter += a0 + a1;
}

void count1(unsigned a0) {
counter += a0;
}

void count0() {
counter += 0;
}

void event_class_test() {
counter = 0;
EventQueue queue(2048);

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

void event_class_helper_test() {
counter = 0;
EventQueue queue(2048);

Event<> e5 = queue.event(count5, 1, 1, 1, 1, 1);
Event<> e4 = queue.event(count4, 1, 1, 1, 1);
Event<> e3 = queue.event(count3, 1, 1, 1);
Event<> e2 = queue.event(count2, 1, 1);
Event<> e1 = queue.event(count1, 1);
Event<> e0 = queue.event(count0);

e5.post();
e4.post();
e3.post();
e2.post();
e1.post();
e0.post();

queue.dispatch(0);

TEST_ASSERT_EQUAL(counter, 15);
}


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

Case("Testing event cancel 1", cancel_test1<20>),
Case("Testing the event class", event_class_test),
Case("Testing the event class helpers", event_class_helper_test),
};

Specification specification(test_setup, cases);
Expand Down

0 comments on commit f66a5f3

Please sign in to comment.