A library to send and receive packets over serial, uses MODSERIAL

Dependents:   SimpleSerialProtocolExample SerialFileReceiver

Revision:
0:1639507580d5
Child:
1:98ad30934a71
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FPointer.h	Thu Jun 28 21:19:58 2012 +0000
@@ -0,0 +1,97 @@
+/*
+    Copyright (c) 2011 Andy Kirkham
+
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the "Software"), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE.
+*/
+
+#ifndef AJK_FPOINTER_H
+#define AJK_FPOINTER_H
+
+#include "Packet.h"
+
+namespace SimpleSerialProtocol {
+
+class FPointerDummy;
+
+typedef Packet* Packet_ptr;
+
+class FPointer {
+
+protected:
+
+    //! C callback function pointer.
+    void (*c_callback)(Packet_ptr);
+
+    //! C++ callback object/method pointer (the object part).
+    FPointerDummy  *obj_callback;
+
+    //! C++ callback object/method pointer (the method part).
+    void (FPointerDummy::*method_callback)(Packet_ptr);
+
+public:
+
+    /** Constructor
+     */
+    FPointer() {
+        c_callback      = NULL;
+        obj_callback    = NULL;
+        method_callback = NULL;
+    }
+
+
+    void attach(void (*function)(Packet_ptr) = 0) {
+        c_callback = function;
+    }
+
+    template<class T>
+    void attach(T* item, void (T::*method)(Packet_ptr)) {
+        obj_callback    = (FPointerDummy *)item;
+        method_callback = (void (FPointerDummy::*)(Packet_ptr))method;
+    }
+
+    void call(Packet_ptr arg) {
+        if (c_callback != NULL) {
+            return (*c_callback)(arg);
+        } else {
+            if (obj_callback  != NULL && method_callback != NULL) {
+                (obj_callback->*method_callback)(arg);
+                return;
+            }
+        }
+        return;
+    }
+
+    void call(void) {
+        if (c_callback != NULL) {
+            (*c_callback)((Packet_ptr)NULL);
+            return;
+        } else {
+            if (obj_callback  != NULL && method_callback != NULL) {
+                (obj_callback->*method_callback)((Packet_ptr)NULL);
+                return;
+            }
+        }
+        return;
+    }
+};
+
+};
+
+
+#endif