Updated
Fork of BLE_API by
ble/FunctionPointerWithContext.h@1045:b9d15970040f, 2016-01-11 (annotated)
- Committer:
- vcoubard
- Date:
- Mon Jan 11 08:51:26 2016 +0000
- Revision:
- 1045:b9d15970040f
- Parent:
- 1042:21a86ac7f5b1
- Child:
- 1046:87a2ebe45470
Synchronized with git rev 62a1c4a9
Author: Vincent Coubard
Improve Characteristic descriptor discovery:
- all member of
CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t are now
const by default
- CharacteristicDescriptorDiscovery::TerminationCallbackParams_t now
accept a status parameter which indicate if the operation ends properly
or not
- Remove DiscoveredCharacteristicDescriptor declaration from
DiscoveredCharacteristic.h file
- Add comparison operation to DiscoveredCharacteristic::Properties_t type
- Add lastHandle member to DiscoveredCharacteristic
- Add equality operator to DiscoveredCharacteristic
- make FunctionPointerWithContext call operation const, so that it mirror
std::function and allow to call this kind of objects to be called when
they are passed by const reference
- Add primitive operations to GattClient for dicovering characteristic
descriptors
- Fullfil DiscoveredCharacteristic::discoverDescriptors function
implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 710:b2e1a2660ec2 | 1 | /* mbed Microcontroller Library |
rgrover1 | 710:b2e1a2660ec2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 710:b2e1a2660ec2 | 3 | * |
rgrover1 | 710:b2e1a2660ec2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 710:b2e1a2660ec2 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 710:b2e1a2660ec2 | 6 | * You may obtain a copy of the License at |
rgrover1 | 710:b2e1a2660ec2 | 7 | * |
rgrover1 | 710:b2e1a2660ec2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 710:b2e1a2660ec2 | 9 | * |
rgrover1 | 710:b2e1a2660ec2 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 710:b2e1a2660ec2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 710:b2e1a2660ec2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 710:b2e1a2660ec2 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 710:b2e1a2660ec2 | 14 | * limitations under the License. |
rgrover1 | 710:b2e1a2660ec2 | 15 | */ |
rgrover1 | 710:b2e1a2660ec2 | 16 | |
rgrover1 | 710:b2e1a2660ec2 | 17 | #ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |
rgrover1 | 710:b2e1a2660ec2 | 18 | #define MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |
rgrover1 | 710:b2e1a2660ec2 | 19 | |
rgrover1 | 710:b2e1a2660ec2 | 20 | #include <string.h> |
rgrover1 | 710:b2e1a2660ec2 | 21 | |
rgrover1 | 710:b2e1a2660ec2 | 22 | /** A class for storing and calling a pointer to a static or member void function |
vcoubard | 1042:21a86ac7f5b1 | 23 | * which takes a context. |
rgrover1 | 710:b2e1a2660ec2 | 24 | */ |
rgrover1 | 710:b2e1a2660ec2 | 25 | template <typename ContextType> |
vcoubard | 1042:21a86ac7f5b1 | 26 | class FunctionPointerWithContext { |
rgrover1 | 710:b2e1a2660ec2 | 27 | public: |
rgrover1 | 710:b2e1a2660ec2 | 28 | typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t; |
vcoubard | 1045:b9d15970040f | 29 | typedef const FunctionPointerWithContext<ContextType> *cpFunctionPointerWithContext_t; |
rgrover1 | 710:b2e1a2660ec2 | 30 | typedef void (*pvoidfcontext_t)(ContextType context); |
rgrover1 | 710:b2e1a2660ec2 | 31 | |
vcoubard | 1042:21a86ac7f5b1 | 32 | /** Create a FunctionPointerWithContext, attaching a static function |
rgrover1 | 710:b2e1a2660ec2 | 33 | * |
vcoubard | 1042:21a86ac7f5b1 | 34 | * @param function The void static function to attach (default is none) |
rgrover1 | 710:b2e1a2660ec2 | 35 | */ |
rgrover1 | 710:b2e1a2660ec2 | 36 | FunctionPointerWithContext(void (*function)(ContextType context) = NULL) : |
vcoubard | 1042:21a86ac7f5b1 | 37 | _function(NULL), _caller(NULL), _next(NULL) { |
rgrover1 | 710:b2e1a2660ec2 | 38 | attach(function); |
rgrover1 | 710:b2e1a2660ec2 | 39 | } |
rgrover1 | 710:b2e1a2660ec2 | 40 | |
vcoubard | 1042:21a86ac7f5b1 | 41 | /** Create a FunctionPointerWithContext, attaching a member function |
rgrover1 | 710:b2e1a2660ec2 | 42 | * |
vcoubard | 1042:21a86ac7f5b1 | 43 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
vcoubard | 1042:21a86ac7f5b1 | 44 | * @param function The address of the void member function to attach |
rgrover1 | 710:b2e1a2660ec2 | 45 | */ |
rgrover1 | 710:b2e1a2660ec2 | 46 | template<typename T> |
rgrover1 | 710:b2e1a2660ec2 | 47 | FunctionPointerWithContext(T *object, void (T::*member)(ContextType context)) : |
rgrover1 | 897:838e1375dbaa | 48 | _memberFunctionAndPointer(), _caller(NULL), _next(NULL) { |
rgrover1 | 710:b2e1a2660ec2 | 49 | attach(object, member); |
rgrover1 | 710:b2e1a2660ec2 | 50 | } |
rgrover1 | 710:b2e1a2660ec2 | 51 | |
vcoubard | 1042:21a86ac7f5b1 | 52 | /** Attach a static function |
rgrover1 | 710:b2e1a2660ec2 | 53 | * |
vcoubard | 1042:21a86ac7f5b1 | 54 | * @param function The void static function to attach (default is none) |
rgrover1 | 710:b2e1a2660ec2 | 55 | */ |
rgrover1 | 710:b2e1a2660ec2 | 56 | void attach(void (*function)(ContextType context) = NULL) { |
rgrover1 | 710:b2e1a2660ec2 | 57 | _function = function; |
rgrover1 | 897:838e1375dbaa | 58 | _caller = functioncaller; |
rgrover1 | 710:b2e1a2660ec2 | 59 | } |
rgrover1 | 710:b2e1a2660ec2 | 60 | |
vcoubard | 1042:21a86ac7f5b1 | 61 | /** Attach a member function |
rgrover1 | 710:b2e1a2660ec2 | 62 | * |
vcoubard | 1042:21a86ac7f5b1 | 63 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
vcoubard | 1042:21a86ac7f5b1 | 64 | * @param function The address of the void member function to attach |
rgrover1 | 710:b2e1a2660ec2 | 65 | */ |
rgrover1 | 710:b2e1a2660ec2 | 66 | template<typename T> |
rgrover1 | 710:b2e1a2660ec2 | 67 | void attach(T *object, void (T::*member)(ContextType context)) { |
rgrover1 | 897:838e1375dbaa | 68 | _memberFunctionAndPointer._object = static_cast<void *>(object); |
rgrover1 | 897:838e1375dbaa | 69 | memcpy(_memberFunctionAndPointer._memberFunction, (char*) &member, sizeof(member)); |
rgrover1 | 897:838e1375dbaa | 70 | _caller = &FunctionPointerWithContext::membercaller<T>; |
rgrover1 | 710:b2e1a2660ec2 | 71 | } |
rgrover1 | 710:b2e1a2660ec2 | 72 | |
vcoubard | 1042:21a86ac7f5b1 | 73 | /** Call the attached static or member function; and if there are chained |
rgrover1 | 710:b2e1a2660ec2 | 74 | * FunctionPointers their callbacks are invoked as well. |
vcoubard | 1042:21a86ac7f5b1 | 75 | * @Note: all chained callbacks stack up; so hopefully there won't be too |
rgrover1 | 710:b2e1a2660ec2 | 76 | * many FunctionPointers in a chain. */ |
vcoubard | 1045:b9d15970040f | 77 | void call(ContextType context) const { |
rgrover1 | 952:8a6c287de1be | 78 | _caller(this, context); |
vcoubard | 1042:21a86ac7f5b1 | 79 | |
vcoubard | 1042:21a86ac7f5b1 | 80 | /* Propagate the call to next in the chain. */ |
vcoubard | 1042:21a86ac7f5b1 | 81 | if (_next) { |
vcoubard | 1042:21a86ac7f5b1 | 82 | _next->call(context); |
vcoubard | 1042:21a86ac7f5b1 | 83 | } |
rgrover1 | 990:53ac0ac3aa39 | 84 | } |
rgrover1 | 990:53ac0ac3aa39 | 85 | |
rgrover1 | 990:53ac0ac3aa39 | 86 | /** |
vcoubard | 1042:21a86ac7f5b1 | 87 | * Setup an external FunctionPointer as a next in the chain of related |
rgrover1 | 710:b2e1a2660ec2 | 88 | * callbacks. Invoking call() on the head FunctionPointer will invoke all |
rgrover1 | 710:b2e1a2660ec2 | 89 | * chained callbacks. |
rgrover1 | 710:b2e1a2660ec2 | 90 | * |
rgrover1 | 710:b2e1a2660ec2 | 91 | * Refer to 'CallChain' as an alternative. |
rgrover1 | 710:b2e1a2660ec2 | 92 | */ |
rgrover1 | 710:b2e1a2660ec2 | 93 | void chainAsNext(pFunctionPointerWithContext_t next) { |
rgrover1 | 710:b2e1a2660ec2 | 94 | _next = next; |
rgrover1 | 710:b2e1a2660ec2 | 95 | } |
rgrover1 | 710:b2e1a2660ec2 | 96 | |
rgrover1 | 710:b2e1a2660ec2 | 97 | pFunctionPointerWithContext_t getNext(void) const { |
rgrover1 | 710:b2e1a2660ec2 | 98 | return _next; |
rgrover1 | 710:b2e1a2660ec2 | 99 | } |
rgrover1 | 710:b2e1a2660ec2 | 100 | |
rgrover1 | 710:b2e1a2660ec2 | 101 | pvoidfcontext_t get_function() const { |
rgrover1 | 710:b2e1a2660ec2 | 102 | return (pvoidfcontext_t)_function; |
rgrover1 | 710:b2e1a2660ec2 | 103 | } |
rgrover1 | 710:b2e1a2660ec2 | 104 | |
vcoubard | 1045:b9d15970040f | 105 | friend bool operator==(const FunctionPointerWithContext& lhs, const FunctionPointerWithContext& rhs) { |
vcoubard | 1045:b9d15970040f | 106 | return rhs._caller == lhs._caller && |
vcoubard | 1045:b9d15970040f | 107 | memcmp( |
vcoubard | 1045:b9d15970040f | 108 | &rhs._memberFunctionAndPointer, |
vcoubard | 1045:b9d15970040f | 109 | &lhs._memberFunctionAndPointer, |
vcoubard | 1045:b9d15970040f | 110 | sizeof(rhs._memberFunctionAndPointer) |
vcoubard | 1045:b9d15970040f | 111 | ) == 0; |
vcoubard | 1045:b9d15970040f | 112 | } |
vcoubard | 1045:b9d15970040f | 113 | |
rgrover1 | 710:b2e1a2660ec2 | 114 | private: |
rgrover1 | 710:b2e1a2660ec2 | 115 | template<typename T> |
vcoubard | 1045:b9d15970040f | 116 | static void membercaller(cpFunctionPointerWithContext_t self, ContextType context) { |
rgrover1 | 897:838e1375dbaa | 117 | if (self->_memberFunctionAndPointer._object) { |
rgrover1 | 897:838e1375dbaa | 118 | T *o = static_cast<T *>(self->_memberFunctionAndPointer._object); |
rgrover1 | 897:838e1375dbaa | 119 | void (T::*m)(ContextType); |
rgrover1 | 897:838e1375dbaa | 120 | memcpy((char*) &m, self->_memberFunctionAndPointer._memberFunction, sizeof(m)); |
rgrover1 | 897:838e1375dbaa | 121 | (o->*m)(context); |
rgrover1 | 897:838e1375dbaa | 122 | } |
rgrover1 | 897:838e1375dbaa | 123 | } |
rgrover1 | 897:838e1375dbaa | 124 | |
vcoubard | 1045:b9d15970040f | 125 | static void functioncaller(cpFunctionPointerWithContext_t self, ContextType context) { |
rgrover1 | 897:838e1375dbaa | 126 | if (self->_function) { |
rgrover1 | 897:838e1375dbaa | 127 | self->_function(context); |
rgrover1 | 897:838e1375dbaa | 128 | } |
rgrover1 | 710:b2e1a2660ec2 | 129 | } |
rgrover1 | 710:b2e1a2660ec2 | 130 | |
rgrover1 | 897:838e1375dbaa | 131 | struct MemberFunctionAndPtr { |
rgrover1 | 897:838e1375dbaa | 132 | /* |
vcoubard | 1042:21a86ac7f5b1 | 133 | * forward declaration of a class and a member function to this class. |
rgrover1 | 897:838e1375dbaa | 134 | * Because the compiler doesn't know anything about the forwarded member |
rgrover1 | 897:838e1375dbaa | 135 | * function, it will always use the biggest size and the biggest alignment |
rgrover1 | 897:838e1375dbaa | 136 | * that a member function can take for objects of type UndefinedMemberFunction. |
rgrover1 | 897:838e1375dbaa | 137 | */ |
rgrover1 | 897:838e1375dbaa | 138 | class UndefinedClass; |
rgrover1 | 897:838e1375dbaa | 139 | typedef void (UndefinedClass::*UndefinedMemberFunction)(ContextType); |
rgrover1 | 897:838e1375dbaa | 140 | |
rgrover1 | 897:838e1375dbaa | 141 | void* _object; |
rgrover1 | 897:838e1375dbaa | 142 | union { |
rgrover1 | 897:838e1375dbaa | 143 | char _memberFunction[sizeof(UndefinedMemberFunction)]; |
rgrover1 | 897:838e1375dbaa | 144 | UndefinedMemberFunction _alignment; |
rgrover1 | 897:838e1375dbaa | 145 | }; |
rgrover1 | 897:838e1375dbaa | 146 | }; |
rgrover1 | 897:838e1375dbaa | 147 | |
rgrover1 | 897:838e1375dbaa | 148 | union { |
vcoubard | 1042:21a86ac7f5b1 | 149 | pvoidfcontext_t _function; /**< static function pointer - NULL if none attached */ |
rgrover1 | 897:838e1375dbaa | 150 | /** |
rgrover1 | 897:838e1375dbaa | 151 | * object this pointer and pointer to member - |
rgrover1 | 897:838e1375dbaa | 152 | * _memberFunctionAndPointer._object will be NULL if none attached |
rgrover1 | 897:838e1375dbaa | 153 | */ |
vcoubard | 1045:b9d15970040f | 154 | mutable MemberFunctionAndPtr _memberFunctionAndPointer; |
rgrover1 | 897:838e1375dbaa | 155 | }; |
rgrover1 | 897:838e1375dbaa | 156 | |
vcoubard | 1045:b9d15970040f | 157 | void (*_caller)(const FunctionPointerWithContext*, ContextType); |
rgrover1 | 897:838e1375dbaa | 158 | |
vcoubard | 1042:21a86ac7f5b1 | 159 | pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers; this |
rgrover1 | 710:b2e1a2660ec2 | 160 | * allows chaining function pointers without requiring |
vcoubard | 1042:21a86ac7f5b1 | 161 | * external memory to manage the chain. Also refer to |
rgrover1 | 710:b2e1a2660ec2 | 162 | * 'CallChain' as an alternative. */ |
rgrover1 | 710:b2e1a2660ec2 | 163 | }; |
rgrover1 | 710:b2e1a2660ec2 | 164 | |
rgrover1 | 993:4d62b7967c11 | 165 | /** |
rgrover1 | 993:4d62b7967c11 | 166 | * @brief Create a new FunctionPointerWithContext which bind an instance and a |
rgrover1 | 993:4d62b7967c11 | 167 | * a member function together. |
rgrover1 | 993:4d62b7967c11 | 168 | * @details This little helper is a just here to eliminate the need to write the |
rgrover1 | 993:4d62b7967c11 | 169 | * FunctionPointerWithContext type each time you want to create one by kicking |
rgrover1 | 993:4d62b7967c11 | 170 | * automatic type deduction of function templates. With this function, it is easy |
rgrover1 | 993:4d62b7967c11 | 171 | * to write only one entry point for functions which expect a FunctionPointer |
rgrover1 | 993:4d62b7967c11 | 172 | * in parameters. |
rgrover1 | 993:4d62b7967c11 | 173 | * |
rgrover1 | 993:4d62b7967c11 | 174 | * @param object to bound with member function |
rgrover1 | 993:4d62b7967c11 | 175 | * @param member The member function called |
rgrover1 | 993:4d62b7967c11 | 176 | * @return a new FunctionPointerWithContext |
rgrover1 | 993:4d62b7967c11 | 177 | */ |
rgrover1 | 993:4d62b7967c11 | 178 | template<typename T, typename ContextType> |
rgrover1 | 993:4d62b7967c11 | 179 | FunctionPointerWithContext<ContextType> makeFunctionPointer(T *object, void (T::*member)(ContextType context)) |
rgrover1 | 993:4d62b7967c11 | 180 | { |
rgrover1 | 993:4d62b7967c11 | 181 | return FunctionPointerWithContext<ContextType>(object, member); |
rgrover1 | 993:4d62b7967c11 | 182 | } |
rgrover1 | 993:4d62b7967c11 | 183 | |
rgrover1 | 710:b2e1a2660ec2 | 184 | #endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |