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

Commit

Permalink
Moved delay/period to first argument of post_in/post_every
Browse files Browse the repository at this point in the history
matches previous change in c api

per suggestion from @kilogram
- makes the call clearer, especially when callback argument looks like
  a delay value
- simplifies implementation of c++11 wrappers
  • Loading branch information
geky committed Jul 8, 2016
1 parent ab956fd commit ede8401
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
44 changes: 22 additions & 22 deletions EventQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,32 @@ class EventQueue {
* or 0 on failure
*/
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
int post_in(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, int ms) {
return post_in(Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4), ms);
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
return post_in(ms, Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
}

template <typename F, typename A0, typename A1, typename A2, typename A3>
int post_in(F f, A0 a0, A1 a1, A2 a2, A3 a3, int ms) {
return post_in(Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3), ms);
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3) {
return post_in(ms, Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
}

template <typename F, typename A0, typename A1, typename A2>
int post_in(F f, A0 a0, A1 a1, A2 a2, int ms) {
return post_in(Context3<F,A0,A1,A2>(f,a0,a1,a2), ms);
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2) {
return post_in(ms, Context3<F,A0,A1,A2>(f,a0,a1,a2));
}

template <typename F, typename A0, typename A1>
int post_in(F f, A0 a0, A1 a1, int ms) {
return post_in(Context2<F,A0,A1>(f,a0,a1), ms);
int post_in(int ms, F f, A0 a0, A1 a1) {
return post_in(ms, Context2<F,A0,A1>(f,a0,a1));
}

template <typename F, typename A0>
int post_in(F f, A0 a0, int ms) {
return post_in(Context1<F,A0>(f,a0), ms);
int post_in(int ms, F f, A0 a0) {
return post_in(ms, Context1<F,A0>(f,a0));
}

template <typename F>
int post_in(F f, int ms) {
int post_in(int ms, F f) {
void *p = event_alloc(&_equeue, sizeof(F));
if (!p) {
return 0;
Expand All @@ -173,32 +173,32 @@ class EventQueue {
* or 0 on failure
*/
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
int post_every(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, int ms) {
return post_every(Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4), ms);
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
return post_every(ms, Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
}

template <typename F, typename A0, typename A1, typename A2, typename A3>
int post_every(F f, A0 a0, A1 a1, A2 a2, A3 a3, int ms) {
return post_every(Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3), ms);
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3) {
return post_every(ms, Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
}

template <typename F, typename A0, typename A1, typename A2>
int post_every(F f, A0 a0, A1 a1, A2 a2, int ms) {
return post_every(Context3<F,A0,A1,A2>(f,a0,a1,a2), ms);
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2) {
return post_every(ms, Context3<F,A0,A1,A2>(f,a0,a1,a2));
}

template <typename F, typename A0, typename A1>
int post_every(F f, A0 a0, A1 a1, int ms) {
return post_every(Context2<F,A0,A1>(f,a0,a1), ms);
int post_every(int ms, F f, A0 a0, A1 a1) {
return post_every(ms, Context2<F,A0,A1>(f,a0,a1));
}

template <typename F, typename A0>
int post_every(F f, A0 a0, int ms) {
return post_every(Context1<F,A0>(f,a0), ms);
int post_every(int ms, F f, A0 a0) {
return post_every(ms, Context1<F,A0>(f,a0));
}

template <typename F>
int post_every(F f, int ms) {
int post_every(int ms, F f) {
void *p = event_alloc(&_equeue, sizeof(F));
if (!p) {
return 0;
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ int main() {

// events are simple callbacks
queue.post(printf, "called immediately\n");
queue.post_in(printf, "called in 2 seconds\n", 2000);
queue.post_every(printf, "called every 1 seconds\n", 1000);
queue.post_in(printf, 2000, "called in 2 seconds\n");
queue.post_every(printf, 1000, "called every 1 seconds\n");

// executed by the dispatch method
queue.dispatch();
Expand Down Expand Up @@ -68,13 +68,13 @@ queue.post(Callback<void(char)>(&serial, &Serial::printf), "hello\n");

// The post_in function registers events to be called after a delay
// specified in milliseconds
queue.post_in(doit_in_two_seconds, 2000);
queue.post_in(printf, "called in 0.3 seconds\n", 300);
queue.post_in(2000, doit_in_two_seconds);
queue.post_in(300, printf, "called in 0.3 seconds\n");

// The post_every function registers events to be called repeatedly
// with a period specified in milliseconds
queue.post_every(doit_every_two_seconds, 2000);
queue.post_every(printf, "called every 0.4 seconds\n", 400);
queue.post_every(2000, doit_every_two_seconds);
queue.post_every(400, printf, "called every 0.4 seconds\n");
```

All post calls return an integer id that uniquely represents the event
Expand All @@ -83,7 +83,7 @@ there is no memory or the queue's event size is exceeded.

``` cpp
// The event id is uniqueue to the queue
int id = queue.post_in(printf, "will this work?\n", 100);
int id = queue.post_in(100, printf, "will this work?\n");

// An id of 0 indicates an error
if (id) {
Expand Down
14 changes: 7 additions & 7 deletions TESTS/events/queue/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ void simple_posts_test##i() { \
TEST_ASSERT(touched); \
\
touched = false; \
queue.post_in(func##i,##__VA_ARGS__, 1); \
queue.post_in(1, func##i,##__VA_ARGS__); \
queue.dispatch(2); \
TEST_ASSERT(touched); \
\
touched = false; \
queue.post_every(func##i,##__VA_ARGS__, 1); \
queue.post_every(1, func##i,##__VA_ARGS__); \
queue.dispatch(2); \
TEST_ASSERT(touched); \
}
Expand All @@ -82,7 +82,7 @@ void post_in_test() {

for (int i = 0; i < N; i++) {
tickers[i].start();
queue.post_in(time_func, &tickers[i], (i+1)*100, (i+1)*100);
queue.post_in((i+1)*100, time_func, &tickers[i], (i+1)*100);
}

queue.dispatch(N*100);
Expand All @@ -96,7 +96,7 @@ void post_every_test() {

for (int i = 0; i < N; i++) {
tickers[i].start();
queue.post_every(time_func, &tickers[i], (i+1)*100, (i+1)*100);
queue.post_every((i+1)*100, time_func, &tickers[i], (i+1)*100);
}

queue.dispatch(N*100);
Expand Down Expand Up @@ -127,7 +127,7 @@ void event_loop_test2() {

for (int i = 0; i < N; i++) {
tickers[i].start();
loop.post_every(time_func, &tickers[i], (i+1)*100, (i+1)*100);
loop.post_every((i+1)*100, time_func, &tickers[i], (i+1)*100);
Thread::yield();
wait_ms(75);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ void cancel_test1() {
int ids[N];

for (int i = 0; i < N; i++) {
ids[i] = queue.post_in(no, 1000);
ids[i] = queue.post_in(1000, no);
}

for (int i = N-1; i >= 0; i--) {
Expand All @@ -186,7 +186,7 @@ void cancel_test2() {
int ids[N];

for (int i = 0; i < N; i++) {
ids[i] = loop.post_in(no, 1000);
ids[i] = loop.post_in(1000, no);
}

for (int i = N-1; i >= 0; i--) {
Expand Down

0 comments on commit ede8401

Please sign in to comment.