FPointer - A callback system that allows for 32bit unsigned ints to be passed to and from the callback.
Dependents: FYPFinalProgram FYPFinalizeProgram KEYS SaveKeypad ... more
Revision 1:d7803001a259, committed 2011-01-18
- Comitter:
- AjK
- Date:
- Tue Jan 18 22:40:19 2011 +0000
- Parent:
- 0:fcfb13f40846
- Child:
- 2:56e309e76c19
- Commit message:
- 1.1 See ChangeLog.h
Changed in this revision
--- a/ChangeLog.h Tue Jan 18 17:31:19 2011 +0000 +++ b/ChangeLog.h Tue Jan 18 22:40:19 2011 +0000 @@ -1,5 +1,5 @@ /* - Copyright (c) 2010 Andy Kirkham + Copyright (c) 2011 Andy Kirkham Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -19,6 +19,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + 1.1 18/Jan/2011 + Fixed the date in the copyright. + Fixed some typos. + Added the uint32_t call(void); method to allow simple calling the + calback without passing an argument. The callback function itself + still has to have a prototype uint32_t callback(uint32_t). 1.0 18/Jan/2011 Initial release
--- a/FPointer.h Tue Jan 18 17:31:19 2011 +0000 +++ b/FPointer.h Tue Jan 18 22:40:19 2011 +0000 @@ -52,7 +52,7 @@ * data structure thus allowing the callback to again get the values. * * Note, when passing pointers to variables to the callback, if the callback - * function/method changes that variables value then it will also change the + * function/method changes that variable's value then it will also change the * value the caller sees. If C pointers are new to you, you are strongly * advised to read up on the subject. It's pointers that often get beginners * into trouble when mis-used. @@ -67,10 +67,10 @@ protected: - // C callback function pointer for to pass read data to. + //! C callback function pointer. uint32_t (*c_callback)(uint32_t); - // C++ callback object/method pointer for to pass read data to. + //! C++ callback object/method pointer. FPointerDummy *obj_callback; uint32_t (FPointerDummy::*method_callback)(uint32_t); @@ -114,20 +114,44 @@ method_callback = (uint32_t (FPointerDummy::*)(uint32_t))method; } - /** call + /** call - Overloaded callback initiator. * * call the callback function. + * * @param uint32_t The value to pass to the callback. * @return uint32_t The value the callback returns. */ uint32_t call(uint32_t arg) { - if (c_callback != NULL) return (*c_callback)(arg); + if (c_callback != NULL) { + return (*c_callback)(arg); + } else { - if (obj_callback != NULL && method_callback != NULL) + if (obj_callback != NULL && method_callback != NULL) { return (obj_callback->*method_callback)(arg); + } } - return 0; + return (uint32_t)NULL; } + + /** call - Overloaded callback initiator. + * + * Call the callback function without passing an argument. + * The callback itself is passed NULL. Note, the callback + * prototype should still be <b>uint32_t callback(uint32_t)</b>. + * + * @return uint32_t The value the callback returns. + */ + uint32_t call(void) { + if (c_callback != NULL) { + return (*c_callback)((uint32_t)NULL); + } + else { + if (obj_callback != NULL && method_callback != NULL) { + return (obj_callback->*method_callback)((uint32_t)NULL); + } + } + return (uint32_t)NULL; + } }; }; // namespace AjK ends
--- a/example1.h Tue Jan 18 17:31:19 2011 +0000 +++ b/example1.h Tue Jan 18 22:40:19 2011 +0000 @@ -1,5 +1,5 @@ /* - Copyright (c) 2010 Andy Kirkham + Copyright (c) 2011 Andy Kirkham Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -32,7 +32,7 @@ uint32_t myCallback(uint32_t value) { // Get the value of the count in main - // (deference it) so we know what it is. + // (de-reference it) so we know what it is. int i = *((int *)value); // Then display the bottom four bits of @@ -45,7 +45,7 @@ // What we return doesn't matter as it's // not used in this example but we return // "something" (zero in this case) to keep - // teh compiler happy as it expects us to + // the compiler happy as it expects us to // return something. return 0; }
--- a/example2.h Tue Jan 18 17:31:19 2011 +0000 +++ b/example2.h Tue Jan 18 22:40:19 2011 +0000 @@ -1,5 +1,5 @@ /* - Copyright (c) 2010 Andy Kirkham + Copyright (c) 2011 Andy Kirkham Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
--- a/example3.h Tue Jan 18 17:31:19 2011 +0000 +++ b/example3.h Tue Jan 18 22:40:19 2011 +0000 @@ -1,5 +1,5 @@ /* - Copyright (c) 2010 Andy Kirkham + Copyright (c) 2011 Andy Kirkham Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -39,11 +39,11 @@ float f_value; } MYDATA; -// The call back function. +// The callback function. uint32_t myCallback(uint32_t value) { // Get the value of the count in main - // (deference it) so we know what it is. + // (de-reference it) so we know what it is. // Note, Mbed/LPC17xx pointers are 32bit // values. So we can "cast" a value back // into a pointer. @@ -62,7 +62,7 @@ // What we return doesn't matter as it's // not used in this example but we return // "something" (zero in this case) to keep - // teh compiler happy as it expects us to + // the compiler happy as it expects us to // return something. return 0; } @@ -80,7 +80,7 @@ while(1) { wait(0.5); - // Make the callback passing a pointer + // Make the callback passing a pointer (& means "address of") // to the data structure "data". myPointer.call((uint32_t)&data);