Note! This project has moved to github.com/armmbed/mbed-events

Dependents:   SimpleHTTPExample

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.

Revision:
13:b84e049b2d9c
Parent:
12:1feb78280125
Child:
14:5abf2ccf2dbf
--- a/Event.h	Fri Apr 22 22:18:55 2016 -0500
+++ b/Event.h	Fri Apr 22 22:32:35 2016 -0500
@@ -20,17 +20,9 @@
 template <typename A0, typename A1, typename A2, typename A3, typename A4>
 class Event<void(A0, A1, A2, A3, A4)> {
 public:
-    /** Create an unbound event
-     */
-    Event() {
-        memset(&_event, 0, sizeof _event);
-        _event.delay = 0;
-        _event.period = -1;
-    }
-
     /** Create an event bound to a queue
      */
-    Event(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3, A4)> callback) {
+    Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2, A3, A4)> callback=0) {
         memset(&_event, 0, sizeof _event);
         _event.delay = 0;
         _event.period = -1;
@@ -55,17 +47,36 @@
 
     /** Attach an event to a queue
      */
-    void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3, A4)> callback) {
+    void attach(EventQueue *queue) {
         _queue = queue;
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(FuncPtr<void(A0, A1, A2, A3, A4)> callback) {
         _callback.attach(callback);
     }
 
+    /** Attach a callback to an event
+     */
+    template <typename T, typename M>
+    void attach(T *obj, M method) {
+        attach(FuncPtr<void(A0, A1, A2, A3, A4)>(obj, method));
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3, A4)> callback) {
+        attach(queue);
+        attach(callback);
+    }
+
     /** Attach an event to a queue
      */
     template <typename T, typename M>
     void attach(EventQueue *queue, T *obj, M method) {
-        _queue = queue;
-        _callback.attach(FuncPtr<void(A0, A1, A2, A3, A4)>(obj, method));
+        attach(queue);
+        attach(obj, method);
     }
 
     /** Set delay for when the event is dispatched after it is posted
@@ -148,17 +159,9 @@
 template <typename A0, typename A1, typename A2, typename A3>
 class Event<void(A0, A1, A2, A3)> {
 public:
-    /** Create an unbound event
-     */
-    Event() {
-        memset(&_event, 0, sizeof _event);
-        _event.delay = 0;
-        _event.period = -1;
-    }
-
     /** Create an event bound to a queue
      */
-    Event(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3)> callback) {
+    Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2, A3)> callback=0) {
         memset(&_event, 0, sizeof _event);
         _event.delay = 0;
         _event.period = -1;
@@ -183,17 +186,36 @@
 
     /** Attach an event to a queue
      */
-    void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3)> callback) {
+    void attach(EventQueue *queue) {
         _queue = queue;
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(FuncPtr<void(A0, A1, A2, A3)> callback) {
         _callback.attach(callback);
     }
 
+    /** Attach a callback to an event
+     */
+    template <typename T, typename M>
+    void attach(T *obj, M method) {
+        attach(FuncPtr<void(A0, A1, A2, A3)>(obj, method));
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3)> callback) {
+        attach(queue);
+        attach(callback);
+    }
+
     /** Attach an event to a queue
      */
     template <typename T, typename M>
     void attach(EventQueue *queue, T *obj, M method) {
-        _queue = queue;
-        _callback.attach(FuncPtr<void(A0, A1, A2, A3)>(obj, method));
+        attach(queue);
+        attach(obj, method);
     }
 
     /** Set delay for when the event is dispatched after it is posted
@@ -275,17 +297,9 @@
 template <typename A0, typename A1, typename A2>
 class Event<void(A0, A1, A2)> {
 public:
-    /** Create an unbound event
-     */
-    Event() {
-        memset(&_event, 0, sizeof _event);
-        _event.delay = 0;
-        _event.period = -1;
-    }
-
     /** Create an event bound to a queue
      */
-    Event(EventQueue *queue, FuncPtr<void(A0, A1, A2)> callback) {
+    Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2)> callback=0) {
         memset(&_event, 0, sizeof _event);
         _event.delay = 0;
         _event.period = -1;
@@ -310,17 +324,36 @@
 
     /** Attach an event to a queue
      */
-    void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2)> callback) {
+    void attach(EventQueue *queue) {
         _queue = queue;
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(FuncPtr<void(A0, A1, A2)> callback) {
         _callback.attach(callback);
     }
 
+    /** Attach a callback to an event
+     */
+    template <typename T, typename M>
+    void attach(T *obj, M method) {
+        attach(FuncPtr<void(A0, A1, A2)>(obj, method));
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2)> callback) {
+        attach(queue);
+        attach(callback);
+    }
+
     /** Attach an event to a queue
      */
     template <typename T, typename M>
     void attach(EventQueue *queue, T *obj, M method) {
-        _queue = queue;
-        _callback.attach(FuncPtr<void(A0, A1, A2)>(obj, method));
+        attach(queue);
+        attach(obj, method);
     }
 
     /** Set delay for when the event is dispatched after it is posted
@@ -402,17 +435,9 @@
 template <typename A0, typename A1>
 class Event<void(A0, A1)> {
 public:
-    /** Create an unbound event
-     */
-    Event() {
-        memset(&_event, 0, sizeof _event);
-        _event.delay = 0;
-        _event.period = -1;
-    }
-
     /** Create an event bound to a queue
      */
-    Event(EventQueue *queue, FuncPtr<void(A0, A1)> callback) {
+    Event(EventQueue *queue=0, FuncPtr<void(A0, A1)> callback=0) {
         memset(&_event, 0, sizeof _event);
         _event.delay = 0;
         _event.period = -1;
@@ -437,17 +462,36 @@
 
     /** Attach an event to a queue
      */
-    void attach(EventQueue *queue, FuncPtr<void(A0, A1)> callback) {
+    void attach(EventQueue *queue) {
         _queue = queue;
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(FuncPtr<void(A0, A1)> callback) {
         _callback.attach(callback);
     }
 
+    /** Attach a callback to an event
+     */
+    template <typename T, typename M>
+    void attach(T *obj, M method) {
+        attach(FuncPtr<void(A0, A1)>(obj, method));
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(EventQueue *queue, FuncPtr<void(A0, A1)> callback) {
+        attach(queue);
+        attach(callback);
+    }
+
     /** Attach an event to a queue
      */
     template <typename T, typename M>
     void attach(EventQueue *queue, T *obj, M method) {
-        _queue = queue;
-        _callback.attach(FuncPtr<void(A0, A1)>(obj, method));
+        attach(queue);
+        attach(obj, method);
     }
 
     /** Set delay for when the event is dispatched after it is posted
@@ -529,17 +573,9 @@
 template <typename A0>
 class Event<void(A0)> {
 public:
-    /** Create an unbound event
-     */
-    Event() {
-        memset(&_event, 0, sizeof _event);
-        _event.delay = 0;
-        _event.period = -1;
-    }
-
     /** Create an event bound to a queue
      */
-    Event(EventQueue *queue, FuncPtr<void(A0)> callback) {
+    Event(EventQueue *queue=0, FuncPtr<void(A0)> callback=0) {
         memset(&_event, 0, sizeof _event);
         _event.delay = 0;
         _event.period = -1;
@@ -564,17 +600,36 @@
 
     /** Attach an event to a queue
      */
-    void attach(EventQueue *queue, FuncPtr<void(A0)> callback) {
+    void attach(EventQueue *queue) {
         _queue = queue;
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(FuncPtr<void(A0)> callback) {
         _callback.attach(callback);
     }
 
+    /** Attach a callback to an event
+     */
+    template <typename T, typename M>
+    void attach(T *obj, M method) {
+        attach(FuncPtr<void(A0)>(obj, method));
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(EventQueue *queue, FuncPtr<void(A0)> callback) {
+        attach(queue);
+        attach(callback);
+    }
+
     /** Attach an event to a queue
      */
     template <typename T, typename M>
     void attach(EventQueue *queue, T *obj, M method) {
-        _queue = queue;
-        _callback.attach(FuncPtr<void(A0)>(obj, method));
+        attach(queue);
+        attach(obj, method);
     }
 
     /** Set delay for when the event is dispatched after it is posted
@@ -666,7 +721,7 @@
 
     /** Create an event bound to a queue
      */
-    Event(EventQueue *queue, FuncPtr<void()> callback) {
+    Event(EventQueue *queue=0, FuncPtr<void()> callback=0) {
         memset(&_event, 0, sizeof _event);
         _event.delay = 0;
         _event.period = -1;
@@ -691,17 +746,36 @@
 
     /** Attach an event to a queue
      */
-    void attach(EventQueue *queue, FuncPtr<void()> callback) {
+    void attach(EventQueue *queue) {
         _queue = queue;
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(FuncPtr<void()> callback) {
         _callback.attach(callback);
     }
 
+    /** Attach a callback to an event
+     */
+    template <typename T, typename M>
+    void attach(T *obj, M method) {
+        attach(FuncPtr<void()>(obj, method));
+    }
+
+    /** Attach a callback to an event
+     */
+    void attach(EventQueue *queue, FuncPtr<void()> callback) {
+        attach(queue);
+        attach(callback);
+    }
+
     /** Attach an event to a queue
      */
     template <typename T, typename M>
     void attach(EventQueue *queue, T *obj, M method) {
-        _queue = queue;
-        _callback.attach(FuncPtr<void()>(obj, method));
+        attach(queue);
+        attach(obj, method);
     }
 
     /** Set delay for when the event is dispatched after it is posted