Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
FunctionPointer.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_FUNCTIONPOINTER_H 00017 #define MBED_FUNCTIONPOINTER_H 00018 00019 #include <string.h> 00020 00021 namespace mbed { 00022 00023 typedef void (*pvoidf_t)(void); 00024 00025 /** A class for storing and calling a pointer to a static or member void function 00026 */ 00027 class FunctionPointer { 00028 public: 00029 00030 /** Create a FunctionPointer, attaching a static function 00031 * 00032 * @param function The void static function to attach (default is none) 00033 */ 00034 FunctionPointer(void (*function)(void) = 0); 00035 00036 /** Create a FunctionPointer, attaching a member function 00037 * 00038 * @param object The object pointer to invoke the member function on (i.e. the this pointer) 00039 * @param function The address of the void member function to attach 00040 */ 00041 template<typename T> 00042 FunctionPointer(T *object, void (T::*member)(void)) { 00043 attach(object, member); 00044 } 00045 00046 /** Attach a static function 00047 * 00048 * @param function The void static function to attach (default is none) 00049 */ 00050 void attach(void (*function)(void) = 0); 00051 00052 /** Attach a member function 00053 * 00054 * @param object The object pointer to invoke the member function on (i.e. the this pointer) 00055 * @param function The address of the void member function to attach 00056 */ 00057 template<typename T> 00058 void attach(T *object, void (T::*member)(void)) { 00059 _object = static_cast<void*>(object); 00060 memcpy(_member, (char*)&member, sizeof(member)); 00061 _membercaller = &FunctionPointer::membercaller<T>; 00062 _function = 0; 00063 } 00064 00065 /** Call the attached static or member function 00066 */ 00067 void call(); 00068 00069 pvoidf_t get_function() const { 00070 return (pvoidf_t)_function; 00071 } 00072 00073 #ifdef MBED_OPERATORS 00074 void operator ()(void); 00075 #endif 00076 00077 private: 00078 template<typename T> 00079 static void membercaller(void *object, char *member) { 00080 T* o = static_cast<T*>(object); 00081 void (T::*m)(void); 00082 memcpy((char*)&m, member, sizeof(m)); 00083 (o->*m)(); 00084 } 00085 00086 void (*_function)(void); // static function pointer - 0 if none attached 00087 void *_object; // object this pointer - 0 if none attached 00088 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller 00089 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object 00090 }; 00091 00092 } // namespace mbed 00093 00094 #endif
Generated on Tue Jul 12 2022 21:21:33 by
