GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Dependents:   GSwifi_httpd GSwifi_websocket GSwifi_tcpclient GSwifi_tcpserver ... more

Fork of GSwifi by gs fan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GSFunctionPointer.h Source File

GSFunctionPointer.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 /* 
00023  * Modifyed for the GSwifi library, by 2013 gsfan
00024  */
00025 #ifndef GS_FUNCTIONPOINTER_H
00026 #define GS_FUNCTIONPOINTER_H
00027 
00028 #include <string.h>
00029 
00030 namespace mbed { 
00031 
00032 /** A class for storing and calling a pointer to a static or member void function
00033  */
00034 class GSFunctionPointer {
00035 public:
00036 
00037     /** Create a FunctionPointer, attaching a static function
00038      * 
00039      *  @param function The void static function to attach (default is none)
00040      */
00041     GSFunctionPointer(void (*function)(int, int) = 0) {
00042         attach(function);
00043     }
00044 
00045     /** Create a FunctionPointer, attaching a member function
00046      * 
00047      *  @param object The object pointer to invoke the member function on (i.e. the this pointer)
00048      *  @param function The address of the void member function to attach 
00049      */
00050     template<typename T>
00051     GSFunctionPointer(T *object, void (T::*member)(int, int)) {
00052         attach(object, member);
00053     }
00054     
00055     /** Attach a static function
00056      * 
00057      *  @param function The void static function to attach (default is none)
00058      */
00059     void attach(void (*function)(int, int) = 0) {
00060         _function = function;
00061         _object = 0;
00062     }
00063     
00064     /** Attach a member function
00065      * 
00066      *  @param object The object pointer to invoke the member function on (i.e. the this pointer)
00067      *  @param function The address of the void member function to attach 
00068      */
00069     template<typename T>
00070     void attach(T *object, void (T::*member)(int, int)) {
00071         _object = static_cast<void*>(object);
00072         memcpy(_member, (char*)&member, sizeof(member));
00073         _membercaller = &GSFunctionPointer::membercaller<T>;
00074         _function = NULL;
00075     }
00076     
00077     /** Call the attached static or member function
00078      */
00079     int call(int a, int b) {
00080         if (_function) {
00081             _function(a, b);
00082             return 0;
00083         } else
00084         if (_object) {
00085             _membercaller(_object, _member, a, b);
00086             return 0;
00087         }
00088         return -1;
00089     }
00090 
00091     void detach() {
00092         _function = NULL;
00093         _object = 0;
00094     }
00095 
00096     GSFunctionPointer &operator= (GSFunctionPointer &gsfp) {
00097         _function = gsfp._function;
00098         _object = gsfp._object;
00099         memcpy(_member, gsfp._member, sizeof(_member));
00100         _membercaller = gsfp._membercaller;
00101         return *this;
00102     }
00103 
00104 private:
00105     template<typename T>
00106     static void membercaller(void *object, char *member, int a, int b) {
00107         T* o = static_cast<T*>(object);
00108         void (T::*m)(int, int);
00109         memcpy((char*)&m, member, sizeof(m));
00110         (o->*m)(a, b);
00111     }
00112     
00113     void (*_function)(int, int);                // static function pointer - 0 if none attached
00114     void *_object;                            // object this pointer - 0 if none attached
00115     char _member[16];                        // raw member function pointer storage - converted back by registered _membercaller
00116     void (*_membercaller)(void*, char*, int, int);    // registered membercaller function to convert back and call _member on _object
00117 };
00118 
00119 } // namespace mbed
00120 
00121 #endif