17
17
#ifndef EVENT_H
18
18
#define EVENT_H
19
19
20
+ #include < utility>
20
21
#include " events/EventQueue.h"
21
22
#include " platform/mbed_assert.h"
22
23
@@ -45,6 +46,8 @@ class Event;
45
46
template <typename ... ArgTs>
46
47
class Event <void (ArgTs...)> {
47
48
public:
49
+ using duration = std::chrono::duration<int , std::milli>;
50
+
48
51
/* * Create an event
49
52
*
50
53
* Constructs an event bound to the specified event queue. The specified
@@ -63,13 +66,13 @@ class Event<void(ArgTs...)> {
63
66
if (_event) {
64
67
_event->equeue = &q->_equeue ;
65
68
_event->id = 0 ;
66
- _event->delay = 0 ;
67
- _event->period = - 1 ;
69
+ _event->delay = duration ( 0 ) ;
70
+ _event->period = duration (- 1 ) ;
68
71
69
72
_event->post = &Event::event_post<F>;
70
73
_event->dtor = &Event::event_dtor<F>;
71
74
72
- new (_event + 1 ) F (f );
75
+ new (_event + 1 ) F (std::move (f) );
73
76
74
77
_event->ref = 1 ;
75
78
}
@@ -113,26 +116,48 @@ class Event<void(ArgTs...)> {
113
116
114
117
/* * Configure the delay of an event
115
118
*
116
- * @param delay Millisecond delay before dispatching the event
119
+ * @param d Millisecond delay before dispatching the event
117
120
*/
118
- void delay (int delay )
121
+ void delay (duration d )
119
122
{
120
123
if (_event) {
121
- _event->delay = delay ;
124
+ _event->delay = d ;
122
125
}
123
126
}
124
127
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
+
125
139
/* * Configure the period of an event
126
140
*
127
- * @param period Millisecond period for repeatedly dispatching an event
141
+ * @param p Millisecond period for repeatedly dispatching an event
128
142
*/
129
- void period (int period )
143
+ void period (duration p )
130
144
{
131
145
if (_event) {
132
- _event->period = period ;
146
+ _event->period = p ;
133
147
}
134
148
}
135
149
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
+
136
161
/* * Posts an event onto the underlying event queue
137
162
*
138
163
* The event is posted to the underlying queue and is executed in the
@@ -209,8 +234,8 @@ class Event<void(ArgTs...)> {
209
234
equeue_t *equeue;
210
235
int id;
211
236
212
- int delay;
213
- int period;
237
+ duration delay;
238
+ duration period;
214
239
215
240
int (*post)(struct event *, ArgTs... args);
216
241
void (*dtor)(struct event *);
@@ -229,8 +254,8 @@ class Event<void(ArgTs...)> {
229
254
}
230
255
231
256
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 () );
234
259
equeue_event_dtor (p, &EventQueue::function_dtor<C>);
235
260
return equeue_post (e->equeue , &EventQueue::function_call<C>, p);
236
261
}
0 commit comments