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

Commit ede8401

Browse files
committed
Moved delay/period to first argument of post_in/post_every
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
1 parent ab956fd commit ede8401

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

EventQueue.h

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,32 +127,32 @@ class EventQueue {
127127
* or 0 on failure
128128
*/
129129
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
130-
int post_in(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, int ms) {
131-
return post_in(Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4), ms);
130+
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
131+
return post_in(ms, Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
132132
}
133133

134134
template <typename F, typename A0, typename A1, typename A2, typename A3>
135-
int post_in(F f, A0 a0, A1 a1, A2 a2, A3 a3, int ms) {
136-
return post_in(Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3), ms);
135+
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3) {
136+
return post_in(ms, Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
137137
}
138138

139139
template <typename F, typename A0, typename A1, typename A2>
140-
int post_in(F f, A0 a0, A1 a1, A2 a2, int ms) {
141-
return post_in(Context3<F,A0,A1,A2>(f,a0,a1,a2), ms);
140+
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2) {
141+
return post_in(ms, Context3<F,A0,A1,A2>(f,a0,a1,a2));
142142
}
143143

144144
template <typename F, typename A0, typename A1>
145-
int post_in(F f, A0 a0, A1 a1, int ms) {
146-
return post_in(Context2<F,A0,A1>(f,a0,a1), ms);
145+
int post_in(int ms, F f, A0 a0, A1 a1) {
146+
return post_in(ms, Context2<F,A0,A1>(f,a0,a1));
147147
}
148148

149149
template <typename F, typename A0>
150-
int post_in(F f, A0 a0, int ms) {
151-
return post_in(Context1<F,A0>(f,a0), ms);
150+
int post_in(int ms, F f, A0 a0) {
151+
return post_in(ms, Context1<F,A0>(f,a0));
152152
}
153153

154154
template <typename F>
155-
int post_in(F f, int ms) {
155+
int post_in(int ms, F f) {
156156
void *p = event_alloc(&_equeue, sizeof(F));
157157
if (!p) {
158158
return 0;
@@ -173,32 +173,32 @@ class EventQueue {
173173
* or 0 on failure
174174
*/
175175
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
176-
int post_every(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, int ms) {
177-
return post_every(Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4), ms);
176+
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
177+
return post_every(ms, Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
178178
}
179179

180180
template <typename F, typename A0, typename A1, typename A2, typename A3>
181-
int post_every(F f, A0 a0, A1 a1, A2 a2, A3 a3, int ms) {
182-
return post_every(Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3), ms);
181+
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3) {
182+
return post_every(ms, Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
183183
}
184184

185185
template <typename F, typename A0, typename A1, typename A2>
186-
int post_every(F f, A0 a0, A1 a1, A2 a2, int ms) {
187-
return post_every(Context3<F,A0,A1,A2>(f,a0,a1,a2), ms);
186+
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2) {
187+
return post_every(ms, Context3<F,A0,A1,A2>(f,a0,a1,a2));
188188
}
189189

190190
template <typename F, typename A0, typename A1>
191-
int post_every(F f, A0 a0, A1 a1, int ms) {
192-
return post_every(Context2<F,A0,A1>(f,a0,a1), ms);
191+
int post_every(int ms, F f, A0 a0, A1 a1) {
192+
return post_every(ms, Context2<F,A0,A1>(f,a0,a1));
193193
}
194194

195195
template <typename F, typename A0>
196-
int post_every(F f, A0 a0, int ms) {
197-
return post_every(Context1<F,A0>(f,a0), ms);
196+
int post_every(int ms, F f, A0 a0) {
197+
return post_every(ms, Context1<F,A0>(f,a0));
198198
}
199199

200200
template <typename F>
201-
int post_every(F f, int ms) {
201+
int post_every(int ms, F f) {
202202
void *p = event_alloc(&_equeue, sizeof(F));
203203
if (!p) {
204204
return 0;

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ int main() {
1212

1313
// events are simple callbacks
1414
queue.post(printf, "called immediately\n");
15-
queue.post_in(printf, "called in 2 seconds\n", 2000);
16-
queue.post_every(printf, "called every 1 seconds\n", 1000);
15+
queue.post_in(printf, 2000, "called in 2 seconds\n");
16+
queue.post_every(printf, 1000, "called every 1 seconds\n");
1717

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

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

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

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

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

8888
// An id of 0 indicates an error
8989
if (id) {

TESTS/events/queue/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ void simple_posts_test##i() { \
5151
TEST_ASSERT(touched); \
5252
\
5353
touched = false; \
54-
queue.post_in(func##i,##__VA_ARGS__, 1); \
54+
queue.post_in(1, func##i,##__VA_ARGS__); \
5555
queue.dispatch(2); \
5656
TEST_ASSERT(touched); \
5757
\
5858
touched = false; \
59-
queue.post_every(func##i,##__VA_ARGS__, 1); \
59+
queue.post_every(1, func##i,##__VA_ARGS__); \
6060
queue.dispatch(2); \
6161
TEST_ASSERT(touched); \
6262
}
@@ -82,7 +82,7 @@ void post_in_test() {
8282

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

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

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

102102
queue.dispatch(N*100);
@@ -127,7 +127,7 @@ void event_loop_test2() {
127127

128128
for (int i = 0; i < N; i++) {
129129
tickers[i].start();
130-
loop.post_every(time_func, &tickers[i], (i+1)*100, (i+1)*100);
130+
loop.post_every((i+1)*100, time_func, &tickers[i], (i+1)*100);
131131
Thread::yield();
132132
wait_ms(75);
133133
}
@@ -166,7 +166,7 @@ void cancel_test1() {
166166
int ids[N];
167167

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

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

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

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

0 commit comments

Comments
 (0)