From bf498de1277000d2aa821c9daf2d5c80e6d54105 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 13 May 2016 14:55:48 -0500 Subject: [PATCH] Add backward compatiblity for FunctionPointer class using Callback effectively: typedef Callback FunctionPointerArg1 typedef Callback FunctionPointerArg1 typedef Callback FunctionPointer typedef Callback event_callback_t --- hal/TESTS/api/callback/main.cpp | 19 ++++ hal/api/FunctionPointer.h | 174 ++++---------------------------- hal/api/mbed.h | 1 + 3 files changed, 38 insertions(+), 156 deletions(-) diff --git a/hal/TESTS/api/callback/main.cpp b/hal/TESTS/api/callback/main.cpp index 057b5ac367..27fab43173 100644 --- a/hal/TESTS/api/callback/main.cpp +++ b/hal/TESTS/api/callback/main.cpp @@ -211,6 +211,22 @@ void test_dispatch0() { Verifier::verify0((void*)&callback, &Callback::thunk); } +template +void test_fparg1() { + Thing thing; + FunctionPointerArg1 fp(static_func1); + Verifier::verify1(fp); + Verifier::verify1(fp.get_function()); +} + +template +void test_fparg0() { + Thing thing; + FunctionPointerArg1 fp(static_func0); + Verifier::verify0(fp); + Verifier::verify0(fp.get_function()); +} + // Test setup status_t test_setup(const size_t number_of_cases) { @@ -239,6 +255,9 @@ Case cases[] = { Case("Testing callbacks with 2 uint64s", test_dispatch2), Case("Testing callbacks with 1 uint64s", test_dispatch1), Case("Testing callbacks with 0 uint64s", test_dispatch0), + + Case("Testing FunctionPointerArg1 compatibility", test_fparg1), + Case("Testing FunctionPointer compatibility", test_fparg0), }; Specification specification(test_setup, cases); diff --git a/hal/api/FunctionPointer.h b/hal/api/FunctionPointer.h index 2d49ba035e..b2ef2b971c 100644 --- a/hal/api/FunctionPointer.h +++ b/hal/api/FunctionPointer.h @@ -16,187 +16,49 @@ #ifndef MBED_FUNCTIONPOINTER_H #define MBED_FUNCTIONPOINTER_H +#include "Callback.h" #include #include namespace mbed { -/* If we had variaditic templates, this wouldn't be a problem, but until C++11 is enabled, we are stuck with multiple classes... */ -/** A class for storing and calling a pointer to a static or member function - */ +// Declarations for backwards compatibility +// To be foward compatible, code should adopt the Callback class template -class FunctionPointerArg1{ +class FunctionPointerArg1 : public Callback { public: - /** Create a FunctionPointer, attaching a static function - * - * @param function The static function to attach (default is none) - */ - FunctionPointerArg1(R (*function)(A1) = 0) { - attach(function); - } + FunctionPointerArg1(R (*function)(A1) = 0) + : Callback(function) {} - /** Create a FunctionPointer, attaching a member function - * - * @param object The object pointer to invoke the member function on (i.e. the this pointer) - * @param function The address of the member function to attach - */ template - FunctionPointerArg1(T *object, R (T::*member)(A1)) { - attach(object, member); - } + FunctionPointerArg1(T *object, R (T::*member)(A1)) + : Callback(object, member) {} - /** Attach a static function - * - * @param function The static function to attach (default is none) - */ - void attach(R (*function)(A1)) { - _p.function = function; - _membercaller = 0; + R (*get_function())(A1) { + return *reinterpret_cast(this); } - - /** Attach a member function - * - * @param object The object pointer to invoke the member function on (i.e. the this pointer) - * @param function The address of the member function to attach - */ - template - void attach(T *object, R (T::*member)(A1)) { - _p.object = static_cast(object); - *reinterpret_cast(_member) = member; - _membercaller = &FunctionPointerArg1::membercaller; - } - - /** Call the attached static or member function - */ - R call(A1 a) { - if (_membercaller == 0 && _p.function) { - return _p.function(a); - } else if (_membercaller && _p.object) { - return _membercaller(_p.object, _member, a); - } - return (R)0; - } - - /** Get registered static function - */ - R(*get_function(A1))() { - return _membercaller ? (R(*)(A1))0 : (R(*)(A1))_p.function; - } - -#ifdef MBED_OPERATORS - R operator ()(A1 a) { - return call(a); - } - operator bool(void) const { - return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL; - } -#endif -private: - template - static R membercaller(void *object, uintptr_t *member, A1 a) { - T* o = static_cast(object); - R (T::**m)(A1) = reinterpret_cast(member); - return (o->**m)(a); - } - - union { - R (*function)(A1); // static function pointer - void *object; // object this pointer - } _p; - uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller - R (*_membercaller)(void*, uintptr_t*, A1); // registered membercaller function to convert back and call _m.member on _object }; -/** A class for storing and calling a pointer to a static or member function (R ()(void)) - */ template -class FunctionPointerArg1{ +class FunctionPointerArg1 : public Callback { public: - /** Create a FunctionPointer, attaching a static function - * - * @param function The static function to attach (default is none) - */ - FunctionPointerArg1(R (*function)(void) = 0) { - attach(function); - } + FunctionPointerArg1(R (*function)() = 0) + : Callback(function) {} - /** Create a FunctionPointer, attaching a member function - * - * @param object The object pointer to invoke the member function on (i.e. the this pointer) - * @param function The address of the void member function to attach - */ template - FunctionPointerArg1(T *object, R (T::*member)(void)) { - attach(object, member); - } + FunctionPointerArg1(T *object, R (T::*member)()) + : Callback(object, member) {} - /** Attach a static function - * - * @param function The void static function to attach (default is none) - */ - void attach(R (*function)(void)) { - _p.function = function; - _membercaller = 0; + R (*get_function())() { + return *reinterpret_cast(this); } - - /** Attach a member function - * - * @param object The object pointer to invoke the member function on (i.e. the this pointer) - * @param function The address of the void member function to attach - */ - template - void attach(T *object, R (T::*member)(void)) { - _p.object = static_cast(object); - *reinterpret_cast(_member) = member; - _membercaller = &FunctionPointerArg1::membercaller; - } - - /** Call the attached static or member function - */ - R call(){ - if (_membercaller == 0 && _p.function) { - return _p.function(); - } else if (_membercaller && _p.object) { - return _membercaller(_p.object, _member); - } - return (R)0; - } - - /** Get registered static function - */ - R(*get_function())() { - return _membercaller ? (R(*)())0 : (R(*)())_p.function; - } - -#ifdef MBED_OPERATORS - R operator ()(void) { - return call(); - } - operator bool(void) const { - return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL; - } -#endif - -private: - template - static R membercaller(void *object, uintptr_t *member) { - T* o = static_cast(object); - R (T::**m)(void) = reinterpret_cast(member); - return (o->**m)(); - } - - union { - R (*function)(void); // static function pointer - void *object; // object this pointer - } _p; - uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller - R (*_membercaller)(void*, uintptr_t*); // registered membercaller function to convert back and call _m.member on _object }; typedef FunctionPointerArg1 FunctionPointer; typedef FunctionPointerArg1 event_callback_t; + } // namespace mbed #endif diff --git a/hal/api/mbed.h b/hal/api/mbed.h index 8d615d4c6f..e1a6a12e01 100644 --- a/hal/api/mbed.h +++ b/hal/api/mbed.h @@ -66,6 +66,7 @@ // mbed Non-hardware components #include "Callback.h" +#include "FunctionPointer.h" using namespace mbed; using namespace std;