11
FPointer.h@17:643241424fd2, 2019-10-03 (annotated)
- Committer:
- jRix
- Date:
- Thu Oct 03 13:21:53 2019 +0000
- Revision:
- 17:643241424fd2
11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jRix | 17:643241424fd2 | 1 | /* |
jRix | 17:643241424fd2 | 2 | Copyright (c) 2011 Andy Kirkham |
jRix | 17:643241424fd2 | 3 | |
jRix | 17:643241424fd2 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
jRix | 17:643241424fd2 | 5 | of this software and associated documentation files (the "Software"), to deal |
jRix | 17:643241424fd2 | 6 | in the Software without restriction, including without limitation the rights |
jRix | 17:643241424fd2 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
jRix | 17:643241424fd2 | 8 | copies of the Software, and to permit persons to whom the Software is |
jRix | 17:643241424fd2 | 9 | furnished to do so, subject to the following conditions: |
jRix | 17:643241424fd2 | 10 | |
jRix | 17:643241424fd2 | 11 | The above copyright notice and this permission notice shall be included in |
jRix | 17:643241424fd2 | 12 | all copies or substantial portions of the Software. |
jRix | 17:643241424fd2 | 13 | |
jRix | 17:643241424fd2 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
jRix | 17:643241424fd2 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
jRix | 17:643241424fd2 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
jRix | 17:643241424fd2 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
jRix | 17:643241424fd2 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jRix | 17:643241424fd2 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
jRix | 17:643241424fd2 | 20 | THE SOFTWARE. |
jRix | 17:643241424fd2 | 21 | */ |
jRix | 17:643241424fd2 | 22 | |
jRix | 17:643241424fd2 | 23 | #ifndef AJK_FPOINTER_H |
jRix | 17:643241424fd2 | 24 | #define AJK_FPOINTER_H |
jRix | 17:643241424fd2 | 25 | |
jRix | 17:643241424fd2 | 26 | namespace AjK { |
jRix | 17:643241424fd2 | 27 | |
jRix | 17:643241424fd2 | 28 | class FPointerDummy; |
jRix | 17:643241424fd2 | 29 | |
jRix | 17:643241424fd2 | 30 | /** FPointer - Adds callbacks that take and return a 32bit uint32_t data type. |
jRix | 17:643241424fd2 | 31 | * |
jRix | 17:643241424fd2 | 32 | * The Mbed library supplies a callback using the FunctionPointer object as |
jRix | 17:643241424fd2 | 33 | * defined in FunctionPointer.h However, this callback system does not allow |
jRix | 17:643241424fd2 | 34 | * the caller to pass a value to the callback. Likewise, the callback itself |
jRix | 17:643241424fd2 | 35 | * cannot return a value. |
jRix | 17:643241424fd2 | 36 | * |
jRix | 17:643241424fd2 | 37 | * FPointer operates in the same way but allows the callback function to be |
jRix | 17:643241424fd2 | 38 | * passed one arg, a uint32_t value. Additionally, the callback can return |
jRix | 17:643241424fd2 | 39 | * a single uint32_t value. The reason for using uint32_t is that the Mbed |
jRix | 17:643241424fd2 | 40 | * and the microcontroller (LPC1768) have a natural data size of 32bits and |
jRix | 17:643241424fd2 | 41 | * this means we can use the uint32_t as a pointer. See example1.h for more |
jRix | 17:643241424fd2 | 42 | * information. This example passes an "int" by passing a pointer to that |
jRix | 17:643241424fd2 | 43 | * int as a 32bit value. Using this technique you can pass any value you like. |
jRix | 17:643241424fd2 | 44 | * All you have to do is pass a pointer to your value cast to (uint32_t). Your |
jRix | 17:643241424fd2 | 45 | * callback can the deference it to get the original value. |
jRix | 17:643241424fd2 | 46 | * |
jRix | 17:643241424fd2 | 47 | * example2.h shows how to do the same thing but demostrates how to specify |
jRix | 17:643241424fd2 | 48 | * the callback into a class object/method. |
jRix | 17:643241424fd2 | 49 | * |
jRix | 17:643241424fd2 | 50 | * Finally, example3.h shows how to pass multiple values. In this example we |
jRix | 17:643241424fd2 | 51 | * define a data structure and in the callback we pass a pointer to that |
jRix | 17:643241424fd2 | 52 | * data structure thus allowing the callback to again get the values. |
jRix | 17:643241424fd2 | 53 | * |
jRix | 17:643241424fd2 | 54 | * Note, when passing pointers to variables to the callback, if the callback |
jRix | 17:643241424fd2 | 55 | * function/method changes that variable's value then it will also change the |
jRix | 17:643241424fd2 | 56 | * value the caller sees. If C pointers are new to you, you are strongly |
jRix | 17:643241424fd2 | 57 | * advised to read up on the subject. It's pointers that often get beginners |
jRix | 17:643241424fd2 | 58 | * into trouble when mis-used. |
jRix | 17:643241424fd2 | 59 | * |
jRix | 17:643241424fd2 | 60 | * @see example1.h |
jRix | 17:643241424fd2 | 61 | * @see example2.h |
jRix | 17:643241424fd2 | 62 | * @see example3.h |
jRix | 17:643241424fd2 | 63 | * @see http://mbed.org/handbook/C-Data-Types |
jRix | 17:643241424fd2 | 64 | * @see http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h |
jRix | 17:643241424fd2 | 65 | */ |
jRix | 17:643241424fd2 | 66 | class FPointer { |
jRix | 17:643241424fd2 | 67 | |
jRix | 17:643241424fd2 | 68 | protected: |
jRix | 17:643241424fd2 | 69 | |
jRix | 17:643241424fd2 | 70 | //! C callback function pointer. |
jRix | 17:643241424fd2 | 71 | uint32_t (*c_callback)(uint32_t); |
jRix | 17:643241424fd2 | 72 | |
jRix | 17:643241424fd2 | 73 | //! C++ callback object/method pointer (the object part). |
jRix | 17:643241424fd2 | 74 | FPointerDummy *obj_callback; |
jRix | 17:643241424fd2 | 75 | |
jRix | 17:643241424fd2 | 76 | //! C++ callback object/method pointer (the method part). |
jRix | 17:643241424fd2 | 77 | uint32_t (FPointerDummy::*method_callback)(uint32_t); |
jRix | 17:643241424fd2 | 78 | |
jRix | 17:643241424fd2 | 79 | public: |
jRix | 17:643241424fd2 | 80 | |
jRix | 17:643241424fd2 | 81 | /** Constructor |
jRix | 17:643241424fd2 | 82 | */ |
jRix | 17:643241424fd2 | 83 | FPointer() { |
jRix | 17:643241424fd2 | 84 | c_callback = NULL; |
jRix | 17:643241424fd2 | 85 | obj_callback = NULL; |
jRix | 17:643241424fd2 | 86 | method_callback = NULL; |
jRix | 17:643241424fd2 | 87 | } |
jRix | 17:643241424fd2 | 88 | |
jRix | 17:643241424fd2 | 89 | /** attach - Overloaded attachment function. |
jRix | 17:643241424fd2 | 90 | * |
jRix | 17:643241424fd2 | 91 | * Attach a C type function pointer as the callback. |
jRix | 17:643241424fd2 | 92 | * |
jRix | 17:643241424fd2 | 93 | * Note, the callback function prototype must be:- |
jRix | 17:643241424fd2 | 94 | * @code |
jRix | 17:643241424fd2 | 95 | * uint32_t myCallbackFunction(uint32_t); |
jRix | 17:643241424fd2 | 96 | * @endcode |
jRix | 17:643241424fd2 | 97 | * @param A C function pointer to call. |
jRix | 17:643241424fd2 | 98 | */ |
jRix | 17:643241424fd2 | 99 | void attach(uint32_t (*function)(uint32_t) = 0) { c_callback = function; } |
jRix | 17:643241424fd2 | 100 | |
jRix | 17:643241424fd2 | 101 | /** attach - Overloaded attachment function. |
jRix | 17:643241424fd2 | 102 | * |
jRix | 17:643241424fd2 | 103 | * Attach a C++ type object/method pointer as the callback. |
jRix | 17:643241424fd2 | 104 | * |
jRix | 17:643241424fd2 | 105 | * Note, the callback method prototype must be:- |
jRix | 17:643241424fd2 | 106 | * @code |
jRix | 17:643241424fd2 | 107 | * public: |
jRix | 17:643241424fd2 | 108 | * uint32_t myCallbackFunction(uint32_t); |
jRix | 17:643241424fd2 | 109 | * @endcode |
jRix | 17:643241424fd2 | 110 | * @param A C++ object pointer. |
jRix | 17:643241424fd2 | 111 | * @param A C++ method within the object to call. |
jRix | 17:643241424fd2 | 112 | */ |
jRix | 17:643241424fd2 | 113 | template<class T> |
jRix | 17:643241424fd2 | 114 | void attach(T* item, uint32_t (T::*method)(uint32_t)) { |
jRix | 17:643241424fd2 | 115 | obj_callback = (FPointerDummy *)item; |
jRix | 17:643241424fd2 | 116 | method_callback = (uint32_t (FPointerDummy::*)(uint32_t))method; |
jRix | 17:643241424fd2 | 117 | } |
jRix | 17:643241424fd2 | 118 | |
jRix | 17:643241424fd2 | 119 | /** call - Overloaded callback initiator. |
jRix | 17:643241424fd2 | 120 | * |
jRix | 17:643241424fd2 | 121 | * call the callback function. |
jRix | 17:643241424fd2 | 122 | * |
jRix | 17:643241424fd2 | 123 | * @param uint32_t The value to pass to the callback. |
jRix | 17:643241424fd2 | 124 | * @return uint32_t The value the callback returns. |
jRix | 17:643241424fd2 | 125 | */ |
jRix | 17:643241424fd2 | 126 | uint32_t call(uint32_t arg) { |
jRix | 17:643241424fd2 | 127 | if (c_callback != NULL) { |
jRix | 17:643241424fd2 | 128 | return (*c_callback)(arg); |
jRix | 17:643241424fd2 | 129 | } |
jRix | 17:643241424fd2 | 130 | else { |
jRix | 17:643241424fd2 | 131 | if (obj_callback != NULL && method_callback != NULL) { |
jRix | 17:643241424fd2 | 132 | return (obj_callback->*method_callback)(arg); |
jRix | 17:643241424fd2 | 133 | } |
jRix | 17:643241424fd2 | 134 | } |
jRix | 17:643241424fd2 | 135 | return (uint32_t)NULL; |
jRix | 17:643241424fd2 | 136 | } |
jRix | 17:643241424fd2 | 137 | |
jRix | 17:643241424fd2 | 138 | /** call - Overloaded callback initiator. |
jRix | 17:643241424fd2 | 139 | * |
jRix | 17:643241424fd2 | 140 | * Call the callback function without passing an argument. |
jRix | 17:643241424fd2 | 141 | * The callback itself is passed NULL. Note, the callback |
jRix | 17:643241424fd2 | 142 | * prototype should still be <b>uint32_t callback(uint32_t)</b>. |
jRix | 17:643241424fd2 | 143 | * |
jRix | 17:643241424fd2 | 144 | * @return uint32_t The value the callback returns. |
jRix | 17:643241424fd2 | 145 | */ |
jRix | 17:643241424fd2 | 146 | uint32_t call(void) { |
jRix | 17:643241424fd2 | 147 | if (c_callback != NULL) { |
jRix | 17:643241424fd2 | 148 | return (*c_callback)((uint32_t)NULL); |
jRix | 17:643241424fd2 | 149 | } |
jRix | 17:643241424fd2 | 150 | else { |
jRix | 17:643241424fd2 | 151 | if (obj_callback != NULL && method_callback != NULL) { |
jRix | 17:643241424fd2 | 152 | return (obj_callback->*method_callback)((uint32_t)NULL); |
jRix | 17:643241424fd2 | 153 | } |
jRix | 17:643241424fd2 | 154 | } |
jRix | 17:643241424fd2 | 155 | return (uint32_t)NULL; |
jRix | 17:643241424fd2 | 156 | } |
jRix | 17:643241424fd2 | 157 | }; |
jRix | 17:643241424fd2 | 158 | |
jRix | 17:643241424fd2 | 159 | }; // namespace AjK ends |
jRix | 17:643241424fd2 | 160 | |
jRix | 17:643241424fd2 | 161 | using namespace AjK; |
jRix | 17:643241424fd2 | 162 | |
jRix | 17:643241424fd2 | 163 | #endif |