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

Commit 59d08c4

Browse files
authored
Merge pull request #2 from ARMmbed/post-refactor
Moved delay/period to first argument of post_in/post_every
2 parents ab956fd + 58dca8f commit 59d08c4

File tree

3 files changed

+84
-85
lines changed

3 files changed

+84
-85
lines changed

EventQueue.h

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,6 @@ class EventQueue {
8181
* @return A positive id representing the event in the queue,
8282
* or 0 on failure
8383
*/
84-
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
85-
int post(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
86-
return post(Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
87-
}
88-
89-
template <typename F, typename A0, typename A1, typename A2, typename A3>
90-
int post(F f, A0 a0, A1 a1, A2 a2, A3 a3) {
91-
return post(Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
92-
}
93-
94-
template <typename F, typename A0, typename A1, typename A2>
95-
int post(F f, A0 a0, A1 a1, A2 a2) {
96-
return post(Context3<F,A0,A1,A2>(f,a0,a1,a2));
97-
}
98-
99-
template <typename F, typename A0, typename A1>
100-
int post(F f, A0 a0, A1 a1) {
101-
return post(Context2<F,A0,A1>(f,a0,a1));
102-
}
103-
104-
template <typename F, typename A0>
105-
int post(F f, A0 a0) {
106-
return post(Context1<F,A0>(f,a0));
107-
}
108-
10984
template <typename F>
11085
int post(F f) {
11186
void *p = event_alloc(&_equeue, sizeof(F));
@@ -118,41 +93,41 @@ class EventQueue {
11893
return event_post(&_equeue, &EventQueue::call<F>, e);
11994
}
12095

121-
/** Post an event to the queue after a specified delay
122-
*
123-
* @param f Function to call on event dispatch
124-
* @param a0..a4 Arguments to pass to the callback
125-
* @param ms Time to delay in milliseconds
126-
* @return A positive id representing the event in the queue,
127-
* or 0 on failure
128-
*/
129-
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);
96+
template <typename F, typename A0>
97+
int post(F f, A0 a0) {
98+
return post(Context1<F,A0>(f,a0));
13299
}
133100

134-
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);
101+
template <typename F, typename A0, typename A1>
102+
int post(F f, A0 a0, A1 a1) {
103+
return post(Context2<F,A0,A1>(f,a0,a1));
137104
}
138105

139106
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);
107+
int post(F f, A0 a0, A1 a1, A2 a2) {
108+
return post(Context3<F,A0,A1,A2>(f,a0,a1,a2));
142109
}
143110

144-
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);
111+
template <typename F, typename A0, typename A1, typename A2, typename A3>
112+
int post(F f, A0 a0, A1 a1, A2 a2, A3 a3) {
113+
return post(Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
147114
}
148115

149-
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);
116+
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
117+
int post(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
118+
return post(Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
152119
}
153120

121+
/** Post an event to the queue after a specified delay
122+
*
123+
* @param f Function to call on event dispatch
124+
* @param a0..a4 Arguments to pass to the callback
125+
* @param ms Time to delay in milliseconds
126+
* @return A positive id representing the event in the queue,
127+
* or 0 on failure
128+
*/
154129
template <typename F>
155-
int post_in(F f, int ms) {
130+
int post_in(int ms, F f) {
156131
void *p = event_alloc(&_equeue, sizeof(F));
157132
if (!p) {
158133
return 0;
@@ -164,41 +139,41 @@ class EventQueue {
164139
return event_post(&_equeue, &EventQueue::call<F>, e);
165140
}
166141

167-
/** Post an event to the queue periodically
168-
*
169-
* @param f Function to call on event dispatch
170-
* @param a0..a4 Arguments to pass to the callback
171-
* @param ms Period of the event in milliseconds
172-
* @return A positive id representing the event in the queue,
173-
* or 0 on failure
174-
*/
175-
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);
142+
template <typename F, typename A0>
143+
int post_in(int ms, F f, A0 a0) {
144+
return post_in(ms, Context1<F,A0>(f,a0));
178145
}
179146

180-
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);
147+
template <typename F, typename A0, typename A1>
148+
int post_in(int ms, F f, A0 a0, A1 a1) {
149+
return post_in(ms, Context2<F,A0,A1>(f,a0,a1));
183150
}
184151

185152
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);
153+
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2) {
154+
return post_in(ms, Context3<F,A0,A1,A2>(f,a0,a1,a2));
188155
}
189156

190-
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);
157+
template <typename F, typename A0, typename A1, typename A2, typename A3>
158+
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3) {
159+
return post_in(ms, Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
193160
}
194161

195-
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);
162+
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
163+
int post_in(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
164+
return post_in(ms, Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
198165
}
199166

167+
/** Post an event to the queue periodically
168+
*
169+
* @param f Function to call on event dispatch
170+
* @param a0..a4 Arguments to pass to the callback
171+
* @param ms Period of the event in milliseconds
172+
* @return A positive id representing the event in the queue,
173+
* or 0 on failure
174+
*/
200175
template <typename F>
201-
int post_every(F f, int ms) {
176+
int post_every(int ms, F f) {
202177
void *p = event_alloc(&_equeue, sizeof(F));
203178
if (!p) {
204179
return 0;
@@ -211,6 +186,30 @@ class EventQueue {
211186
return event_post(&_equeue, &EventQueue::call<F>, e);
212187
}
213188

189+
template <typename F, typename A0>
190+
int post_every(int ms, F f, A0 a0) {
191+
return post_every(ms, Context1<F,A0>(f,a0));
192+
}
193+
194+
template <typename F, typename A0, typename A1>
195+
int post_every(int ms, F f, A0 a0, A1 a1) {
196+
return post_every(ms, Context2<F,A0,A1>(f,a0,a1));
197+
}
198+
199+
template <typename F, typename A0, typename A1, typename A2>
200+
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2) {
201+
return post_every(ms, Context3<F,A0,A1,A2>(f,a0,a1,a2));
202+
}
203+
204+
template <typename F, typename A0, typename A1, typename A2, typename A3>
205+
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3) {
206+
return post_every(ms, Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
207+
}
208+
209+
template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
210+
int post_every(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
211+
return post_every(ms, Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
212+
}
214213

215214
protected:
216215
void break_();

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)