a implementation of template class that is used for create event-driven task thread.

Revision:
0:1f4516e81c1b
Child:
1:7d11951c1fc0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Actor.h	Wed May 20 08:11:04 2015 +0000
@@ -0,0 +1,46 @@
+#ifndef ACTOR_H
+#define ACTOR_H
+
+#include <cstdarg>
+#include <map>
+#include "mbed.h"
+#include "rtos.h"
+
+#define MAX_MAIL_NUM    255
+#define STACK_SIZE      1024 * 4
+#define START_THREAD    -1
+
+typedef void (*Routine)(void const *argument);
+typedef uint32_t MessageID;
+
+struct MailPacket {
+    uint32_t messageId;
+    void *packet;
+};
+
+typedef Mail<MailPacket, MAX_MAIL_NUM> MailBox;
+typedef std::map<MessageID, void(*)(void *)> JobList;
+
+class Actor
+{
+public:
+    Actor();
+    virtual ~Actor();
+
+    virtual void subscribe(MessageID msg, void(*task)(void *));
+
+    virtual void unsubscribe(MessageID msg);
+
+    static void sendMail(Actor *dest, MessageID msg, void *pkt);
+
+protected:
+    virtual void threadMain();
+
+    static void threadKicker(void const *p);
+
+    Thread thread;
+    MailBox mbox;
+    JobList joblist;
+};
+
+#endif