https://github.com/WebBluetoothCG/demos/pull/42
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
inc/core/MemberFunctionCallback.h@1:8aa5cdb4ab67, 2016-04-07 (annotated)
- Committer:
- Jonathan Austin
- Date:
- Thu Apr 07 01:33:22 2016 +0100
- Revision:
- 1:8aa5cdb4ab67
Synchronized with git rev 55cb9199
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
1:8aa5cdb4ab67 | 1 | /* |
Jonathan Austin |
1:8aa5cdb4ab67 | 2 | The MIT License (MIT) |
Jonathan Austin |
1:8aa5cdb4ab67 | 3 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
Jonathan Austin |
1:8aa5cdb4ab67 | 6 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
Jonathan Austin |
1:8aa5cdb4ab67 | 8 | copy of this software and associated documentation files (the "Software"), |
Jonathan Austin |
1:8aa5cdb4ab67 | 9 | to deal in the Software without restriction, including without limitation |
Jonathan Austin |
1:8aa5cdb4ab67 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Jonathan Austin |
1:8aa5cdb4ab67 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
Jonathan Austin |
1:8aa5cdb4ab67 | 12 | Software is furnished to do so, subject to the following conditions: |
Jonathan Austin |
1:8aa5cdb4ab67 | 13 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 14 | The above copyright notice and this permission notice shall be included in |
Jonathan Austin |
1:8aa5cdb4ab67 | 15 | all copies or substantial portions of the Software. |
Jonathan Austin |
1:8aa5cdb4ab67 | 16 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Jonathan Austin |
1:8aa5cdb4ab67 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jonathan Austin |
1:8aa5cdb4ab67 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
Jonathan Austin |
1:8aa5cdb4ab67 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
Jonathan Austin |
1:8aa5cdb4ab67 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 23 | DEALINGS IN THE SOFTWARE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 24 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 25 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 26 | #ifndef MEMBER_FUNCTION_CALLBACK_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | #define MEMBER_FUNCTION_CALLBACK_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | #include "mbed.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | #include "MicroBitEvent.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | #include "MicroBitCompat.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | * Class definition for a MemberFunctionCallback. |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | * C++ member functions (also known as methods) have a more complex |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | * representation than normal C functions. This class allows a reference to |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | * a C++ member function to be stored then called at a later date. |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | * This class is used extensively by the MicroBitMessageBus to deliver |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | * events to C++ methods. |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | class MemberFunctionCallback |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | private: |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | void* object; |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | uint32_t method[4]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | void (*invoke)(void *object, uint32_t *method, MicroBitEvent e); |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | template <typename T> static void methodCall(void* object, uint32_t*method, MicroBitEvent e); |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | * Constructor. Creates a MemberFunctionCallback based on a pointer to given method. |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | * @param object The object the callback method should be invooked on. |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | * @param method The method to invoke. |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | template <typename T> MemberFunctionCallback(T* object, void (T::*method)(MicroBitEvent e)); |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | * A comparison of two MemberFunctionCallback objects. |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | * @return true if the given MemberFunctionCallback is equivalent to this one, false otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | bool operator==(const MemberFunctionCallback &mfc); |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | * Calls the method reference held by this MemberFunctionCallback. |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | * @param e The event to deliver to the method |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | void fire(MicroBitEvent e); |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | * Constructor. Creates a MemberFunctionCallback based on a pointer to given method. |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | * @param object The object the callback method should be invooked on. |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | * @param method The method to invoke. |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | template <typename T> |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | MemberFunctionCallback::MemberFunctionCallback(T* object, void (T::*method)(MicroBitEvent e)) |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | this->object = object; |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | memclr(this->method, sizeof(this->method)); |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | memcpy(this->method, &method, sizeof(method)); |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | invoke = &MemberFunctionCallback::methodCall<T>; |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | * A template used to create a static method capable of invoking a C++ member function (method) |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | * based on the given parameters. |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | * @param object The object the callback method should be invooked on. |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | * @param method The method to invoke. |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | * @param method The MicroBitEvent to supply to the given member function. |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | template <typename T> |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | void MemberFunctionCallback::methodCall(void *object, uint32_t *method, MicroBitEvent e) |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | T* o = (T*)object; |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | void (T::*m)(MicroBitEvent); |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | memcpy(&m, method, sizeof(m)); |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | (o->*m)(e); |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | #endif |