fork
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
MemberFunctionCallback.h
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 #ifndef MEMBER_FUNCTION_CALLBACK_H 00027 #define MEMBER_FUNCTION_CALLBACK_H 00028 00029 #include "mbed.h" 00030 #include "MicroBitConfig.h" 00031 #include "MicroBitEvent.h" 00032 #include "MicroBitCompat.h" 00033 00034 /** 00035 * Class definition for a MemberFunctionCallback. 00036 * 00037 * C++ member functions (also known as methods) have a more complex 00038 * representation than normal C functions. This class allows a reference to 00039 * a C++ member function to be stored then called at a later date. 00040 * 00041 * This class is used extensively by the MicroBitMessageBus to deliver 00042 * events to C++ methods. 00043 */ 00044 class MemberFunctionCallback 00045 { 00046 private: 00047 void* object; 00048 uint32_t method[4]; 00049 void (*invoke)(void *object, uint32_t *method, MicroBitEvent e); 00050 template <typename T> static void methodCall(void* object, uint32_t*method, MicroBitEvent e); 00051 00052 public: 00053 00054 /** 00055 * Constructor. Creates a MemberFunctionCallback based on a pointer to given method. 00056 * 00057 * @param object The object the callback method should be invooked on. 00058 * 00059 * @param method The method to invoke. 00060 */ 00061 template <typename T> MemberFunctionCallback(T* object, void (T::*method)(MicroBitEvent e)); 00062 00063 /** 00064 * A comparison of two MemberFunctionCallback objects. 00065 * 00066 * @return true if the given MemberFunctionCallback is equivalent to this one, false otherwise. 00067 */ 00068 bool operator==(const MemberFunctionCallback &mfc); 00069 00070 /** 00071 * Calls the method reference held by this MemberFunctionCallback. 00072 * 00073 * @param e The event to deliver to the method 00074 */ 00075 void fire(MicroBitEvent e); 00076 }; 00077 00078 /** 00079 * Constructor. Creates a MemberFunctionCallback based on a pointer to given method. 00080 * 00081 * @param object The object the callback method should be invooked on. 00082 * 00083 * @param method The method to invoke. 00084 */ 00085 template <typename T> 00086 MemberFunctionCallback::MemberFunctionCallback(T* object, void (T::*method)(MicroBitEvent e)) 00087 { 00088 this->object = object; 00089 memclr(this->method, sizeof(this->method)); 00090 memcpy(this->method, &method, sizeof(method)); 00091 invoke = &MemberFunctionCallback::methodCall<T>; 00092 } 00093 00094 /** 00095 * A template used to create a static method capable of invoking a C++ member function (method) 00096 * based on the given parameters. 00097 * 00098 * @param object The object the callback method should be invooked on. 00099 * 00100 * @param method The method to invoke. 00101 * 00102 * @param method The MicroBitEvent to supply to the given member function. 00103 */ 00104 template <typename T> 00105 void MemberFunctionCallback::methodCall(void *object, uint32_t *method, MicroBitEvent e) 00106 { 00107 T* o = (T*)object; 00108 void (T::*m)(MicroBitEvent); 00109 memcpy(&m, method, sizeof(m)); 00110 00111 (o->*m)(e); 00112 } 00113 00114 #endif
Generated on Tue Jul 12 2022 19:47:35 by 1.7.2