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
GainSpan Wi-Fi library
The GS1011 is an ultra low power 802.11b wireless module from GainSpan.
see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011 シリーズ用のライブラリです。
解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
GSFunctionPointer.h@27:5802e39041c5, 2013-02-22 (annotated)
- Committer:
- gsfan
- Date:
- Fri Feb 22 01:10:17 2013 +0000
- Revision:
- 27:5802e39041c5
- Parent:
- 26:b347ee3a1087
- Child:
- 29:1c4419512941
fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 26:b347ee3a1087 | 1 | /* mbed Microcontroller Library |
gsfan | 26:b347ee3a1087 | 2 | * Copyright (c) 2006-2012 ARM Limited |
gsfan | 26:b347ee3a1087 | 3 | * |
gsfan | 26:b347ee3a1087 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
gsfan | 26:b347ee3a1087 | 5 | * of this software and associated documentation files (the "Software"), to deal |
gsfan | 26:b347ee3a1087 | 6 | * in the Software without restriction, including without limitation the rights |
gsfan | 26:b347ee3a1087 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
gsfan | 26:b347ee3a1087 | 8 | * copies of the Software, and to permit persons to whom the Software is |
gsfan | 26:b347ee3a1087 | 9 | * furnished to do so, subject to the following conditions: |
gsfan | 26:b347ee3a1087 | 10 | * |
gsfan | 26:b347ee3a1087 | 11 | * The above copyright notice and this permission notice shall be included in |
gsfan | 26:b347ee3a1087 | 12 | * all copies or substantial portions of the Software. |
gsfan | 26:b347ee3a1087 | 13 | * |
gsfan | 26:b347ee3a1087 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
gsfan | 26:b347ee3a1087 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
gsfan | 26:b347ee3a1087 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
gsfan | 26:b347ee3a1087 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
gsfan | 26:b347ee3a1087 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gsfan | 26:b347ee3a1087 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
gsfan | 26:b347ee3a1087 | 20 | * SOFTWARE. |
gsfan | 26:b347ee3a1087 | 21 | */ |
gsfan | 26:b347ee3a1087 | 22 | /* Copyright (C) 2013 gsfan, MIT License |
gsfan | 27:5802e39041c5 | 23 | * modifyed for the GSwifi library |
gsfan | 26:b347ee3a1087 | 24 | */ |
gsfan | 26:b347ee3a1087 | 25 | #ifndef GS_FUNCTIONPOINTER_H |
gsfan | 26:b347ee3a1087 | 26 | #define GS_FUNCTIONPOINTER_H |
gsfan | 26:b347ee3a1087 | 27 | |
gsfan | 26:b347ee3a1087 | 28 | #include <string.h> |
gsfan | 26:b347ee3a1087 | 29 | |
gsfan | 26:b347ee3a1087 | 30 | namespace mbed { |
gsfan | 26:b347ee3a1087 | 31 | |
gsfan | 26:b347ee3a1087 | 32 | /** A class for storing and calling a pointer to a static or member void function |
gsfan | 26:b347ee3a1087 | 33 | */ |
gsfan | 26:b347ee3a1087 | 34 | class GSFunctionPointer { |
gsfan | 26:b347ee3a1087 | 35 | public: |
gsfan | 26:b347ee3a1087 | 36 | |
gsfan | 26:b347ee3a1087 | 37 | /** Create a FunctionPointer, attaching a static function |
gsfan | 26:b347ee3a1087 | 38 | * |
gsfan | 26:b347ee3a1087 | 39 | * @param function The void static function to attach (default is none) |
gsfan | 26:b347ee3a1087 | 40 | */ |
gsfan | 26:b347ee3a1087 | 41 | GSFunctionPointer(void (*function)(int, int) = 0) { |
gsfan | 26:b347ee3a1087 | 42 | attach(function); |
gsfan | 26:b347ee3a1087 | 43 | } |
gsfan | 26:b347ee3a1087 | 44 | |
gsfan | 26:b347ee3a1087 | 45 | /** Create a FunctionPointer, attaching a member function |
gsfan | 26:b347ee3a1087 | 46 | * |
gsfan | 26:b347ee3a1087 | 47 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
gsfan | 26:b347ee3a1087 | 48 | * @param function The address of the void member function to attach |
gsfan | 26:b347ee3a1087 | 49 | */ |
gsfan | 26:b347ee3a1087 | 50 | template<typename T> |
gsfan | 26:b347ee3a1087 | 51 | GSFunctionPointer(T *object, void (T::*member)(int, int)) { |
gsfan | 26:b347ee3a1087 | 52 | attach(object, member); |
gsfan | 26:b347ee3a1087 | 53 | } |
gsfan | 26:b347ee3a1087 | 54 | |
gsfan | 26:b347ee3a1087 | 55 | /** Attach a static function |
gsfan | 26:b347ee3a1087 | 56 | * |
gsfan | 26:b347ee3a1087 | 57 | * @param function The void static function to attach (default is none) |
gsfan | 26:b347ee3a1087 | 58 | */ |
gsfan | 26:b347ee3a1087 | 59 | void attach(void (*function)(int, int) = 0) { |
gsfan | 26:b347ee3a1087 | 60 | _function = function; |
gsfan | 26:b347ee3a1087 | 61 | _membercaller = NULL; |
gsfan | 26:b347ee3a1087 | 62 | } |
gsfan | 26:b347ee3a1087 | 63 | |
gsfan | 26:b347ee3a1087 | 64 | /** Attach a member function |
gsfan | 26:b347ee3a1087 | 65 | * |
gsfan | 26:b347ee3a1087 | 66 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
gsfan | 26:b347ee3a1087 | 67 | * @param function The address of the void member function to attach |
gsfan | 26:b347ee3a1087 | 68 | */ |
gsfan | 26:b347ee3a1087 | 69 | template<typename T> |
gsfan | 26:b347ee3a1087 | 70 | void attach(T *object, void (T::*member)(int, int)) { |
gsfan | 26:b347ee3a1087 | 71 | _object = static_cast<void*>(object); |
gsfan | 26:b347ee3a1087 | 72 | memcpy(_member, (char*)&member, sizeof(member)); |
gsfan | 26:b347ee3a1087 | 73 | _membercaller = &GSFunctionPointer::membercaller<T>; |
gsfan | 26:b347ee3a1087 | 74 | _function = NULL; |
gsfan | 26:b347ee3a1087 | 75 | } |
gsfan | 26:b347ee3a1087 | 76 | |
gsfan | 26:b347ee3a1087 | 77 | /** Call the attached static or member function |
gsfan | 26:b347ee3a1087 | 78 | */ |
gsfan | 26:b347ee3a1087 | 79 | int call(int a, int b) { |
gsfan | 26:b347ee3a1087 | 80 | if (_function != NULL) { |
gsfan | 26:b347ee3a1087 | 81 | _function(a, b); |
gsfan | 26:b347ee3a1087 | 82 | return 0; |
gsfan | 26:b347ee3a1087 | 83 | } else |
gsfan | 26:b347ee3a1087 | 84 | if (_membercaller != NULL) { |
gsfan | 26:b347ee3a1087 | 85 | _membercaller(_object, _member, a, b); |
gsfan | 26:b347ee3a1087 | 86 | return 0; |
gsfan | 26:b347ee3a1087 | 87 | } |
gsfan | 26:b347ee3a1087 | 88 | return -1; |
gsfan | 26:b347ee3a1087 | 89 | } |
gsfan | 26:b347ee3a1087 | 90 | |
gsfan | 26:b347ee3a1087 | 91 | void detach() { |
gsfan | 26:b347ee3a1087 | 92 | _function = NULL; |
gsfan | 26:b347ee3a1087 | 93 | _membercaller = NULL; |
gsfan | 26:b347ee3a1087 | 94 | } |
gsfan | 26:b347ee3a1087 | 95 | |
gsfan | 26:b347ee3a1087 | 96 | GSFunctionPointer &operator= (GSFunctionPointer &gsfp) { |
gsfan | 26:b347ee3a1087 | 97 | _function = gsfp._function; |
gsfan | 26:b347ee3a1087 | 98 | _object = gsfp._object; |
gsfan | 26:b347ee3a1087 | 99 | memcpy(_member, gsfp._member, sizeof(_member)); |
gsfan | 26:b347ee3a1087 | 100 | _membercaller = gsfp._membercaller; |
gsfan | 26:b347ee3a1087 | 101 | return *this; |
gsfan | 26:b347ee3a1087 | 102 | } |
gsfan | 26:b347ee3a1087 | 103 | |
gsfan | 26:b347ee3a1087 | 104 | private: |
gsfan | 26:b347ee3a1087 | 105 | template<typename T> |
gsfan | 26:b347ee3a1087 | 106 | static void membercaller(void *object, char *member, int a, int b) { |
gsfan | 26:b347ee3a1087 | 107 | T* o = static_cast<T*>(object); |
gsfan | 26:b347ee3a1087 | 108 | void (T::*m)(int, int); |
gsfan | 26:b347ee3a1087 | 109 | memcpy((char*)&m, member, sizeof(m)); |
gsfan | 26:b347ee3a1087 | 110 | (o->*m)(a, b); |
gsfan | 26:b347ee3a1087 | 111 | } |
gsfan | 26:b347ee3a1087 | 112 | |
gsfan | 26:b347ee3a1087 | 113 | void (*_function)(int, int); // static function pointer - 0 if none attached |
gsfan | 26:b347ee3a1087 | 114 | void *_object; // object this pointer - 0 if none attached |
gsfan | 26:b347ee3a1087 | 115 | char _member[16]; // raw member function pointer storage - converted back by registered _membercaller |
gsfan | 26:b347ee3a1087 | 116 | void (*_membercaller)(void*, char*, int, int); // registered membercaller function to convert back and call _member on _object |
gsfan | 26:b347ee3a1087 | 117 | }; |
gsfan | 26:b347ee3a1087 | 118 | |
gsfan | 26:b347ee3a1087 | 119 | } // namespace mbed |
gsfan | 26:b347ee3a1087 | 120 | |
gsfan | 26:b347ee3a1087 | 121 | #endif |