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