Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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