The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.
Dependents: Cayenne-ESP8266Interface Cayenne-WIZnet_Library Cayenne-WIZnetInterface Cayenne-X-NUCLEO-IDW01M1 ... more
src/CayenneMQTTClient/FP.h@4:421366004b5d, 2016-10-07 (annotated)
- Committer:
- jburhenn
- Date:
- Fri Oct 07 22:44:16 2016 +0000
- Revision:
- 4:421366004b5d
- Parent:
- 2:c0419dbecfbd
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 | 4:421366004b5d | 23 | * @class FP |
jburhenn | 4:421366004b5d | 24 | * @brief API for managing Function Pointers |
jburhenn | 4:421366004b5d | 25 | * |
jburhenn | 4:421366004b5d | 26 | * Example using the FP Class with global functions |
jburhenn | 4:421366004b5d | 27 | * @code |
jburhenn | 4:421366004b5d | 28 | * #include "mbed.h" |
jburhenn | 4:421366004b5d | 29 | * #include "FP.h" |
jburhenn | 4:421366004b5d | 30 | * |
jburhenn | 4:421366004b5d | 31 | * FP<void,bool>fp; |
jburhenn | 4:421366004b5d | 32 | * DigitalOut myled(LED1); |
jburhenn | 4:421366004b5d | 33 | * |
jburhenn | 4:421366004b5d | 34 | * void handler(bool value) |
jburhenn | 4:421366004b5d | 35 | * { |
jburhenn | 4:421366004b5d | 36 | * myled = value; |
jburhenn | 4:421366004b5d | 37 | * return; |
jburhenn | 4:421366004b5d | 38 | * } |
jburhenn | 4:421366004b5d | 39 | * |
jburhenn | 4:421366004b5d | 40 | * int main() |
jburhenn | 4:421366004b5d | 41 | * { |
jburhenn | 4:421366004b5d | 42 | * fp.attach(&handler); |
jburhenn | 4:421366004b5d | 43 | * |
jburhenn | 4:421366004b5d | 44 | * while(1) |
jburhenn | 4:421366004b5d | 45 | * { |
jburhenn | 4:421366004b5d | 46 | * fp(1); |
jburhenn | 4:421366004b5d | 47 | * wait(0.2); |
jburhenn | 4:421366004b5d | 48 | * fp(0); |
jburhenn | 4:421366004b5d | 49 | * wait(0.2); |
jburhenn | 4:421366004b5d | 50 | * } |
jburhenn | 4:421366004b5d | 51 | * } |
jburhenn | 4:421366004b5d | 52 | * @endcode |
jburhenn | 4:421366004b5d | 53 | * |
jburhenn | 4:421366004b5d | 54 | * Example using the FP Class with different class member functions |
jburhenn | 4:421366004b5d | 55 | * @code |
jburhenn | 4:421366004b5d | 56 | * #include "mbed.h" |
jburhenn | 4:421366004b5d | 57 | * #include "FP.h" |
jburhenn | 4:421366004b5d | 58 | * |
jburhenn | 4:421366004b5d | 59 | * FP<void,bool>fp; |
jburhenn | 4:421366004b5d | 60 | * DigitalOut myled(LED4); |
jburhenn | 4:421366004b5d | 61 | * |
jburhenn | 4:421366004b5d | 62 | * class Wrapper |
jburhenn | 4:421366004b5d | 63 | * { |
jburhenn | 4:421366004b5d | 64 | * public: |
jburhenn | 4:421366004b5d | 65 | * Wrapper(){} |
jburhenn | 4:421366004b5d | 66 | * |
jburhenn | 4:421366004b5d | 67 | * void handler(bool value) |
jburhenn | 4:421366004b5d | 68 | * { |
jburhenn | 4:421366004b5d | 69 | * myled = value; |
jburhenn | 4:421366004b5d | 70 | * return; |
jburhenn | 4:421366004b5d | 71 | * } |
jburhenn | 4:421366004b5d | 72 | * }; |
jburhenn | 4:421366004b5d | 73 | * |
jburhenn | 4:421366004b5d | 74 | * int main() |
jburhenn | 4:421366004b5d | 75 | * { |
jburhenn | 4:421366004b5d | 76 | * Wrapper wrapped; |
jburhenn | 4:421366004b5d | 77 | * fp.attach(&wrapped, &Wrapper::handler); |
jburhenn | 4:421366004b5d | 78 | * |
jburhenn | 4:421366004b5d | 79 | * while(1) |
jburhenn | 4:421366004b5d | 80 | * { |
jburhenn | 4:421366004b5d | 81 | * fp(1); |
jburhenn | 4:421366004b5d | 82 | * wait(0.2); |
jburhenn | 4:421366004b5d | 83 | * fp(0); |
jburhenn | 4:421366004b5d | 84 | * wait(0.2); |
jburhenn | 4:421366004b5d | 85 | * } |
jburhenn | 4:421366004b5d | 86 | * } |
jburhenn | 4:421366004b5d | 87 | * @endcode |
jburhenn | 4:421366004b5d | 88 | * |
jburhenn | 4:421366004b5d | 89 | * Example using the FP Class with member FP and member function |
jburhenn | 0:09ef59d2d0f7 | 90 | * @code |
jburhenn | 0:09ef59d2d0f7 | 91 | * #include "mbed.h" |
jburhenn | 0:09ef59d2d0f7 | 92 | * #include "FP.h" |
jburhenn | 0:09ef59d2d0f7 | 93 | * |
jburhenn | 0:09ef59d2d0f7 | 94 | * DigitalOut myled(LED2); |
jburhenn | 0:09ef59d2d0f7 | 95 | * |
jburhenn | 0:09ef59d2d0f7 | 96 | * class Wrapper |
jburhenn | 0:09ef59d2d0f7 | 97 | * { |
jburhenn | 0:09ef59d2d0f7 | 98 | * public: |
jburhenn | 0:09ef59d2d0f7 | 99 | * Wrapper() |
jburhenn | 0:09ef59d2d0f7 | 100 | * { |
jburhenn | 0:09ef59d2d0f7 | 101 | * fp.attach(this, &Wrapper::handler); |
jburhenn | 0:09ef59d2d0f7 | 102 | * } |
jburhenn | 0:09ef59d2d0f7 | 103 | * |
jburhenn | 0:09ef59d2d0f7 | 104 | * void handler(bool value) |
jburhenn | 0:09ef59d2d0f7 | 105 | * { |
jburhenn | 0:09ef59d2d0f7 | 106 | * myled = value; |
jburhenn | 0:09ef59d2d0f7 | 107 | * return; |
jburhenn | 0:09ef59d2d0f7 | 108 | * } |
jburhenn | 0:09ef59d2d0f7 | 109 | * |
jburhenn | 0:09ef59d2d0f7 | 110 | * FP<void,bool>fp; |
jburhenn | 0:09ef59d2d0f7 | 111 | * }; |
jburhenn | 0:09ef59d2d0f7 | 112 | * |
jburhenn | 0:09ef59d2d0f7 | 113 | * int main() |
jburhenn | 0:09ef59d2d0f7 | 114 | * { |
jburhenn | 0:09ef59d2d0f7 | 115 | * Wrapper wrapped; |
jburhenn | 0:09ef59d2d0f7 | 116 | * |
jburhenn | 0:09ef59d2d0f7 | 117 | * while(1) |
jburhenn | 0:09ef59d2d0f7 | 118 | * { |
jburhenn | 0:09ef59d2d0f7 | 119 | * wrapped.fp(1); |
jburhenn | 0:09ef59d2d0f7 | 120 | * wait(0.2); |
jburhenn | 0:09ef59d2d0f7 | 121 | * wrapped.fp(0); |
jburhenn | 0:09ef59d2d0f7 | 122 | * wait(0.2); |
jburhenn | 0:09ef59d2d0f7 | 123 | * } |
jburhenn | 0:09ef59d2d0f7 | 124 | * } |
jburhenn | 0:09ef59d2d0f7 | 125 | * @endcode |
jburhenn | 0:09ef59d2d0f7 | 126 | */ |
jburhenn | 0:09ef59d2d0f7 | 127 | template<class retT, class argT> |
jburhenn | 0:09ef59d2d0f7 | 128 | class FP |
jburhenn | 0:09ef59d2d0f7 | 129 | { |
jburhenn | 0:09ef59d2d0f7 | 130 | public: |
jburhenn | 0:09ef59d2d0f7 | 131 | /** Create the FP object - only one callback can be attached to the object, that is |
jburhenn | 0:09ef59d2d0f7 | 132 | * a member function or a global function, not both at the same time |
jburhenn | 0:09ef59d2d0f7 | 133 | */ |
jburhenn | 0:09ef59d2d0f7 | 134 | FP() |
jburhenn | 0:09ef59d2d0f7 | 135 | { |
jburhenn | 0:09ef59d2d0f7 | 136 | obj_callback = 0; |
jburhenn | 0:09ef59d2d0f7 | 137 | c_callback = 0; |
jburhenn | 0:09ef59d2d0f7 | 138 | } |
jburhenn | 0:09ef59d2d0f7 | 139 | |
jburhenn | 0:09ef59d2d0f7 | 140 | /** Add a callback function to the object |
jburhenn | 0:09ef59d2d0f7 | 141 | * @param item - Address of the initialized object |
jburhenn | 0:09ef59d2d0f7 | 142 | * @param member - Address of the member function (dont forget the scope that the function is defined in) |
jburhenn | 0:09ef59d2d0f7 | 143 | */ |
jburhenn | 0:09ef59d2d0f7 | 144 | template<class T> |
jburhenn | 0:09ef59d2d0f7 | 145 | void attach(T *item, retT (T::*method)(argT)) |
jburhenn | 0:09ef59d2d0f7 | 146 | { |
jburhenn | 0:09ef59d2d0f7 | 147 | obj_callback = (FPtrDummy *)(item); |
jburhenn | 0:09ef59d2d0f7 | 148 | method_callback = (retT (FPtrDummy::*)(argT))(method); |
jburhenn | 0:09ef59d2d0f7 | 149 | return; |
jburhenn | 0:09ef59d2d0f7 | 150 | } |
jburhenn | 0:09ef59d2d0f7 | 151 | |
jburhenn | 0:09ef59d2d0f7 | 152 | /** Add a callback function to the object |
jburhenn | 0:09ef59d2d0f7 | 153 | * @param function - The address of a globally defined function |
jburhenn | 0:09ef59d2d0f7 | 154 | */ |
jburhenn | 0:09ef59d2d0f7 | 155 | void attach(retT (*function)(argT)) |
jburhenn | 0:09ef59d2d0f7 | 156 | { |
jburhenn | 0:09ef59d2d0f7 | 157 | c_callback = function; |
jburhenn | 0:09ef59d2d0f7 | 158 | } |
jburhenn | 0:09ef59d2d0f7 | 159 | |
jburhenn | 0:09ef59d2d0f7 | 160 | /** Invoke the function attached to the class |
jburhenn | 0:09ef59d2d0f7 | 161 | * @param arg - An argument that is passed into the function handler that is called |
jburhenn | 0:09ef59d2d0f7 | 162 | * @return The return from the function hanlder called by this class |
jburhenn | 0:09ef59d2d0f7 | 163 | */ |
jburhenn | 0:09ef59d2d0f7 | 164 | retT operator()(argT arg) const |
jburhenn | 0:09ef59d2d0f7 | 165 | { |
jburhenn | 0:09ef59d2d0f7 | 166 | if( 0 != c_callback ) { |
jburhenn | 0:09ef59d2d0f7 | 167 | return obj_callback ? (obj_callback->*method_callback)(arg) : (*c_callback)(arg); |
jburhenn | 0:09ef59d2d0f7 | 168 | } |
jburhenn | 0:09ef59d2d0f7 | 169 | return (retT)0; |
jburhenn | 0:09ef59d2d0f7 | 170 | } |
jburhenn | 0:09ef59d2d0f7 | 171 | |
jburhenn | 0:09ef59d2d0f7 | 172 | /** Determine if an callback is currently hooked |
jburhenn | 0:09ef59d2d0f7 | 173 | * @return 1 if a method is hooked, 0 otherwise |
jburhenn | 0:09ef59d2d0f7 | 174 | */ |
jburhenn | 0:09ef59d2d0f7 | 175 | bool attached() |
jburhenn | 0:09ef59d2d0f7 | 176 | { |
jburhenn | 0:09ef59d2d0f7 | 177 | return obj_callback || c_callback; |
jburhenn | 0:09ef59d2d0f7 | 178 | } |
jburhenn | 0:09ef59d2d0f7 | 179 | |
jburhenn | 0:09ef59d2d0f7 | 180 | /** Release a function from the callback hook |
jburhenn | 0:09ef59d2d0f7 | 181 | */ |
jburhenn | 0:09ef59d2d0f7 | 182 | void detach() |
jburhenn | 0:09ef59d2d0f7 | 183 | { |
jburhenn | 0:09ef59d2d0f7 | 184 | obj_callback = 0; |
jburhenn | 0:09ef59d2d0f7 | 185 | c_callback = 0; |
jburhenn | 0:09ef59d2d0f7 | 186 | } |
jburhenn | 0:09ef59d2d0f7 | 187 | |
jburhenn | 0:09ef59d2d0f7 | 188 | private: |
jburhenn | 0:09ef59d2d0f7 | 189 | |
jburhenn | 0:09ef59d2d0f7 | 190 | // empty type used for casting |
jburhenn | 0:09ef59d2d0f7 | 191 | class FPtrDummy; |
jburhenn | 0:09ef59d2d0f7 | 192 | |
jburhenn | 0:09ef59d2d0f7 | 193 | FPtrDummy *obj_callback; |
jburhenn | 0:09ef59d2d0f7 | 194 | |
jburhenn | 0:09ef59d2d0f7 | 195 | /** |
jburhenn | 0:09ef59d2d0f7 | 196 | * @union Funciton |
jburhenn | 0:09ef59d2d0f7 | 197 | * @brief Member or global callback function |
jburhenn | 0:09ef59d2d0f7 | 198 | */ |
jburhenn | 0:09ef59d2d0f7 | 199 | union { |
jburhenn | 0:09ef59d2d0f7 | 200 | retT (*c_callback)(argT); /*!< Footprint for a global function */ |
jburhenn | 0:09ef59d2d0f7 | 201 | retT (FPtrDummy::*method_callback)(argT); /*!< Footprint for a member function */ |
jburhenn | 0:09ef59d2d0f7 | 202 | }; |
jburhenn | 0:09ef59d2d0f7 | 203 | }; |
jburhenn | 0:09ef59d2d0f7 | 204 | |
jburhenn | 0:09ef59d2d0f7 | 205 | #endif |