The official mbed C/C SDK provides the software platform and libraries to build your applications.

Fork of mbed by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FunctionPointer.h Source File

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 /** A class for storing and calling a pointer to a static or member void function
00024  */
00025 class FunctionPointer {
00026 public:
00027 
00028     /** Create a FunctionPointer, attaching a static function
00029      *
00030      *  @param function The void static function to attach (default is none)
00031      */
00032     FunctionPointer(void (*function)(void) = 0);
00033 
00034     /** Create a FunctionPointer, attaching a member function
00035      *
00036      *  @param object The object pointer to invoke the member function on (i.e. the this pointer)
00037      *  @param function The address of the void member function to attach
00038      */
00039     template<typename T>
00040     FunctionPointer(T *object, void (T::*member)(void)) {
00041         attach(object, member);
00042     }
00043 
00044     /** Attach a static function
00045      *
00046      *  @param function The void static function to attach (default is none)
00047      */
00048     void attach(void (*function)(void) = 0);
00049 
00050     /** Attach a member function
00051      *
00052      *  @param object The object pointer to invoke the member function on (i.e. the this pointer)
00053      *  @param function The address of the void member function to attach
00054      */
00055     template<typename T>
00056     void attach(T *object, void (T::*member)(void)) {
00057         _object = static_cast<void*>(object);
00058         memcpy(_member, (char*)&member, sizeof(member));
00059         _membercaller = &FunctionPointer::membercaller<T>;
00060         _function = 0;
00061     }
00062 
00063     /** Call the attached static or member function
00064      */
00065     void call();
00066 
00067 private:
00068     template<typename T>
00069     static void membercaller(void *object, char *member) {
00070         T* o = static_cast<T*>(object);
00071         void (T::*m)(void);
00072         memcpy((char*)&m, member, sizeof(m));
00073         (o->*m)();
00074     }
00075 
00076     void (*_function)(void);                // static function pointer - 0 if none attached
00077     void *_object;                            // object this pointer - 0 if none attached
00078     char _member[16];                        // raw member function pointer storage - converted back by registered _membercaller
00079     void (*_membercaller)(void*, char*);    // registered membercaller function to convert back and call _member on _object
00080 };
00081 
00082 } // namespace mbed
00083 
00084 #endif