|
1 |
| -/* mbed Microcontroller Library |
2 |
| - * Copyright (c) 2006-2013 ARM Limited |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| -#ifndef MBED_FUNCTIONPOINTER_H |
17 |
| -#define MBED_FUNCTIONPOINTER_H |
18 |
| - |
19 |
| -#include <string.h> |
20 |
| - |
21 |
| -namespace mbed { |
22 |
| - |
23 |
| -typedef void (*pvoidf_t)(void); |
24 |
| - |
25 |
| -/** A class for storing and calling a pointer to a static or member void function |
26 |
| - */ |
27 |
| -class FunctionPointer { |
28 |
| -public: |
29 |
| - |
30 |
| - /** Create a FunctionPointer, attaching a static function |
31 |
| - * |
32 |
| - * @param function The void static function to attach (default is none) |
33 |
| - */ |
34 |
| - FunctionPointer(void (*function)(void) = 0); |
35 |
| - |
36 |
| - /** Create a FunctionPointer, attaching a member function |
37 |
| - * |
38 |
| - * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
39 |
| - * @param function The address of the void member function to attach |
40 |
| - */ |
41 |
| - template<typename T> |
42 |
| - FunctionPointer(T *object, void (T::*member)(void)) { |
43 |
| - attach(object, member); |
44 |
| - } |
45 |
| - |
46 |
| - /** Attach a static function |
47 |
| - * |
48 |
| - * @param function The void static function to attach (default is none) |
49 |
| - */ |
50 |
| - void attach(void (*function)(void) = 0); |
51 |
| - |
52 |
| - /** Attach a member function |
53 |
| - * |
54 |
| - * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
55 |
| - * @param function The address of the void member function to attach |
56 |
| - */ |
57 |
| - template<typename T> |
58 |
| - void attach(T *object, void (T::*member)(void)) { |
59 |
| - _object = static_cast<void*>(object); |
60 |
| - memcpy(_member, (char*)&member, sizeof(member)); |
61 |
| - _membercaller = &FunctionPointer::membercaller<T>; |
62 |
| - _function = 0; |
63 |
| - } |
64 |
| - |
65 |
| - /** Call the attached static or member function |
66 |
| - */ |
67 |
| - void call(); |
68 |
| - |
69 |
| - pvoidf_t get_function() const { |
70 |
| - return (pvoidf_t)_function; |
71 |
| - } |
72 |
| - |
73 |
| -#ifdef MBED_OPERATORS |
74 |
| - void operator ()(void); |
75 |
| -#endif |
76 |
| - |
77 |
| -private: |
78 |
| - template<typename T> |
79 |
| - static void membercaller(void *object, char *member) { |
80 |
| - T* o = static_cast<T*>(object); |
81 |
| - void (T::*m)(void); |
82 |
| - memcpy((char*)&m, member, sizeof(m)); |
83 |
| - (o->*m)(); |
84 |
| - } |
85 |
| - |
86 |
| - void (*_function)(void); // static function pointer - 0 if none attached |
87 |
| - void *_object; // object this pointer - 0 if none attached |
88 |
| - char _member[16]; // raw member function pointer storage - converted back by registered _membercaller |
89 |
| - void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object |
90 |
| -}; |
91 |
| - |
92 |
| -} // namespace mbed |
93 |
| - |
94 |
| -#endif |
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2013 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#ifndef MBED_FUNCTIONPOINTER_H |
| 17 | +#define MBED_FUNCTIONPOINTER_H |
| 18 | + |
| 19 | +#include <string.h> |
| 20 | + |
| 21 | +namespace mbed { |
| 22 | + |
| 23 | +typedef void (*pvoidf_t)(void); |
| 24 | + |
| 25 | +/** A class for storing and calling a pointer to a static or member void function |
| 26 | + */ |
| 27 | +class FunctionPointer { |
| 28 | +public: |
| 29 | + |
| 30 | + /** Create a FunctionPointer, attaching a static function |
| 31 | + * |
| 32 | + * @param function The void static function to attach (default is none) |
| 33 | + */ |
| 34 | + FunctionPointer(void (*function)(void) = 0); |
| 35 | + |
| 36 | + /** Create a FunctionPointer, attaching a member function |
| 37 | + * |
| 38 | + * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
| 39 | + * @param function The address of the void member function to attach |
| 40 | + */ |
| 41 | + template<typename T> |
| 42 | + FunctionPointer(T *object, void (T::*member)(void)) { |
| 43 | + attach(object, member); |
| 44 | + } |
| 45 | + |
| 46 | + /** Attach a static function |
| 47 | + * |
| 48 | + * @param function The void static function to attach (default is none) |
| 49 | + */ |
| 50 | + void attach(void (*function)(void) = 0); |
| 51 | + |
| 52 | + /** Attach a member function |
| 53 | + * |
| 54 | + * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
| 55 | + * @param function The address of the void member function to attach |
| 56 | + */ |
| 57 | + template<typename T> |
| 58 | + void attach(T *object, void (T::*member)(void)) { |
| 59 | + _object = static_cast<void*>(object); |
| 60 | + memcpy(_member, (char*)&member, sizeof(member)); |
| 61 | + _membercaller = &FunctionPointer::membercaller<T>; |
| 62 | + _function = 0; |
| 63 | + } |
| 64 | + |
| 65 | + /** Call the attached static or member function |
| 66 | + */ |
| 67 | + void call(); |
| 68 | + |
| 69 | + pvoidf_t get_function() const { |
| 70 | + return (pvoidf_t)_function; |
| 71 | + } |
| 72 | + |
| 73 | +#ifdef MBED_OPERATORS |
| 74 | + void operator ()(void); |
| 75 | +#endif |
| 76 | + |
| 77 | +private: |
| 78 | + template<typename T> |
| 79 | + static void membercaller(void *object, char *member) { |
| 80 | + T* o = static_cast<T*>(object); |
| 81 | + void (T::*m)(void); |
| 82 | + memcpy((char*)&m, member, sizeof(m)); |
| 83 | + (o->*m)(); |
| 84 | + } |
| 85 | + |
| 86 | + void (*_function)(void); // static function pointer - 0 if none attached |
| 87 | + void *_object; // object this pointer - 0 if none attached |
| 88 | + char _member[16]; // raw member function pointer storage - converted back by registered _membercaller |
| 89 | + void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object |
| 90 | +}; |
| 91 | + |
| 92 | +} // namespace mbed |
| 93 | + |
| 94 | +#endif |
0 commit comments