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