21 #include "events/equeue.h" 22 #include "platform/Callback.h" 23 #include "platform/NonCopyable.h" 37 #define EVENTS_EVENT_SIZE \ 38 (EQUEUE_EVENT_SIZE - 2*sizeof(void*) + sizeof(mbed::Callback<void()>)) 43 #define EVENTS_QUEUE_SIZE (32*EVENTS_EVENT_SIZE) 48 template <
typename F,
typename A>
170 template<
typename F,
typename A>
173 if (event->_equeue != &_equeue) {
176 return equeue_cancel_user_allocated(&_equeue, event);
216 template<
typename F,
typename A>
219 if (event && event->_equeue != &_equeue) {
222 return equeue_timeleft_user_allocated(&_equeue, &event->_e);
265 #if defined(DOXYGEN_ONLY) 297 template <
typename F,
typename ...Args>
298 int call(F f, Args ...args);
348 template <
typename T,
typename R,
typename ...Args>
349 int call(T *obj, R (T::*method)(Args ...args), Args ...args);
381 template <
typename F,
typename ...Args>
382 int call_in(
int ms, Args ...args);
431 template <
typename T,
typename R,
typename ...Args>
432 int call_in(
int ms, T *obj, R (T::*method)(Args ...args), Args ...args);
478 template <
typename F,
typename ...Args>
528 template <
typename T,
typename R,
typename ...Args>
529 int call_every(
int ms, T *obj, R (T::*method)(Args ...args), Args ...args);
574 template <
typename R,
typename ...BoundArgs,
typename ...ContextArgs,
typename ...Args>
575 Event<void(Args...)>
event(R (*func)(BoundArgs..., Args...), ContextArgs ...context_args);
622 template <
typename T,
typename R,
typename ...BoundArgs,
typename ...ContextArgs,
typename ...Args>
623 Event<void(Args...)>
event(T *obj, R (T::*method)(BoundArgs..., Args...), ContextArgs ...context_args);
661 template <
typename R,
typename ...BoundArgs,
typename ...ContextArgs,
typename ...Args>
701 template <
typename F,
typename... ArgTs>
707 template <
typename T,
typename R,
typename... ArgTs>
744 template <
typename F>
747 void *p = equeue_alloc(&_equeue,
sizeof(F));
753 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
754 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
763 template <
typename F,
typename... ArgTs>
764 int call(F f, ArgTs... args)
766 return call(context<F, ArgTs...>(f, args...));
772 template <
typename T,
typename R,
typename... ArgTs>
773 int call(T *obj, R(T::*method)(ArgTs...), ArgTs... args)
781 template <
typename T,
typename R,
typename... ArgTs>
782 int call(
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
790 template <
typename T,
typename R,
typename... ArgTs>
791 int call(
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
799 template <
typename T,
typename R,
typename... ArgTs>
800 int call(
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
819 template <
typename F>
822 void *p = equeue_alloc(&_equeue,
sizeof(F));
828 equeue_event_delay(e, ms);
829 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
830 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
839 template <
typename F,
typename... ArgTs>
840 int call_in(
int ms, F f, ArgTs... args)
842 return call_in(ms, context<F, ArgTs...>(f, args...));
848 template <
typename T,
typename R,
typename... ArgTs>
849 int call_in(
int ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
857 template <
typename T,
typename R,
typename... ArgTs>
858 int call_in(
int ms,
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
866 template <
typename T,
typename R,
typename... ArgTs>
867 int call_in(
int ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
875 template <
typename T,
typename R,
typename... ArgTs>
876 int call_in(
int ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
898 template <
typename F>
901 void *p = equeue_alloc(&_equeue,
sizeof(F));
907 equeue_event_delay(e, ms);
908 equeue_event_period(e, ms);
909 equeue_event_dtor(e, &EventQueue::function_dtor<F>);
910 return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
919 template <
typename F,
typename... ArgTs>
922 return call_every(ms, context<F, ArgTs...>(f, args...));
928 template <
typename T,
typename R,
typename... ArgTs>
929 int call_every(
int ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args)
937 template <
typename T,
typename R,
typename... ArgTs>
938 int call_every(
int ms,
const T *obj, R(T::*method)(ArgTs...)
const, ArgTs... args)
946 template <
typename T,
typename R,
typename... ArgTs>
947 int call_every(
int ms,
volatile T *obj, R(T::*method)(ArgTs...)
volatile, ArgTs... args)
955 template <
typename T,
typename R,
typename... ArgTs>
956 int call_every(
int ms,
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile, ArgTs... args)
970 template <
typename R,
typename... ArgTs>
971 Event<void(ArgTs...)>
event(R(*func)(ArgTs...));
976 template <
typename T,
typename R,
typename... ArgTs>
977 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(ArgTs...));
982 template <
typename T,
typename R,
typename... ArgTs>
983 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(ArgTs...)
const);
988 template <
typename T,
typename R,
typename... ArgTs>
989 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(ArgTs...)
volatile);
994 template <
typename T,
typename R,
typename... ArgTs>
995 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(ArgTs...)
const volatile);
1000 template <
typename R,
typename... ArgTs>
1006 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
1007 Event<void(ArgTs...)>
event(R(*func)(B0, ArgTs...), C0 c0);
1012 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1013 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, ArgTs...), C0 c0);
1018 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1019 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, ArgTs...)
const, C0 c0);
1024 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1025 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, ArgTs...)
volatile, C0 c0);
1030 template <
typename T,
typename R,
typename B0,
typename C0,
typename... ArgTs>
1031 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, ArgTs...)
const volatile, C0 c0);
1036 template <
typename R,
typename B0,
typename C0,
typename... ArgTs>
1042 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1043 Event<void(ArgTs...)>
event(R(*func)(B0, B1, ArgTs...), C0 c0, C1 c1);
1048 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1049 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, ArgTs...), C0 c0, C1 c1);
1054 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1055 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, ArgTs...)
const, C0 c0, C1 c1);
1060 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1061 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
volatile, C0 c0, C1 c1);
1066 template <
typename T,
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1067 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, ArgTs...)
const volatile, C0 c0, C1 c1);
1072 template <
typename R,
typename B0,
typename B1,
typename C0,
typename C1,
typename... ArgTs>
1078 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1079 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, ArgTs...), C0 c0, C1 c1, C2 c2);
1084 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1085 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, B2, ArgTs...), C0 c0, C1 c1, C2 c2);
1090 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1091 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const, C0 c0, C1 c1, C2 c2);
1096 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1097 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2);
1102 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1103 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2);
1108 template <
typename R,
typename B0,
typename B1,
typename B2,
typename C0,
typename C1,
typename C2,
typename... ArgTs>
1114 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1115 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, B3, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3);
1120 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1121 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3);
1126 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1127 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const, C0 c0, C1 c1, C2 c2, C3 c3);
1132 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1133 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2, C3 c3);
1138 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1139 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2, C3 c3);
1144 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename C0,
typename C1,
typename C2,
typename C3,
typename... ArgTs>
1150 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
1151 Event<void(ArgTs...)>
event(R(*func)(B0, B1, B2, B3, B4, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1156 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
1157 Event<void(ArgTs...)>
event(T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...), C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1162 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
1163 Event<void(ArgTs...)>
event(
const T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
const, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1168 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
1169 Event<void(ArgTs...)>
event(
volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
volatile, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1174 template <
typename T,
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
1175 Event<void(ArgTs...)>
event(
const volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...)
const volatile, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1180 template <
typename R,
typename B0,
typename B1,
typename B2,
typename B3,
typename B4,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4,
typename... ArgTs>
1181 Event<void(ArgTs...)>
event(
mbed::Callback<R(B0, B1, B2, B3, B4, ArgTs...)> cb, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4);
1192 template <
typename F,
typename... ArgTs>
1198 template <
typename T,
typename R,
typename... ArgTs>
1204 template <
typename T,
typename R,
typename... ArgTs>
1210 template <
typename T,
typename R,
typename... ArgTs>
1216 template <
typename T,
typename R,
typename... ArgTs>
1221 #if !defined(DOXYGEN_ONLY) 1222 template <
typename F>
1224 template <
typename F,
typename A>
1230 template <
typename F>
1231 static void function_call(
void *p)
1236 template <
typename F>
1237 static void function_dtor(
void *p)
1243 template <
typename F,
typename... ContextArgTs>
1246 template <
typename F>
1250 constexpr context(F f)
1253 template <
typename... ArgTs>
1254 void operator()(ArgTs... args)
1260 template <
typename F,
typename C0>
1261 struct context<F, C0> {
1265 constexpr context(F f, C0 c0)
1268 template <
typename... ArgTs>
1269 void operator()(ArgTs... args)
1275 template <
typename F,
typename C0,
typename C1>
1276 struct context<F, C0, C1> {
1281 constexpr context(F f, C0 c0, C1 c1)
1282 : f(f), c0(c0), c1(c1) {}
1284 template <
typename... ArgTs>
1285 void operator()(ArgTs... args)
1291 template <
typename F,
typename C0,
typename C1,
typename C2>
1292 struct context<F, C0, C1, C2> {
1298 constexpr context(F f, C0 c0, C1 c1, C2 c2)
1299 : f(f), c0(c0), c1(c1), c2(c2) {}
1301 template <
typename... ArgTs>
1302 void operator()(ArgTs... args)
1304 f(c0, c1, c2, args...);
1308 template <
typename F,
typename C0,
typename C1,
typename C2,
typename C3>
1309 struct context<F, C0, C1, C2, C3> {
1316 constexpr context(F f, C0 c0, C1 c1, C2 c2, C3 c3)
1317 : f(f), c0(c0), c1(c1), c2(c2), c3(c3) {}
1319 template <
typename... ArgTs>
1320 void operator()(ArgTs... args)
1322 f(c0, c1, c2, c3, args...);
1326 template <
typename F,
typename C0,
typename C1,
typename C2,
typename C3,
typename C4>
1327 struct context<F, C0, C1, C2, C3, C4> {
1335 constexpr context(F f, C0 c0, C1 c1, C2 c2, C3 c3, C4 c4)
1336 : f(f), c0(c0), c1(c1), c2(c2), c3(c3), c4(c4) {}
1338 template <
typename... ArgTs>
1339 void operator()(ArgTs... args)
1341 f(c0, c1, c2, c3, c4, args...);
void dispatch_forever()
Dispatch events without a timeout.
int time_left(int id)
Query how much time is left for delayed event.
void break_dispatch()
Break out of a running event loop.
int chain(EventQueue *target)
Chain an event queue onto another event queue.
bool cancel(UserAllocatedEvent< F, A > *event)
Cancel an in-flight user allocated event.
Prevents generation of copy constructor and copy assignment operator in derived classes.
UserAllocatedEvent< F, void(ArgTs...)> make_user_allocated_event(F f, ArgTs...args)
Creates an user allocated event bound to the event queue.
unsigned tick()
Millisecond counter.
bool cancel(int id)
Cancel an in-flight event.
void background(mbed::Callback< void(int)> update)
Background an event queue onto a single-shot timer-interrupt.
int call(F f, Args...args)
Calls an event on the queue.
Event< void(Args...)> event(R(*func)(BoundArgs..., Args...), ContextArgs...context_args)
Creates an event bound to the event queue.
~EventQueue()
Destroy an EventQueue.
EventQueue(unsigned size=(32 *(EQUEUE_EVENT_SIZE-2 *sizeof(void *)+sizeof(mbed::Callback< void()>))), unsigned char *buffer=NULL)
Create an EventQueue.
int time_left(UserAllocatedEvent< F, A > *event)
Query how much time is left for delayed UserAllocatedEvent.
Callback class based on template specialization.
int call_every(int ms, F f, Args...args)
Calls an event on the queue periodically.
int call_in(int ms, Args...args)
Calls an event on the queue after a specified delay.
#define EVENTS_QUEUE_SIZE
EVENTS_QUEUE_SIZE Default size of buffer for events.
void dispatch(int ms=-1)
Dispatch events.