File size: 4,826 Bytes
69c3cbe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | /*******************************************************************************
* Copyright (c) 2013, 2014
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Sam Grove - initial API and implementation and/or initial documentation
* Ian Craggs - added attached and detached member functions
* Sam Grove - removed need for FP.cpp
*******************************************************************************/
#ifndef FP_H
#define FP_H
/** Example using the FP Class with global functions
* @code
* #include "mbed.h"
* #include "FP.h"
*
* FP<void,bool>fp;
* DigitalOut myled(LED1);
*
* void handler(bool value)
* {
* myled = value;
* return;
* }
*
* int main()
* {
* fp.attach(&handler);
*
* while(1)
* {
* fp(1);
* wait(0.2);
* fp(0);
* wait(0.2);
* }
* }
* @endcode
*/
/** Example using the FP Class with different class member functions
* @code
* #include "mbed.h"
* #include "FP.h"
*
* FP<void,bool>fp;
* DigitalOut myled(LED4);
*
* class Wrapper
* {
* public:
* Wrapper(){}
*
* void handler(bool value)
* {
* myled = value;
* return;
* }
* };
*
* int main()
* {
* Wrapper wrapped;
* fp.attach(&wrapped, &Wrapper::handler);
*
* while(1)
* {
* fp(1);
* wait(0.2);
* fp(0);
* wait(0.2);
* }
* }
* @endcode
*/
/** Example using the FP Class with member FP and member function
* @code
* #include "mbed.h"
* #include "FP.h"
*
* DigitalOut myled(LED2);
*
* class Wrapper
* {
* public:
* Wrapper()
* {
* fp.attach(this, &Wrapper::handler);
* }
*
* void handler(bool value)
* {
* myled = value;
* return;
* }
*
* FP<void,bool>fp;
* };
*
* int main()
* {
* Wrapper wrapped;
*
* while(1)
* {
* wrapped.fp(1);
* wait(0.2);
* wrapped.fp(0);
* wait(0.2);
* }
* }
* @endcode
*/
/**
* @class FP
* @brief API for managing Function Pointers
*/
template<class retT, class argT>
class FP
{
public:
/** Create the FP object - only one callback can be attached to the object, that is
* a member function or a global function, not both at the same time
*/
FP()
{
obj_callback = 0;
c_callback = 0;
}
/** Add a callback function to the object
* @param item - Address of the initialized object
* @param member - Address of the member function (dont forget the scope that the function is defined in)
*/
template<class T>
void attach(T *item, retT (T::*method)(argT))
{
obj_callback = (FPtrDummy *)(item);
method_callback = (retT (FPtrDummy::*)(argT))(method);
return;
}
/** Add a callback function to the object
* @param function - The address of a globally defined function
*/
void attach(retT (*function)(argT))
{
c_callback = function;
}
/** Invoke the function attached to the class
* @param arg - An argument that is passed into the function handler that is called
* @return The return from the function hanlder called by this class
*/
retT operator()(argT arg) const
{
if( 0 != c_callback ) {
return obj_callback ? (obj_callback->*method_callback)(arg) : (*c_callback)(arg);
}
return (retT)0;
}
/** Determine if an callback is currently hooked
* @return 1 if a method is hooked, 0 otherwise
*/
bool attached()
{
return obj_callback || c_callback;
}
/** Release a function from the callback hook
*/
void detach()
{
obj_callback = 0;
c_callback = 0;
}
private:
// empty type used for casting
class FPtrDummy;
FPtrDummy *obj_callback;
/**
* @union Funciton
* @brief Member or global callback function
*/
union {
retT (*c_callback)(argT); /*!< Footprint for a global function */
retT (FPtrDummy::*method_callback)(argT); /*!< Footprint for a member function */
};
};
#endif
|