Skip to content

Commit 08ed067

Browse files
committed
Add Chrono support to Event/EventQueue
1 parent e7c896b commit 08ed067

File tree

3 files changed

+242
-53
lines changed

3 files changed

+242
-53
lines changed

events/Event.h

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef EVENT_H
1818
#define EVENT_H
1919

20+
#include <utility>
2021
#include "events/EventQueue.h"
2122
#include "platform/mbed_assert.h"
2223

@@ -45,6 +46,8 @@ class Event;
4546
template <typename... ArgTs>
4647
class Event<void(ArgTs...)> {
4748
public:
49+
using duration = std::chrono::duration<int, std::milli>;
50+
4851
/** Create an event
4952
*
5053
* Constructs an event bound to the specified event queue. The specified
@@ -63,13 +66,13 @@ class Event<void(ArgTs...)> {
6366
if (_event) {
6467
_event->equeue = &q->_equeue;
6568
_event->id = 0;
66-
_event->delay = 0;
67-
_event->period = -1;
69+
_event->delay = duration(0);
70+
_event->period = duration(-1);
6871

6972
_event->post = &Event::event_post<F>;
7073
_event->dtor = &Event::event_dtor<F>;
7174

72-
new (_event + 1) F(f);
75+
new (_event + 1) F(std::move(f));
7376

7477
_event->ref = 1;
7578
}
@@ -113,26 +116,48 @@ class Event<void(ArgTs...)> {
113116

114117
/** Configure the delay of an event
115118
*
116-
* @param delay Millisecond delay before dispatching the event
119+
* @param d Millisecond delay before dispatching the event
117120
*/
118-
void delay(int delay)
121+
void delay(duration d)
119122
{
120123
if (_event) {
121-
_event->delay = delay;
124+
_event->delay = d;
122125
}
123126
}
124127

128+
/** Configure the delay of an event
129+
* @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
130+
*
131+
* @param d Millisecond delay before dispatching the event
132+
*/
133+
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
134+
void delay(int d)
135+
{
136+
delay(duration(d));
137+
}
138+
125139
/** Configure the period of an event
126140
*
127-
* @param period Millisecond period for repeatedly dispatching an event
141+
* @param p Millisecond period for repeatedly dispatching an event
128142
*/
129-
void period(int period)
143+
void period(duration p)
130144
{
131145
if (_event) {
132-
_event->period = period;
146+
_event->period = p;
133147
}
134148
}
135149

150+
/** Configure the period of an event
151+
* @deprecated Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.
152+
*
153+
* @param p Millisecond period for repeatedly dispatching an event
154+
*/
155+
MBED_DEPRECATED_SINCE("mbed-os-6.0.0", "Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`.")
156+
void period(int p)
157+
{
158+
period(duration(p));
159+
}
160+
136161
/** Posts an event onto the underlying event queue
137162
*
138163
* The event is posted to the underlying queue and is executed in the
@@ -209,8 +234,8 @@ class Event<void(ArgTs...)> {
209234
equeue_t *equeue;
210235
int id;
211236

212-
int delay;
213-
int period;
237+
duration delay;
238+
duration period;
214239

215240
int (*post)(struct event *, ArgTs... args);
216241
void (*dtor)(struct event *);
@@ -229,8 +254,8 @@ class Event<void(ArgTs...)> {
229254
}
230255

231256
new (p) C(*(F *)(e + 1), args...);
232-
equeue_event_delay(p, e->delay);
233-
equeue_event_period(p, e->period);
257+
equeue_event_delay(p, e->delay.count());
258+
equeue_event_period(p, e->period.count());
234259
equeue_event_dtor(p, &EventQueue::function_dtor<C>);
235260
return equeue_post(e->equeue, &EventQueue::function_call<C>, p);
236261
}

0 commit comments

Comments
 (0)