FPointer - A callback system that allows for 32bit unsigned ints to be passed to and from the callback.

Dependents:   FYPFinalProgram FYPFinalizeProgram KEYS SaveKeypad ... more

Committer:
AjK
Date:
Tue Jan 18 17:31:19 2011 +0000
Revision:
0:fcfb13f40846
Child:
1:d7803001a259
1.0 Initial release

Who changed what in which revision?

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