Host library for controlling a WiConnect enabled Wi-Fi module.

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

Revision:
0:ea85c4bb5e1f
Child:
1:6ec9998427ad
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/types/Callback.h	Mon Aug 11 09:58:24 2014 +0000
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2014, ACKme Networks
+ * All Rights Reserved.
+ *
+ * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks;
+ * the contents of this file may not be disclosed to third parties, copied
+ * or duplicated in any form, in whole or in part, without the prior
+ * written permission of ACKme Networks.
+ */
+
+#pragma once
+
+
+#include "WiconnectTypes.h"
+#include "FunctionPointer.h"
+
+
+
+namespace wiconnect
+{
+
+
+typedef void (*_Callback)(WiconnectResult result, void *arg1, void *arg2);
+
+class Callback : public FunctionPointer
+{
+public:
+    /*************************************************************************************************/
+    Callback(_Callback func = 0)
+    {
+        _function = (void*)func;
+        _membercaller = NULL;
+        _object = NULL;
+    }
+
+    /*************************************************************************************************/
+    template<typename T>
+    Callback(T *object, void (T::*member)(WiconnectResult result, void *arg1, void *arg2))
+    {
+        _object = static_cast<void*>(object);
+        memcpy(_member, (char*)&member, sizeof(member));
+        _membercaller = (void*)&Callback::membercaller<T>;
+        _function = 0;
+    }
+
+    /*************************************************************************************************/
+    void call(WiconnectResult result, void *arg1, void *arg2)
+    {
+        if (_function)
+        {
+            ((_Callback)_function)(result, arg1, arg2);
+        }
+        else if (_object)
+        {
+            typedef void (*membercallerFunc)(void*, char*, WiconnectResult result, void *arg1, void *arg2);
+            ((membercallerFunc)_membercaller)(_object, _member, result, arg1, arg2);
+        }
+    }
+
+private:
+
+    /*************************************************************************************************/
+    template<typename T>
+    static void membercaller(void *object, char *member, WiconnectResult result, void *arg1, void *arg2)
+    {
+        T* o = static_cast<T*>(object);
+        void (T::*m)(WiconnectResult result, void *arg1, void *arg2);
+        memcpy((char*)&m, member, sizeof(m));
+        (o->*m)(result, arg1, arg2);
+    }
+
+};
+
+
+}
+