Note! This project has moved to github.com/armmbed/mbed-events
This repository has been superceded
This project has moved to mbed-events
Composable event loops combine the cheap synchronicity of event loops with the composability of preempted threads.
Two modular event queue classes are provided:
- EventLoop - for loops coupled with a c++ managed thread
- EventQueue - for manually managed event queues
The Event class takes advantage of the extensibility of FuncPtr to allow an event to be passed through APIs as a normal function.
More information on composable event loops.
Event.h@20:2f9d9c53a5af, 2016-04-18 (annotated)
- Committer:
- Christopher Haster
- Date:
- Mon Apr 18 13:22:21 2016 -0500
- Revision:
- 20:2f9d9c53a5af
- Parent:
- 17:6d564266850e
Add event_pointer argument to avoid memory allocation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
0:b1b901ae3696 | 1 | /* Event |
Christopher Haster |
0:b1b901ae3696 | 2 | * |
Christopher Haster |
0:b1b901ae3696 | 3 | * Pendable event |
Christopher Haster |
0:b1b901ae3696 | 4 | */ |
Christopher Haster |
0:b1b901ae3696 | 5 | #ifndef EVENT_H |
Christopher Haster |
0:b1b901ae3696 | 6 | #define EVENT_H |
Christopher Haster |
0:b1b901ae3696 | 7 | |
Christopher Haster |
0:b1b901ae3696 | 8 | #include "EventQueue.h" |
Christopher Haster |
0:b1b901ae3696 | 9 | #include "FuncPtr.h" |
Christopher Haster |
0:b1b901ae3696 | 10 | #include "Binder.h" |
Christopher Haster |
0:b1b901ae3696 | 11 | |
Christopher Haster |
0:b1b901ae3696 | 12 | |
Christopher Haster |
0:b1b901ae3696 | 13 | /** Pendable event class |
Christopher Haster |
0:b1b901ae3696 | 14 | */ |
Christopher Haster |
0:b1b901ae3696 | 15 | template <typename F> |
Christopher Haster |
0:b1b901ae3696 | 16 | class Event; |
Christopher Haster |
0:b1b901ae3696 | 17 | |
Christopher Haster |
0:b1b901ae3696 | 18 | /** Pendable event class |
Christopher Haster |
0:b1b901ae3696 | 19 | */ |
Christopher Haster |
4:30883e8633b4 | 20 | template <typename A0, typename A1, typename A2, typename A3, typename A4> |
Christopher Haster |
4:30883e8633b4 | 21 | class Event<void(A0, A1, A2, A3, A4)> { |
Christopher Haster |
4:30883e8633b4 | 22 | public: |
Christopher Haster |
4:30883e8633b4 | 23 | /** Create an event bound to a queue |
Christopher Haster |
4:30883e8633b4 | 24 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 25 | Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2, A3, A4)> func=0) { |
Christopher Haster |
14:5abf2ccf2dbf | 26 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 27 | _period = -1; |
Christopher Haster |
14:5abf2ccf2dbf | 28 | attach(queue, func); |
Christopher Haster |
4:30883e8633b4 | 29 | } |
Christopher Haster |
4:30883e8633b4 | 30 | |
Christopher Haster |
4:30883e8633b4 | 31 | /** Create an event bound to a queue |
Christopher Haster |
4:30883e8633b4 | 32 | */ |
Christopher Haster |
4:30883e8633b4 | 33 | template <typename T, typename M> |
Christopher Haster |
4:30883e8633b4 | 34 | Event(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
14:5abf2ccf2dbf | 35 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 36 | _period = -1; |
Christopher Haster |
4:30883e8633b4 | 37 | attach(queue, obj, method); |
Christopher Haster |
4:30883e8633b4 | 38 | } |
Christopher Haster |
4:30883e8633b4 | 39 | |
Christopher Haster |
4:30883e8633b4 | 40 | /** Attach an event to a queue |
Christopher Haster |
4:30883e8633b4 | 41 | */ |
Christopher Haster |
13:b84e049b2d9c | 42 | void attach(EventQueue *queue) { |
Christopher Haster |
4:30883e8633b4 | 43 | _queue = queue; |
Christopher Haster |
13:b84e049b2d9c | 44 | } |
Christopher Haster |
13:b84e049b2d9c | 45 | |
Christopher Haster |
13:b84e049b2d9c | 46 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 47 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 48 | void attach(FuncPtr<void(A0, A1, A2, A3, A4)> func) { |
Christopher Haster |
14:5abf2ccf2dbf | 49 | _func.attach(func); |
Christopher Haster |
4:30883e8633b4 | 50 | } |
Christopher Haster |
4:30883e8633b4 | 51 | |
Christopher Haster |
13:b84e049b2d9c | 52 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 53 | */ |
Christopher Haster |
13:b84e049b2d9c | 54 | template <typename T, typename M> |
Christopher Haster |
13:b84e049b2d9c | 55 | void attach(T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 56 | attach(FuncPtr<void(A0, A1, A2, A3, A4)>(obj, method)); |
Christopher Haster |
13:b84e049b2d9c | 57 | } |
Christopher Haster |
13:b84e049b2d9c | 58 | |
Christopher Haster |
14:5abf2ccf2dbf | 59 | /** Attach a func to an event |
Christopher Haster |
13:b84e049b2d9c | 60 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 61 | void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3, A4)> func) { |
Christopher Haster |
13:b84e049b2d9c | 62 | attach(queue); |
Christopher Haster |
14:5abf2ccf2dbf | 63 | attach(func); |
Christopher Haster |
13:b84e049b2d9c | 64 | } |
Christopher Haster |
13:b84e049b2d9c | 65 | |
Christopher Haster |
4:30883e8633b4 | 66 | /** Attach an event to a queue |
Christopher Haster |
4:30883e8633b4 | 67 | */ |
Christopher Haster |
4:30883e8633b4 | 68 | template <typename T, typename M> |
Christopher Haster |
4:30883e8633b4 | 69 | void attach(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 70 | attach(queue); |
Christopher Haster |
13:b84e049b2d9c | 71 | attach(obj, method); |
Christopher Haster |
4:30883e8633b4 | 72 | } |
Christopher Haster |
4:30883e8633b4 | 73 | |
Christopher Haster |
4:30883e8633b4 | 74 | /** Set delay for when the event is dispatched after it is posted |
Christopher Haster |
4:30883e8633b4 | 75 | * @param ms Delay in milliseconds |
Christopher Haster |
4:30883e8633b4 | 76 | */ |
Christopher Haster |
4:30883e8633b4 | 77 | void delay(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 78 | _delay = ms; |
Christopher Haster |
4:30883e8633b4 | 79 | } |
Christopher Haster |
4:30883e8633b4 | 80 | |
Christopher Haster |
4:30883e8633b4 | 81 | /** Set event to repeat periodically after it is posted |
Christopher Haster |
12:1feb78280125 | 82 | * @param ms Period in milliseconds |
Christopher Haster |
4:30883e8633b4 | 83 | */ |
Christopher Haster |
4:30883e8633b4 | 84 | void period(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 85 | _period = ms; |
Christopher Haster |
4:30883e8633b4 | 86 | } |
Christopher Haster |
4:30883e8633b4 | 87 | |
Christopher Haster |
4:30883e8633b4 | 88 | /** Set tolerance hint on when the event must be called, defaults to 0 |
Christopher Haster |
12:1feb78280125 | 89 | * @param ms Tolerance in milliseconds |
Christopher Haster |
4:30883e8633b4 | 90 | */ |
Christopher Haster |
4:30883e8633b4 | 91 | void tolerance(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 92 | _tolerance = ms; |
Christopher Haster |
4:30883e8633b4 | 93 | } |
Christopher Haster |
4:30883e8633b4 | 94 | |
Christopher Haster |
4:30883e8633b4 | 95 | /** Post the event onto the bound queue |
Christopher Haster |
14:5abf2ccf2dbf | 96 | * @param ms Max time to wait if memory is not available in milliseconds |
Christopher Haster |
12:1feb78280125 | 97 | * @return True if the event was posted successfully |
Christopher Haster |
4:30883e8633b4 | 98 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 99 | bool trigger(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, int ms=-1) { |
Christopher Haster |
17:6d564266850e | 100 | return _queue->event_trigger( |
Christopher Haster |
14:5abf2ccf2dbf | 101 | Binder<void(A0,A1,A2,A3,A4),A0,A1,A2,A3,A4>(_func,a0,a1,a2,a3,a4), |
Christopher Haster |
14:5abf2ccf2dbf | 102 | _delay, _period, _tolerance, ms); |
Christopher Haster |
12:1feb78280125 | 103 | } |
Christopher Haster |
12:1feb78280125 | 104 | |
Christopher Haster |
12:1feb78280125 | 105 | /** Post the event onto the bound queue |
Christopher Haster |
12:1feb78280125 | 106 | */ |
Christopher Haster |
12:1feb78280125 | 107 | void call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { |
Christopher Haster |
12:1feb78280125 | 108 | trigger(a0, a1, a2, a3, a4); |
Christopher Haster |
4:30883e8633b4 | 109 | } |
Christopher Haster |
4:30883e8633b4 | 110 | |
Christopher Haster |
4:30883e8633b4 | 111 | /** Post the event onto the bound queue |
Christopher Haster |
4:30883e8633b4 | 112 | */ |
Christopher Haster |
4:30883e8633b4 | 113 | void operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { |
Christopher Haster |
4:30883e8633b4 | 114 | return call(a0, a1, a2, a3, a4); |
Christopher Haster |
4:30883e8633b4 | 115 | } |
Christopher Haster |
4:30883e8633b4 | 116 | |
Christopher Haster |
4:30883e8633b4 | 117 | /** Test if event has been bound |
Christopher Haster |
4:30883e8633b4 | 118 | */ |
Christopher Haster |
4:30883e8633b4 | 119 | operator bool() const { |
Christopher Haster |
14:5abf2ccf2dbf | 120 | return _func && _queue; |
Christopher Haster |
4:30883e8633b4 | 121 | } |
Christopher Haster |
4:30883e8633b4 | 122 | |
Christopher Haster |
4:30883e8633b4 | 123 | /** Static thunk for passing as C-style function |
Christopher Haster |
4:30883e8633b4 | 124 | * @param data Event to dispatch passed as void pointer |
Christopher Haster |
4:30883e8633b4 | 125 | */ |
Christopher Haster |
4:30883e8633b4 | 126 | static void thunk(void *data, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { |
Christopher Haster |
4:30883e8633b4 | 127 | return static_cast<Event<void(A0, A1, A2, A3, A4)>*>(data) |
Christopher Haster |
4:30883e8633b4 | 128 | ->call(a0, a1, a2, a3, a4); |
Christopher Haster |
4:30883e8633b4 | 129 | } |
Christopher Haster |
4:30883e8633b4 | 130 | |
Christopher Haster |
4:30883e8633b4 | 131 | private: |
Christopher Haster |
14:5abf2ccf2dbf | 132 | FuncPtr<void(A0,A1,A2,A3,A4)> _func; |
Christopher Haster |
4:30883e8633b4 | 133 | EventQueue *_queue; |
Christopher Haster |
14:5abf2ccf2dbf | 134 | int _delay; |
Christopher Haster |
14:5abf2ccf2dbf | 135 | int _period; |
Christopher Haster |
14:5abf2ccf2dbf | 136 | int _tolerance; |
Christopher Haster |
4:30883e8633b4 | 137 | }; |
Christopher Haster |
4:30883e8633b4 | 138 | |
Christopher Haster |
4:30883e8633b4 | 139 | /** Pendable event class |
Christopher Haster |
4:30883e8633b4 | 140 | */ |
Christopher Haster |
0:b1b901ae3696 | 141 | template <typename A0, typename A1, typename A2, typename A3> |
Christopher Haster |
0:b1b901ae3696 | 142 | class Event<void(A0, A1, A2, A3)> { |
Christopher Haster |
0:b1b901ae3696 | 143 | public: |
Christopher Haster |
0:b1b901ae3696 | 144 | /** Create an event bound to a queue |
Christopher Haster |
0:b1b901ae3696 | 145 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 146 | Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2, A3)> func=0) { |
Christopher Haster |
14:5abf2ccf2dbf | 147 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 148 | _period = -1; |
Christopher Haster |
14:5abf2ccf2dbf | 149 | attach(queue, func); |
Christopher Haster |
0:b1b901ae3696 | 150 | } |
Christopher Haster |
0:b1b901ae3696 | 151 | |
Christopher Haster |
3:6dccdc36651f | 152 | /** Create an event bound to a queue |
Christopher Haster |
3:6dccdc36651f | 153 | */ |
Christopher Haster |
3:6dccdc36651f | 154 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 155 | Event(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
14:5abf2ccf2dbf | 156 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 157 | _period = -1; |
Christopher Haster |
3:6dccdc36651f | 158 | attach(queue, obj, method); |
Christopher Haster |
3:6dccdc36651f | 159 | } |
Christopher Haster |
3:6dccdc36651f | 160 | |
Christopher Haster |
0:b1b901ae3696 | 161 | /** Attach an event to a queue |
Christopher Haster |
0:b1b901ae3696 | 162 | */ |
Christopher Haster |
13:b84e049b2d9c | 163 | void attach(EventQueue *queue) { |
Christopher Haster |
0:b1b901ae3696 | 164 | _queue = queue; |
Christopher Haster |
13:b84e049b2d9c | 165 | } |
Christopher Haster |
13:b84e049b2d9c | 166 | |
Christopher Haster |
13:b84e049b2d9c | 167 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 168 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 169 | void attach(FuncPtr<void(A0, A1, A2, A3)> func) { |
Christopher Haster |
14:5abf2ccf2dbf | 170 | _func.attach(func); |
Christopher Haster |
0:b1b901ae3696 | 171 | } |
Christopher Haster |
0:b1b901ae3696 | 172 | |
Christopher Haster |
13:b84e049b2d9c | 173 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 174 | */ |
Christopher Haster |
13:b84e049b2d9c | 175 | template <typename T, typename M> |
Christopher Haster |
13:b84e049b2d9c | 176 | void attach(T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 177 | attach(FuncPtr<void(A0, A1, A2, A3)>(obj, method)); |
Christopher Haster |
13:b84e049b2d9c | 178 | } |
Christopher Haster |
13:b84e049b2d9c | 179 | |
Christopher Haster |
14:5abf2ccf2dbf | 180 | /** Attach a func to an event |
Christopher Haster |
13:b84e049b2d9c | 181 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 182 | void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3)> func) { |
Christopher Haster |
13:b84e049b2d9c | 183 | attach(queue); |
Christopher Haster |
14:5abf2ccf2dbf | 184 | attach(func); |
Christopher Haster |
13:b84e049b2d9c | 185 | } |
Christopher Haster |
13:b84e049b2d9c | 186 | |
Christopher Haster |
3:6dccdc36651f | 187 | /** Attach an event to a queue |
Christopher Haster |
3:6dccdc36651f | 188 | */ |
Christopher Haster |
3:6dccdc36651f | 189 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 190 | void attach(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 191 | attach(queue); |
Christopher Haster |
13:b84e049b2d9c | 192 | attach(obj, method); |
Christopher Haster |
3:6dccdc36651f | 193 | } |
Christopher Haster |
3:6dccdc36651f | 194 | |
Christopher Haster |
2:11cda6bead99 | 195 | /** Set delay for when the event is dispatched after it is posted |
Christopher Haster |
2:11cda6bead99 | 196 | * @param ms Delay in milliseconds |
Christopher Haster |
2:11cda6bead99 | 197 | */ |
Christopher Haster |
2:11cda6bead99 | 198 | void delay(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 199 | _delay = ms; |
Christopher Haster |
2:11cda6bead99 | 200 | } |
Christopher Haster |
2:11cda6bead99 | 201 | |
Christopher Haster |
2:11cda6bead99 | 202 | /** Set event to repeat periodically after it is posted |
Christopher Haster |
12:1feb78280125 | 203 | * @param ms Period in milliseconds |
Christopher Haster |
2:11cda6bead99 | 204 | */ |
Christopher Haster |
2:11cda6bead99 | 205 | void period(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 206 | _period = ms; |
Christopher Haster |
2:11cda6bead99 | 207 | } |
Christopher Haster |
2:11cda6bead99 | 208 | |
Christopher Haster |
2:11cda6bead99 | 209 | /** Set tolerance hint on when the event must be called, defaults to 0 |
Christopher Haster |
12:1feb78280125 | 210 | * @param ms Tolerance in milliseconds |
Christopher Haster |
2:11cda6bead99 | 211 | */ |
Christopher Haster |
2:11cda6bead99 | 212 | void tolerance(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 213 | _tolerance = ms; |
Christopher Haster |
2:11cda6bead99 | 214 | } |
Christopher Haster |
2:11cda6bead99 | 215 | |
Christopher Haster |
2:11cda6bead99 | 216 | /** Post the event onto the bound queue |
Christopher Haster |
14:5abf2ccf2dbf | 217 | * @param ms Max time to wait if memory is not available in milliseconds |
Christopher Haster |
14:5abf2ccf2dbf | 218 | * @return True if the event was posted successfully |
Christopher Haster |
0:b1b901ae3696 | 219 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 220 | bool trigger(A0 a0, A1 a1, A2 a2, A3 a3, int ms=-1) { |
Christopher Haster |
17:6d564266850e | 221 | return _queue->event_trigger( |
Christopher Haster |
14:5abf2ccf2dbf | 222 | Binder<void(A0,A1,A2,A3),A0,A1,A2,A3>(_func,a0,a1,a2,a3), |
Christopher Haster |
14:5abf2ccf2dbf | 223 | _delay, _period, _tolerance, ms); |
Christopher Haster |
12:1feb78280125 | 224 | } |
Christopher Haster |
12:1feb78280125 | 225 | |
Christopher Haster |
12:1feb78280125 | 226 | /** Post the event onto the bound queue |
Christopher Haster |
12:1feb78280125 | 227 | */ |
Christopher Haster |
12:1feb78280125 | 228 | void call(A0 a0, A1 a1, A2 a2, A3 a3) { |
Christopher Haster |
12:1feb78280125 | 229 | trigger(a0, a1, a2, a3); |
Christopher Haster |
0:b1b901ae3696 | 230 | } |
Christopher Haster |
0:b1b901ae3696 | 231 | |
Christopher Haster |
2:11cda6bead99 | 232 | /** Post the event onto the bound queue |
Christopher Haster |
0:b1b901ae3696 | 233 | */ |
Christopher Haster |
0:b1b901ae3696 | 234 | void operator()(A0 a0, A1 a1, A2 a2, A3 a3) { |
Christopher Haster |
0:b1b901ae3696 | 235 | return call(a0, a1, a2, a3); |
Christopher Haster |
0:b1b901ae3696 | 236 | } |
Christopher Haster |
0:b1b901ae3696 | 237 | |
Christopher Haster |
0:b1b901ae3696 | 238 | /** Test if event has been bound |
Christopher Haster |
0:b1b901ae3696 | 239 | */ |
Christopher Haster |
0:b1b901ae3696 | 240 | operator bool() const { |
Christopher Haster |
14:5abf2ccf2dbf | 241 | return _func && _queue; |
Christopher Haster |
0:b1b901ae3696 | 242 | } |
Christopher Haster |
0:b1b901ae3696 | 243 | |
Christopher Haster |
0:b1b901ae3696 | 244 | /** Static thunk for passing as C-style function |
Christopher Haster |
0:b1b901ae3696 | 245 | * @param data Event to dispatch passed as void pointer |
Christopher Haster |
0:b1b901ae3696 | 246 | */ |
Christopher Haster |
0:b1b901ae3696 | 247 | static void thunk(void *data, A0 a0, A1 a1, A2 a2, A3 a3) { |
Christopher Haster |
0:b1b901ae3696 | 248 | return static_cast<Event<void(A0, A1, A2, A3)>*>(data) |
Christopher Haster |
0:b1b901ae3696 | 249 | ->call(a0, a1, a2, a3); |
Christopher Haster |
0:b1b901ae3696 | 250 | } |
Christopher Haster |
0:b1b901ae3696 | 251 | |
Christopher Haster |
0:b1b901ae3696 | 252 | private: |
Christopher Haster |
14:5abf2ccf2dbf | 253 | FuncPtr<void(A0,A1,A2,A3)> _func; |
Christopher Haster |
0:b1b901ae3696 | 254 | EventQueue *_queue; |
Christopher Haster |
14:5abf2ccf2dbf | 255 | int _delay; |
Christopher Haster |
14:5abf2ccf2dbf | 256 | int _period; |
Christopher Haster |
14:5abf2ccf2dbf | 257 | int _tolerance; |
Christopher Haster |
0:b1b901ae3696 | 258 | }; |
Christopher Haster |
0:b1b901ae3696 | 259 | |
Christopher Haster |
0:b1b901ae3696 | 260 | /** Pendable event class |
Christopher Haster |
0:b1b901ae3696 | 261 | */ |
Christopher Haster |
0:b1b901ae3696 | 262 | template <typename A0, typename A1, typename A2> |
Christopher Haster |
0:b1b901ae3696 | 263 | class Event<void(A0, A1, A2)> { |
Christopher Haster |
0:b1b901ae3696 | 264 | public: |
Christopher Haster |
0:b1b901ae3696 | 265 | /** Create an event bound to a queue |
Christopher Haster |
0:b1b901ae3696 | 266 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 267 | Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2)> func=0) { |
Christopher Haster |
14:5abf2ccf2dbf | 268 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 269 | _period = -1; |
Christopher Haster |
14:5abf2ccf2dbf | 270 | attach(queue, func); |
Christopher Haster |
0:b1b901ae3696 | 271 | } |
Christopher Haster |
0:b1b901ae3696 | 272 | |
Christopher Haster |
3:6dccdc36651f | 273 | /** Create an event bound to a queue |
Christopher Haster |
3:6dccdc36651f | 274 | */ |
Christopher Haster |
3:6dccdc36651f | 275 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 276 | Event(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
14:5abf2ccf2dbf | 277 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 278 | _period = -1; |
Christopher Haster |
3:6dccdc36651f | 279 | attach(queue, obj, method); |
Christopher Haster |
3:6dccdc36651f | 280 | } |
Christopher Haster |
3:6dccdc36651f | 281 | |
Christopher Haster |
0:b1b901ae3696 | 282 | /** Attach an event to a queue |
Christopher Haster |
0:b1b901ae3696 | 283 | */ |
Christopher Haster |
13:b84e049b2d9c | 284 | void attach(EventQueue *queue) { |
Christopher Haster |
0:b1b901ae3696 | 285 | _queue = queue; |
Christopher Haster |
13:b84e049b2d9c | 286 | } |
Christopher Haster |
13:b84e049b2d9c | 287 | |
Christopher Haster |
13:b84e049b2d9c | 288 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 289 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 290 | void attach(FuncPtr<void(A0, A1, A2)> func) { |
Christopher Haster |
14:5abf2ccf2dbf | 291 | _func.attach(func); |
Christopher Haster |
0:b1b901ae3696 | 292 | } |
Christopher Haster |
0:b1b901ae3696 | 293 | |
Christopher Haster |
13:b84e049b2d9c | 294 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 295 | */ |
Christopher Haster |
13:b84e049b2d9c | 296 | template <typename T, typename M> |
Christopher Haster |
13:b84e049b2d9c | 297 | void attach(T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 298 | attach(FuncPtr<void(A0, A1, A2)>(obj, method)); |
Christopher Haster |
13:b84e049b2d9c | 299 | } |
Christopher Haster |
13:b84e049b2d9c | 300 | |
Christopher Haster |
14:5abf2ccf2dbf | 301 | /** Attach a func to an event |
Christopher Haster |
13:b84e049b2d9c | 302 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 303 | void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2)> func) { |
Christopher Haster |
13:b84e049b2d9c | 304 | attach(queue); |
Christopher Haster |
14:5abf2ccf2dbf | 305 | attach(func); |
Christopher Haster |
13:b84e049b2d9c | 306 | } |
Christopher Haster |
13:b84e049b2d9c | 307 | |
Christopher Haster |
3:6dccdc36651f | 308 | /** Attach an event to a queue |
Christopher Haster |
3:6dccdc36651f | 309 | */ |
Christopher Haster |
3:6dccdc36651f | 310 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 311 | void attach(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 312 | attach(queue); |
Christopher Haster |
13:b84e049b2d9c | 313 | attach(obj, method); |
Christopher Haster |
3:6dccdc36651f | 314 | } |
Christopher Haster |
3:6dccdc36651f | 315 | |
Christopher Haster |
2:11cda6bead99 | 316 | /** Set delay for when the event is dispatched after it is posted |
Christopher Haster |
2:11cda6bead99 | 317 | * @param ms Delay in milliseconds |
Christopher Haster |
2:11cda6bead99 | 318 | */ |
Christopher Haster |
2:11cda6bead99 | 319 | void delay(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 320 | _delay = ms; |
Christopher Haster |
2:11cda6bead99 | 321 | } |
Christopher Haster |
2:11cda6bead99 | 322 | |
Christopher Haster |
2:11cda6bead99 | 323 | /** Set event to repeat periodically after it is posted |
Christopher Haster |
12:1feb78280125 | 324 | * @param ms Period in milliseconds |
Christopher Haster |
2:11cda6bead99 | 325 | */ |
Christopher Haster |
2:11cda6bead99 | 326 | void period(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 327 | _period = ms; |
Christopher Haster |
2:11cda6bead99 | 328 | } |
Christopher Haster |
2:11cda6bead99 | 329 | |
Christopher Haster |
2:11cda6bead99 | 330 | /** Set tolerance hint on when the event must be called, defaults to 0 |
Christopher Haster |
12:1feb78280125 | 331 | * @param ms Tolerance in milliseconds |
Christopher Haster |
2:11cda6bead99 | 332 | */ |
Christopher Haster |
2:11cda6bead99 | 333 | void tolerance(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 334 | _tolerance = ms; |
Christopher Haster |
2:11cda6bead99 | 335 | } |
Christopher Haster |
2:11cda6bead99 | 336 | |
Christopher Haster |
2:11cda6bead99 | 337 | /** Post the event onto the bound queue |
Christopher Haster |
14:5abf2ccf2dbf | 338 | * @param ms Max time to wait if memory is not available in milliseconds |
Christopher Haster |
14:5abf2ccf2dbf | 339 | * @return True if the event was posted successfully |
Christopher Haster |
0:b1b901ae3696 | 340 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 341 | bool trigger(A0 a0, A1 a1, A2 a2, int ms=-1) { |
Christopher Haster |
17:6d564266850e | 342 | return _queue->event_trigger( |
Christopher Haster |
14:5abf2ccf2dbf | 343 | Binder<void(A0,A1,A2),A0,A1,A2>(_func,a0,a1,a2), |
Christopher Haster |
14:5abf2ccf2dbf | 344 | _delay, _period, _tolerance, ms); |
Christopher Haster |
12:1feb78280125 | 345 | } |
Christopher Haster |
12:1feb78280125 | 346 | |
Christopher Haster |
12:1feb78280125 | 347 | /** Post the event onto the bound queue |
Christopher Haster |
12:1feb78280125 | 348 | */ |
Christopher Haster |
12:1feb78280125 | 349 | void call(A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
12:1feb78280125 | 350 | trigger(a0, a1, a2); |
Christopher Haster |
0:b1b901ae3696 | 351 | } |
Christopher Haster |
0:b1b901ae3696 | 352 | |
Christopher Haster |
2:11cda6bead99 | 353 | /** Post the event onto the bound queue |
Christopher Haster |
0:b1b901ae3696 | 354 | */ |
Christopher Haster |
0:b1b901ae3696 | 355 | void operator()(A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
0:b1b901ae3696 | 356 | return call(a0, a1, a2); |
Christopher Haster |
0:b1b901ae3696 | 357 | } |
Christopher Haster |
0:b1b901ae3696 | 358 | |
Christopher Haster |
0:b1b901ae3696 | 359 | /** Test if event has been bound |
Christopher Haster |
0:b1b901ae3696 | 360 | */ |
Christopher Haster |
0:b1b901ae3696 | 361 | operator bool() const { |
Christopher Haster |
14:5abf2ccf2dbf | 362 | return _func && _queue; |
Christopher Haster |
0:b1b901ae3696 | 363 | } |
Christopher Haster |
0:b1b901ae3696 | 364 | |
Christopher Haster |
0:b1b901ae3696 | 365 | /** Static thunk for passing as C-style function |
Christopher Haster |
0:b1b901ae3696 | 366 | * @param data Event to dispatch passed as void pointer |
Christopher Haster |
0:b1b901ae3696 | 367 | */ |
Christopher Haster |
0:b1b901ae3696 | 368 | static void thunk(void *data, A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
0:b1b901ae3696 | 369 | return static_cast<Event<void(A0, A1, A2)>*>(data) |
Christopher Haster |
0:b1b901ae3696 | 370 | ->call(a0, a1, a2); |
Christopher Haster |
0:b1b901ae3696 | 371 | } |
Christopher Haster |
0:b1b901ae3696 | 372 | |
Christopher Haster |
0:b1b901ae3696 | 373 | private: |
Christopher Haster |
14:5abf2ccf2dbf | 374 | FuncPtr<void(A0,A1,A2)> _func; |
Christopher Haster |
0:b1b901ae3696 | 375 | EventQueue *_queue; |
Christopher Haster |
14:5abf2ccf2dbf | 376 | int _delay; |
Christopher Haster |
14:5abf2ccf2dbf | 377 | int _period; |
Christopher Haster |
14:5abf2ccf2dbf | 378 | int _tolerance; |
Christopher Haster |
0:b1b901ae3696 | 379 | }; |
Christopher Haster |
0:b1b901ae3696 | 380 | |
Christopher Haster |
0:b1b901ae3696 | 381 | /** Pendable event class |
Christopher Haster |
0:b1b901ae3696 | 382 | */ |
Christopher Haster |
0:b1b901ae3696 | 383 | template <typename A0, typename A1> |
Christopher Haster |
0:b1b901ae3696 | 384 | class Event<void(A0, A1)> { |
Christopher Haster |
0:b1b901ae3696 | 385 | public: |
Christopher Haster |
0:b1b901ae3696 | 386 | /** Create an event bound to a queue |
Christopher Haster |
0:b1b901ae3696 | 387 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 388 | Event(EventQueue *queue=0, FuncPtr<void(A0, A1)> func=0) { |
Christopher Haster |
14:5abf2ccf2dbf | 389 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 390 | _period = -1; |
Christopher Haster |
14:5abf2ccf2dbf | 391 | attach(queue, func); |
Christopher Haster |
0:b1b901ae3696 | 392 | } |
Christopher Haster |
0:b1b901ae3696 | 393 | |
Christopher Haster |
3:6dccdc36651f | 394 | /** Create an event bound to a queue |
Christopher Haster |
3:6dccdc36651f | 395 | */ |
Christopher Haster |
3:6dccdc36651f | 396 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 397 | Event(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
14:5abf2ccf2dbf | 398 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 399 | _period = -1; |
Christopher Haster |
3:6dccdc36651f | 400 | attach(queue, obj, method); |
Christopher Haster |
3:6dccdc36651f | 401 | } |
Christopher Haster |
3:6dccdc36651f | 402 | |
Christopher Haster |
0:b1b901ae3696 | 403 | /** Attach an event to a queue |
Christopher Haster |
0:b1b901ae3696 | 404 | */ |
Christopher Haster |
13:b84e049b2d9c | 405 | void attach(EventQueue *queue) { |
Christopher Haster |
0:b1b901ae3696 | 406 | _queue = queue; |
Christopher Haster |
13:b84e049b2d9c | 407 | } |
Christopher Haster |
13:b84e049b2d9c | 408 | |
Christopher Haster |
13:b84e049b2d9c | 409 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 410 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 411 | void attach(FuncPtr<void(A0, A1)> func) { |
Christopher Haster |
14:5abf2ccf2dbf | 412 | _func.attach(func); |
Christopher Haster |
0:b1b901ae3696 | 413 | } |
Christopher Haster |
0:b1b901ae3696 | 414 | |
Christopher Haster |
13:b84e049b2d9c | 415 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 416 | */ |
Christopher Haster |
13:b84e049b2d9c | 417 | template <typename T, typename M> |
Christopher Haster |
13:b84e049b2d9c | 418 | void attach(T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 419 | attach(FuncPtr<void(A0, A1)>(obj, method)); |
Christopher Haster |
13:b84e049b2d9c | 420 | } |
Christopher Haster |
13:b84e049b2d9c | 421 | |
Christopher Haster |
14:5abf2ccf2dbf | 422 | /** Attach a func to an event |
Christopher Haster |
13:b84e049b2d9c | 423 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 424 | void attach(EventQueue *queue, FuncPtr<void(A0, A1)> func) { |
Christopher Haster |
13:b84e049b2d9c | 425 | attach(queue); |
Christopher Haster |
14:5abf2ccf2dbf | 426 | attach(func); |
Christopher Haster |
13:b84e049b2d9c | 427 | } |
Christopher Haster |
13:b84e049b2d9c | 428 | |
Christopher Haster |
3:6dccdc36651f | 429 | /** Attach an event to a queue |
Christopher Haster |
3:6dccdc36651f | 430 | */ |
Christopher Haster |
3:6dccdc36651f | 431 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 432 | void attach(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 433 | attach(queue); |
Christopher Haster |
13:b84e049b2d9c | 434 | attach(obj, method); |
Christopher Haster |
3:6dccdc36651f | 435 | } |
Christopher Haster |
3:6dccdc36651f | 436 | |
Christopher Haster |
2:11cda6bead99 | 437 | /** Set delay for when the event is dispatched after it is posted |
Christopher Haster |
2:11cda6bead99 | 438 | * @param ms Delay in milliseconds |
Christopher Haster |
2:11cda6bead99 | 439 | */ |
Christopher Haster |
2:11cda6bead99 | 440 | void delay(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 441 | _delay = ms; |
Christopher Haster |
2:11cda6bead99 | 442 | } |
Christopher Haster |
2:11cda6bead99 | 443 | |
Christopher Haster |
2:11cda6bead99 | 444 | /** Set event to repeat periodically after it is posted |
Christopher Haster |
12:1feb78280125 | 445 | * @param ms Period in milliseconds |
Christopher Haster |
2:11cda6bead99 | 446 | */ |
Christopher Haster |
2:11cda6bead99 | 447 | void period(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 448 | _period = ms; |
Christopher Haster |
2:11cda6bead99 | 449 | } |
Christopher Haster |
2:11cda6bead99 | 450 | |
Christopher Haster |
2:11cda6bead99 | 451 | /** Set tolerance hint on when the event must be called, defaults to 0 |
Christopher Haster |
12:1feb78280125 | 452 | * @param ms Tolerance in milliseconds |
Christopher Haster |
2:11cda6bead99 | 453 | */ |
Christopher Haster |
2:11cda6bead99 | 454 | void tolerance(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 455 | _tolerance = ms; |
Christopher Haster |
2:11cda6bead99 | 456 | } |
Christopher Haster |
2:11cda6bead99 | 457 | |
Christopher Haster |
2:11cda6bead99 | 458 | /** Post the event onto the bound queue |
Christopher Haster |
14:5abf2ccf2dbf | 459 | * @param ms Max time to wait if memory is not available in milliseconds |
Christopher Haster |
14:5abf2ccf2dbf | 460 | * @return True if the event was posted successfully |
Christopher Haster |
0:b1b901ae3696 | 461 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 462 | bool trigger(A0 a0, A1 a1, int ms=-1) { |
Christopher Haster |
17:6d564266850e | 463 | return _queue->event_trigger( |
Christopher Haster |
14:5abf2ccf2dbf | 464 | Binder<void(A0,A1),A0,A1>(_func,a0,a1), |
Christopher Haster |
14:5abf2ccf2dbf | 465 | _delay, _period, _tolerance, ms); |
Christopher Haster |
12:1feb78280125 | 466 | } |
Christopher Haster |
12:1feb78280125 | 467 | |
Christopher Haster |
12:1feb78280125 | 468 | /** Post the event onto the bound queue |
Christopher Haster |
12:1feb78280125 | 469 | */ |
Christopher Haster |
12:1feb78280125 | 470 | void call(A0 a0, A1 a1) { |
Christopher Haster |
12:1feb78280125 | 471 | trigger(a0, a1); |
Christopher Haster |
0:b1b901ae3696 | 472 | } |
Christopher Haster |
0:b1b901ae3696 | 473 | |
Christopher Haster |
2:11cda6bead99 | 474 | /** Post the event onto the bound queue |
Christopher Haster |
0:b1b901ae3696 | 475 | */ |
Christopher Haster |
0:b1b901ae3696 | 476 | void operator()(A0 a0, A1 a1) { |
Christopher Haster |
0:b1b901ae3696 | 477 | return call(a0, a1); |
Christopher Haster |
0:b1b901ae3696 | 478 | } |
Christopher Haster |
0:b1b901ae3696 | 479 | |
Christopher Haster |
0:b1b901ae3696 | 480 | /** Test if event has been bound |
Christopher Haster |
0:b1b901ae3696 | 481 | */ |
Christopher Haster |
0:b1b901ae3696 | 482 | operator bool() const { |
Christopher Haster |
14:5abf2ccf2dbf | 483 | return _func && _queue; |
Christopher Haster |
0:b1b901ae3696 | 484 | } |
Christopher Haster |
0:b1b901ae3696 | 485 | |
Christopher Haster |
0:b1b901ae3696 | 486 | /** Static thunk for passing as C-style function |
Christopher Haster |
0:b1b901ae3696 | 487 | * @param data Event to dispatch passed as void pointer |
Christopher Haster |
0:b1b901ae3696 | 488 | */ |
Christopher Haster |
0:b1b901ae3696 | 489 | static void thunk(void *data, A0 a0, A1 a1) { |
Christopher Haster |
0:b1b901ae3696 | 490 | return static_cast<Event<void(A0, A1)>*>(data) |
Christopher Haster |
0:b1b901ae3696 | 491 | ->call(a0, a1); |
Christopher Haster |
0:b1b901ae3696 | 492 | } |
Christopher Haster |
0:b1b901ae3696 | 493 | |
Christopher Haster |
0:b1b901ae3696 | 494 | private: |
Christopher Haster |
14:5abf2ccf2dbf | 495 | FuncPtr<void(A0,A1)> _func; |
Christopher Haster |
0:b1b901ae3696 | 496 | EventQueue *_queue; |
Christopher Haster |
14:5abf2ccf2dbf | 497 | int _delay; |
Christopher Haster |
14:5abf2ccf2dbf | 498 | int _period; |
Christopher Haster |
14:5abf2ccf2dbf | 499 | int _tolerance; |
Christopher Haster |
0:b1b901ae3696 | 500 | }; |
Christopher Haster |
0:b1b901ae3696 | 501 | |
Christopher Haster |
0:b1b901ae3696 | 502 | /** Pendable event class |
Christopher Haster |
0:b1b901ae3696 | 503 | */ |
Christopher Haster |
0:b1b901ae3696 | 504 | template <typename A0> |
Christopher Haster |
0:b1b901ae3696 | 505 | class Event<void(A0)> { |
Christopher Haster |
0:b1b901ae3696 | 506 | public: |
Christopher Haster |
0:b1b901ae3696 | 507 | /** Create an event bound to a queue |
Christopher Haster |
0:b1b901ae3696 | 508 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 509 | Event(EventQueue *queue=0, FuncPtr<void(A0)> func=0) { |
Christopher Haster |
14:5abf2ccf2dbf | 510 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 511 | _period = -1; |
Christopher Haster |
14:5abf2ccf2dbf | 512 | attach(queue, func); |
Christopher Haster |
0:b1b901ae3696 | 513 | } |
Christopher Haster |
0:b1b901ae3696 | 514 | |
Christopher Haster |
3:6dccdc36651f | 515 | /** Create an event bound to a queue |
Christopher Haster |
3:6dccdc36651f | 516 | */ |
Christopher Haster |
3:6dccdc36651f | 517 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 518 | Event(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
14:5abf2ccf2dbf | 519 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 520 | _period = -1; |
Christopher Haster |
3:6dccdc36651f | 521 | attach(queue, obj, method); |
Christopher Haster |
3:6dccdc36651f | 522 | } |
Christopher Haster |
3:6dccdc36651f | 523 | |
Christopher Haster |
0:b1b901ae3696 | 524 | /** Attach an event to a queue |
Christopher Haster |
0:b1b901ae3696 | 525 | */ |
Christopher Haster |
13:b84e049b2d9c | 526 | void attach(EventQueue *queue) { |
Christopher Haster |
0:b1b901ae3696 | 527 | _queue = queue; |
Christopher Haster |
13:b84e049b2d9c | 528 | } |
Christopher Haster |
13:b84e049b2d9c | 529 | |
Christopher Haster |
13:b84e049b2d9c | 530 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 531 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 532 | void attach(FuncPtr<void(A0)> func) { |
Christopher Haster |
14:5abf2ccf2dbf | 533 | _func.attach(func); |
Christopher Haster |
0:b1b901ae3696 | 534 | } |
Christopher Haster |
0:b1b901ae3696 | 535 | |
Christopher Haster |
13:b84e049b2d9c | 536 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 537 | */ |
Christopher Haster |
13:b84e049b2d9c | 538 | template <typename T, typename M> |
Christopher Haster |
13:b84e049b2d9c | 539 | void attach(T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 540 | attach(FuncPtr<void(A0)>(obj, method)); |
Christopher Haster |
13:b84e049b2d9c | 541 | } |
Christopher Haster |
13:b84e049b2d9c | 542 | |
Christopher Haster |
14:5abf2ccf2dbf | 543 | /** Attach a func to an event |
Christopher Haster |
13:b84e049b2d9c | 544 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 545 | void attach(EventQueue *queue, FuncPtr<void(A0)> func) { |
Christopher Haster |
13:b84e049b2d9c | 546 | attach(queue); |
Christopher Haster |
14:5abf2ccf2dbf | 547 | attach(func); |
Christopher Haster |
13:b84e049b2d9c | 548 | } |
Christopher Haster |
13:b84e049b2d9c | 549 | |
Christopher Haster |
3:6dccdc36651f | 550 | /** Attach an event to a queue |
Christopher Haster |
3:6dccdc36651f | 551 | */ |
Christopher Haster |
3:6dccdc36651f | 552 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 553 | void attach(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 554 | attach(queue); |
Christopher Haster |
13:b84e049b2d9c | 555 | attach(obj, method); |
Christopher Haster |
3:6dccdc36651f | 556 | } |
Christopher Haster |
3:6dccdc36651f | 557 | |
Christopher Haster |
2:11cda6bead99 | 558 | /** Set delay for when the event is dispatched after it is posted |
Christopher Haster |
2:11cda6bead99 | 559 | * @param ms Delay in milliseconds |
Christopher Haster |
2:11cda6bead99 | 560 | */ |
Christopher Haster |
2:11cda6bead99 | 561 | void delay(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 562 | _delay = ms; |
Christopher Haster |
2:11cda6bead99 | 563 | } |
Christopher Haster |
2:11cda6bead99 | 564 | |
Christopher Haster |
2:11cda6bead99 | 565 | /** Set event to repeat periodically after it is posted |
Christopher Haster |
12:1feb78280125 | 566 | * @param ms Period in milliseconds |
Christopher Haster |
2:11cda6bead99 | 567 | */ |
Christopher Haster |
2:11cda6bead99 | 568 | void period(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 569 | _period = ms; |
Christopher Haster |
2:11cda6bead99 | 570 | } |
Christopher Haster |
2:11cda6bead99 | 571 | |
Christopher Haster |
2:11cda6bead99 | 572 | /** Set tolerance hint on when the event must be called, defaults to 0 |
Christopher Haster |
12:1feb78280125 | 573 | * @param ms Tolerance in milliseconds |
Christopher Haster |
2:11cda6bead99 | 574 | */ |
Christopher Haster |
2:11cda6bead99 | 575 | void tolerance(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 576 | _tolerance = ms; |
Christopher Haster |
2:11cda6bead99 | 577 | } |
Christopher Haster |
2:11cda6bead99 | 578 | |
Christopher Haster |
2:11cda6bead99 | 579 | /** Post the event onto the bound queue |
Christopher Haster |
14:5abf2ccf2dbf | 580 | * @param ms Max time to wait if memory is not available in milliseconds |
Christopher Haster |
14:5abf2ccf2dbf | 581 | * @return True if the event was posted successfully |
Christopher Haster |
0:b1b901ae3696 | 582 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 583 | bool trigger(A0 a0, int ms=-1) { |
Christopher Haster |
17:6d564266850e | 584 | return _queue->event_trigger( |
Christopher Haster |
14:5abf2ccf2dbf | 585 | Binder<void(A0),A0>(_func,a0), |
Christopher Haster |
14:5abf2ccf2dbf | 586 | _delay, _period, _tolerance, ms); |
Christopher Haster |
12:1feb78280125 | 587 | } |
Christopher Haster |
12:1feb78280125 | 588 | |
Christopher Haster |
12:1feb78280125 | 589 | /** Post the event onto the bound queue |
Christopher Haster |
12:1feb78280125 | 590 | */ |
Christopher Haster |
12:1feb78280125 | 591 | void call(A0 a0) { |
Christopher Haster |
12:1feb78280125 | 592 | trigger(a0); |
Christopher Haster |
0:b1b901ae3696 | 593 | } |
Christopher Haster |
0:b1b901ae3696 | 594 | |
Christopher Haster |
2:11cda6bead99 | 595 | /** Post the event onto the bound queue |
Christopher Haster |
0:b1b901ae3696 | 596 | */ |
Christopher Haster |
0:b1b901ae3696 | 597 | void operator()(A0 a0) { |
Christopher Haster |
0:b1b901ae3696 | 598 | return call(a0); |
Christopher Haster |
0:b1b901ae3696 | 599 | } |
Christopher Haster |
0:b1b901ae3696 | 600 | |
Christopher Haster |
0:b1b901ae3696 | 601 | /** Test if event has been bound |
Christopher Haster |
0:b1b901ae3696 | 602 | */ |
Christopher Haster |
0:b1b901ae3696 | 603 | operator bool() const { |
Christopher Haster |
14:5abf2ccf2dbf | 604 | return _func && _queue; |
Christopher Haster |
0:b1b901ae3696 | 605 | } |
Christopher Haster |
0:b1b901ae3696 | 606 | |
Christopher Haster |
0:b1b901ae3696 | 607 | /** Static thunk for passing as C-style function |
Christopher Haster |
0:b1b901ae3696 | 608 | * @param data Event to dispatch passed as void pointer |
Christopher Haster |
0:b1b901ae3696 | 609 | */ |
Christopher Haster |
0:b1b901ae3696 | 610 | static void thunk(void *data, A0 a0) { |
Christopher Haster |
0:b1b901ae3696 | 611 | return static_cast<Event<void(A0)>*>(data) |
Christopher Haster |
0:b1b901ae3696 | 612 | ->call(a0); |
Christopher Haster |
0:b1b901ae3696 | 613 | } |
Christopher Haster |
0:b1b901ae3696 | 614 | |
Christopher Haster |
0:b1b901ae3696 | 615 | private: |
Christopher Haster |
14:5abf2ccf2dbf | 616 | FuncPtr<void(A0)> _func; |
Christopher Haster |
0:b1b901ae3696 | 617 | EventQueue *_queue; |
Christopher Haster |
14:5abf2ccf2dbf | 618 | int _delay; |
Christopher Haster |
14:5abf2ccf2dbf | 619 | int _period; |
Christopher Haster |
14:5abf2ccf2dbf | 620 | int _tolerance; |
Christopher Haster |
0:b1b901ae3696 | 621 | }; |
Christopher Haster |
0:b1b901ae3696 | 622 | |
Christopher Haster |
0:b1b901ae3696 | 623 | /** Pendable event class |
Christopher Haster |
0:b1b901ae3696 | 624 | */ |
Christopher Haster |
0:b1b901ae3696 | 625 | template <> |
Christopher Haster |
0:b1b901ae3696 | 626 | class Event<void()> { |
Christopher Haster |
0:b1b901ae3696 | 627 | public: |
Christopher Haster |
0:b1b901ae3696 | 628 | /** Create an event bound to a queue |
Christopher Haster |
0:b1b901ae3696 | 629 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 630 | Event(EventQueue *queue=0, FuncPtr<void()> func=0) { |
Christopher Haster |
14:5abf2ccf2dbf | 631 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 632 | _period = -1; |
Christopher Haster |
14:5abf2ccf2dbf | 633 | attach(queue, func); |
Christopher Haster |
0:b1b901ae3696 | 634 | } |
Christopher Haster |
0:b1b901ae3696 | 635 | |
Christopher Haster |
3:6dccdc36651f | 636 | /** Create an event bound to a queue |
Christopher Haster |
3:6dccdc36651f | 637 | */ |
Christopher Haster |
3:6dccdc36651f | 638 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 639 | Event(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
14:5abf2ccf2dbf | 640 | _delay = 0; |
Christopher Haster |
14:5abf2ccf2dbf | 641 | _period = -1; |
Christopher Haster |
3:6dccdc36651f | 642 | attach(queue, obj, method); |
Christopher Haster |
3:6dccdc36651f | 643 | } |
Christopher Haster |
3:6dccdc36651f | 644 | |
Christopher Haster |
0:b1b901ae3696 | 645 | /** Attach an event to a queue |
Christopher Haster |
0:b1b901ae3696 | 646 | */ |
Christopher Haster |
13:b84e049b2d9c | 647 | void attach(EventQueue *queue) { |
Christopher Haster |
0:b1b901ae3696 | 648 | _queue = queue; |
Christopher Haster |
13:b84e049b2d9c | 649 | } |
Christopher Haster |
13:b84e049b2d9c | 650 | |
Christopher Haster |
13:b84e049b2d9c | 651 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 652 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 653 | void attach(FuncPtr<void()> func) { |
Christopher Haster |
14:5abf2ccf2dbf | 654 | _func.attach(func); |
Christopher Haster |
0:b1b901ae3696 | 655 | } |
Christopher Haster |
0:b1b901ae3696 | 656 | |
Christopher Haster |
13:b84e049b2d9c | 657 | /** Attach a callback to an event |
Christopher Haster |
13:b84e049b2d9c | 658 | */ |
Christopher Haster |
13:b84e049b2d9c | 659 | template <typename T, typename M> |
Christopher Haster |
13:b84e049b2d9c | 660 | void attach(T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 661 | attach(FuncPtr<void()>(obj, method)); |
Christopher Haster |
13:b84e049b2d9c | 662 | } |
Christopher Haster |
13:b84e049b2d9c | 663 | |
Christopher Haster |
14:5abf2ccf2dbf | 664 | /** Attach a func to an event |
Christopher Haster |
13:b84e049b2d9c | 665 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 666 | void attach(EventQueue *queue, FuncPtr<void()> func) { |
Christopher Haster |
13:b84e049b2d9c | 667 | attach(queue); |
Christopher Haster |
14:5abf2ccf2dbf | 668 | attach(func); |
Christopher Haster |
13:b84e049b2d9c | 669 | } |
Christopher Haster |
13:b84e049b2d9c | 670 | |
Christopher Haster |
3:6dccdc36651f | 671 | /** Attach an event to a queue |
Christopher Haster |
3:6dccdc36651f | 672 | */ |
Christopher Haster |
3:6dccdc36651f | 673 | template <typename T, typename M> |
Christopher Haster |
3:6dccdc36651f | 674 | void attach(EventQueue *queue, T *obj, M method) { |
Christopher Haster |
13:b84e049b2d9c | 675 | attach(queue); |
Christopher Haster |
13:b84e049b2d9c | 676 | attach(obj, method); |
Christopher Haster |
3:6dccdc36651f | 677 | } |
Christopher Haster |
3:6dccdc36651f | 678 | |
Christopher Haster |
2:11cda6bead99 | 679 | /** Set delay for when the event is dispatched after it is posted |
Christopher Haster |
2:11cda6bead99 | 680 | * @param ms Delay in milliseconds |
Christopher Haster |
2:11cda6bead99 | 681 | */ |
Christopher Haster |
2:11cda6bead99 | 682 | void delay(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 683 | _delay = ms; |
Christopher Haster |
2:11cda6bead99 | 684 | } |
Christopher Haster |
2:11cda6bead99 | 685 | |
Christopher Haster |
2:11cda6bead99 | 686 | /** Set event to repeat periodically after it is posted |
Christopher Haster |
12:1feb78280125 | 687 | * @param ms Period in milliseconds |
Christopher Haster |
2:11cda6bead99 | 688 | */ |
Christopher Haster |
2:11cda6bead99 | 689 | void period(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 690 | _period = ms; |
Christopher Haster |
2:11cda6bead99 | 691 | } |
Christopher Haster |
2:11cda6bead99 | 692 | |
Christopher Haster |
2:11cda6bead99 | 693 | /** Set tolerance hint on when the event must be called, defaults to 0 |
Christopher Haster |
12:1feb78280125 | 694 | * @param ms Tolerance in milliseconds |
Christopher Haster |
2:11cda6bead99 | 695 | */ |
Christopher Haster |
2:11cda6bead99 | 696 | void tolerance(int ms) { |
Christopher Haster |
14:5abf2ccf2dbf | 697 | _tolerance = ms; |
Christopher Haster |
2:11cda6bead99 | 698 | } |
Christopher Haster |
2:11cda6bead99 | 699 | |
Christopher Haster |
2:11cda6bead99 | 700 | /** Post the event onto the bound queue |
Christopher Haster |
14:5abf2ccf2dbf | 701 | * @param ms Max time to wait if memory is not available in milliseconds |
Christopher Haster |
14:5abf2ccf2dbf | 702 | * @return True if the event was posted successfully |
Christopher Haster |
0:b1b901ae3696 | 703 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 704 | bool trigger(int ms=-1) { |
Christopher Haster |
17:6d564266850e | 705 | return _queue->event_trigger(_func, |
Christopher Haster |
17:6d564266850e | 706 | _delay, _period, _tolerance, ms); |
Christopher Haster |
12:1feb78280125 | 707 | } |
Christopher Haster |
12:1feb78280125 | 708 | |
Christopher Haster |
12:1feb78280125 | 709 | /** Post the event onto the bound queue |
Christopher Haster |
12:1feb78280125 | 710 | */ |
Christopher Haster |
12:1feb78280125 | 711 | void call() { |
Christopher Haster |
12:1feb78280125 | 712 | trigger(); |
Christopher Haster |
0:b1b901ae3696 | 713 | } |
Christopher Haster |
0:b1b901ae3696 | 714 | |
Christopher Haster |
2:11cda6bead99 | 715 | /** Post the event onto the bound queue |
Christopher Haster |
0:b1b901ae3696 | 716 | */ |
Christopher Haster |
0:b1b901ae3696 | 717 | void operator()() { |
Christopher Haster |
0:b1b901ae3696 | 718 | return call(); |
Christopher Haster |
0:b1b901ae3696 | 719 | } |
Christopher Haster |
0:b1b901ae3696 | 720 | |
Christopher Haster |
0:b1b901ae3696 | 721 | /** Test if event has been bound |
Christopher Haster |
0:b1b901ae3696 | 722 | */ |
Christopher Haster |
0:b1b901ae3696 | 723 | operator bool() const { |
Christopher Haster |
14:5abf2ccf2dbf | 724 | return _func && _queue; |
Christopher Haster |
0:b1b901ae3696 | 725 | } |
Christopher Haster |
0:b1b901ae3696 | 726 | |
Christopher Haster |
0:b1b901ae3696 | 727 | /** Static thunk for passing as C-style function |
Christopher Haster |
0:b1b901ae3696 | 728 | * @param data Event to dispatch passed as void pointer |
Christopher Haster |
0:b1b901ae3696 | 729 | */ |
Christopher Haster |
0:b1b901ae3696 | 730 | static void thunk(void *data) { |
Christopher Haster |
0:b1b901ae3696 | 731 | return static_cast<Event<void()>*>(data) |
Christopher Haster |
0:b1b901ae3696 | 732 | ->call(); |
Christopher Haster |
0:b1b901ae3696 | 733 | } |
Christopher Haster |
0:b1b901ae3696 | 734 | |
Christopher Haster |
0:b1b901ae3696 | 735 | private: |
Christopher Haster |
14:5abf2ccf2dbf | 736 | FuncPtr<void()> _func; |
Christopher Haster |
0:b1b901ae3696 | 737 | EventQueue *_queue; |
Christopher Haster |
14:5abf2ccf2dbf | 738 | int _delay; |
Christopher Haster |
14:5abf2ccf2dbf | 739 | int _period; |
Christopher Haster |
14:5abf2ccf2dbf | 740 | int _tolerance; |
Christopher Haster |
0:b1b901ae3696 | 741 | }; |
Christopher Haster |
0:b1b901ae3696 | 742 | |
Christopher Haster |
0:b1b901ae3696 | 743 | |
Christopher Haster |
0:b1b901ae3696 | 744 | #endif |