[175] | 1 | /**
|
---|
| 2 | * @file FP.h
|
---|
| 3 | * @brief Core Utility - Templated Function Pointer Class
|
---|
| 4 | * @author sam grove
|
---|
| 5 | * @version 1.0
|
---|
| 6 | * @see
|
---|
| 7 | *
|
---|
| 8 | * Copyright (c) 2013
|
---|
| 9 | *
|
---|
| 10 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 11 | * you may not use this file except in compliance with the License.
|
---|
| 12 | * You may obtain a copy of the License at
|
---|
| 13 | *
|
---|
| 14 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 15 | *
|
---|
| 16 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 17 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 19 | * See the License for the specific language governing permissions and
|
---|
| 20 | * limitations under the License.
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | #ifndef FP_H
|
---|
| 24 | #define FP_H
|
---|
| 25 |
|
---|
| 26 | /** Example using the FP Class with global functions
|
---|
| 27 | * @code
|
---|
| 28 | * #include "mbed.h"
|
---|
| 29 | * #include "FP.h"
|
---|
| 30 | *
|
---|
| 31 | * FP<void,bool>fp;
|
---|
| 32 | * DigitalOut myled(LED1);
|
---|
| 33 | *
|
---|
| 34 | * void handler(bool value)
|
---|
| 35 | * {
|
---|
| 36 | * myled = value;
|
---|
| 37 | * return;
|
---|
| 38 | * }
|
---|
| 39 | *
|
---|
| 40 | * int main()
|
---|
| 41 | * {
|
---|
| 42 | * fp.attach(&handler);
|
---|
| 43 | *
|
---|
| 44 | * while(1)
|
---|
| 45 | * {
|
---|
| 46 | * fp(1);
|
---|
| 47 | * wait(0.2);
|
---|
| 48 | * fp(0);
|
---|
| 49 | * wait(0.2);
|
---|
| 50 | * }
|
---|
| 51 | * }
|
---|
| 52 | * @endcode
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | /** Example using the FP Class with different class member functions
|
---|
| 56 | * @code
|
---|
| 57 | * #include "mbed.h"
|
---|
| 58 | * #include "FP.h"
|
---|
| 59 | *
|
---|
| 60 | * FP<void,bool>fp;
|
---|
| 61 | * DigitalOut myled(LED4);
|
---|
| 62 | *
|
---|
| 63 | * class Wrapper
|
---|
| 64 | * {
|
---|
| 65 | * public:
|
---|
| 66 | * Wrapper(){}
|
---|
| 67 | *
|
---|
| 68 | * void handler(bool value)
|
---|
| 69 | * {
|
---|
| 70 | * myled = value;
|
---|
| 71 | * return;
|
---|
| 72 | * }
|
---|
| 73 | * };
|
---|
| 74 | *
|
---|
| 75 | * int main()
|
---|
| 76 | * {
|
---|
| 77 | * Wrapper wrapped;
|
---|
| 78 | * fp.attach(&wrapped, &Wrapper::handler);
|
---|
| 79 | *
|
---|
| 80 | * while(1)
|
---|
| 81 | * {
|
---|
| 82 | * fp(1);
|
---|
| 83 | * wait(0.2);
|
---|
| 84 | * fp(0);
|
---|
| 85 | * wait(0.2);
|
---|
| 86 | * }
|
---|
| 87 | * }
|
---|
| 88 | * @endcode
|
---|
| 89 | */
|
---|
| 90 |
|
---|
| 91 | /** Example using the FP Class with member FP and member function
|
---|
| 92 | * @code
|
---|
| 93 | * #include "mbed.h"
|
---|
| 94 | * #include "FP.h"
|
---|
| 95 | *
|
---|
| 96 | * DigitalOut myled(LED2);
|
---|
| 97 | *
|
---|
| 98 | * class Wrapper
|
---|
| 99 | * {
|
---|
| 100 | * public:
|
---|
| 101 | * Wrapper()
|
---|
| 102 | * {
|
---|
| 103 | * fp.attach(this, &Wrapper::handler);
|
---|
| 104 | * }
|
---|
| 105 | *
|
---|
| 106 | * void handler(bool value)
|
---|
| 107 | * {
|
---|
| 108 | * myled = value;
|
---|
| 109 | * return;
|
---|
| 110 | * }
|
---|
| 111 | *
|
---|
| 112 | * FP<void,bool>fp;
|
---|
| 113 | * };
|
---|
| 114 | *
|
---|
| 115 | * int main()
|
---|
| 116 | * {
|
---|
| 117 | * Wrapper wrapped;
|
---|
| 118 | *
|
---|
| 119 | * while(1)
|
---|
| 120 | * {
|
---|
| 121 | * wrapped.fp(1);
|
---|
| 122 | * wait(0.2);
|
---|
| 123 | * wrapped.fp(0);
|
---|
| 124 | * wait(0.2);
|
---|
| 125 | * }
|
---|
| 126 | * }
|
---|
| 127 | * @endcode
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * @class FP
|
---|
| 132 | * @brief API abstraction for a Function Pointers
|
---|
| 133 | */
|
---|
| 134 | template<class retT, class argT>
|
---|
| 135 | class FP
|
---|
| 136 | {
|
---|
| 137 | public:
|
---|
| 138 | /** Create the FP object
|
---|
| 139 | */
|
---|
| 140 | FP();
|
---|
| 141 |
|
---|
| 142 | /** Add a callback function to the class
|
---|
| 143 | * @param item - Address of the initialized class
|
---|
| 144 | * @param member - Address of the member function (dont forget the scope that the function is defined in)
|
---|
| 145 | */
|
---|
| 146 | template<class T>
|
---|
| 147 | void attach(T *item, retT (T::*method)(argT))
|
---|
| 148 | {
|
---|
| 149 | obj_callback = (FPtrDummy *)(item);
|
---|
| 150 | method_callback = (retT (FPtrDummy::*)(argT))(method);
|
---|
| 151 | return;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Add a callback function to the class
|
---|
| 155 | * @param function - The address of a globally defined function
|
---|
| 156 | */
|
---|
| 157 | void attach(retT (*function)(argT));
|
---|
| 158 |
|
---|
| 159 | /** Invoke the function attached to the class
|
---|
| 160 | * @param arg - An argument that is passed into the function handler that is called
|
---|
| 161 | * @return The return from the function hanlder called by this class
|
---|
| 162 | */
|
---|
| 163 | retT operator()(argT arg) const;
|
---|
| 164 |
|
---|
| 165 | bool attached();
|
---|
| 166 |
|
---|
| 167 | void detach();
|
---|
| 168 |
|
---|
| 169 | private:
|
---|
| 170 |
|
---|
| 171 | // empty type used for casting
|
---|
| 172 | class FPtrDummy;
|
---|
| 173 |
|
---|
| 174 | FPtrDummy *obj_callback;
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * @union Funciton
|
---|
| 178 | * @brief Member or global callback function
|
---|
| 179 | */
|
---|
| 180 | union
|
---|
| 181 | {
|
---|
| 182 | retT (*c_callback)(argT); /*!< Footprint for a global function */
|
---|
| 183 | retT (FPtrDummy::*method_callback)(argT); /*!< Footprint for a member function */
|
---|
| 184 | };
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | #endif
|
---|
| 188 |
|
---|