From e806b89df6a7ca62f3b40a68ff7d286dce038906 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 18 Jul 2016 15:51:07 -0500 Subject: [PATCH 1/2] Fixed handling of const objects in Callback class Before, the following results in a compilation error: const struct Object *obj; void obj_doit(const Object *obj); Callback cb(obj, obj_doit); This is especially noticable when migrating from the old Thread constructor, which previously _required_ const. Short term fix for all cv qualifiers through a C cast: void *_obj = (void*)obj; --- hal/api/Callback.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/hal/api/Callback.h b/hal/api/Callback.h index 377d84a2a0d..8ffd70164c4 100644 --- a/hal/api/Callback.h +++ b/hal/api/Callback.h @@ -80,7 +80,7 @@ class Callback { */ template void attach(T *obj, R (*func)(T*, A0, A1, A2, A3, A4)) { - _obj = static_cast(obj); + _obj = (void*)obj; memcpy(&_func, &func, sizeof func); _thunk = &Callback::_boundthunk; } @@ -221,7 +221,7 @@ class Callback { */ template void attach(T *obj, R (*func)(T*, A0, A1, A2, A3)) { - _obj = static_cast(obj); + _obj = (void*)obj; memcpy(&_func, &func, sizeof func); _thunk = &Callback::_boundthunk; } @@ -362,7 +362,7 @@ class Callback { */ template void attach(T *obj, R (*func)(T*, A0, A1, A2)) { - _obj = static_cast(obj); + _obj = (void*)obj; memcpy(&_func, &func, sizeof func); _thunk = &Callback::_boundthunk; } @@ -503,7 +503,7 @@ class Callback { */ template void attach(T *obj, R (*func)(T*, A0, A1)) { - _obj = static_cast(obj); + _obj = (void*)obj; memcpy(&_func, &func, sizeof func); _thunk = &Callback::_boundthunk; } @@ -644,7 +644,7 @@ class Callback { */ template void attach(T *obj, R (*func)(T*, A0)) { - _obj = static_cast(obj); + _obj = (void*)obj; memcpy(&_func, &func, sizeof func); _thunk = &Callback::_boundthunk; } @@ -785,7 +785,7 @@ class Callback { */ template void attach(T *obj, R (*func)(T*)) { - _obj = static_cast(obj); + _obj = (void*)obj; memcpy(&_func, &func, sizeof func); _thunk = &Callback::_boundthunk; } From 8e60fdd919fde8f575ead0ac404b05b43e326f9b Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 20 Jul 2016 19:27:38 -0500 Subject: [PATCH 2/2] Added tests for cv-qualifier c-objects in Callback class --- TESTS/mbed_drivers/callback/main.cpp | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/TESTS/mbed_drivers/callback/main.cpp b/TESTS/mbed_drivers/callback/main.cpp index 9acdee71113..4e169a5a6c7 100644 --- a/TESTS/mbed_drivers/callback/main.cpp +++ b/TESTS/mbed_drivers/callback/main.cpp @@ -48,6 +48,48 @@ T bound_func1(Thing *t, T a0) { return t->t | a0; } template T bound_func0(Thing *t) { return t->t; } +// const bound functions +template +T const_func5(const Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } +template +T const_func4(const Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T const_func2(const Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_func1(const Thing *t, T a0) { return t->t | a0; } +template +T const_func0(const Thing *t) { return t->t; } + +// volatile bound functions +template +T volatile_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } +template +T volatile_func4(volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T volatile_func2(volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T volatile_func1(volatile Thing *t, T a0) { return t->t | a0; } +template +T volatile_func0(volatile Thing *t) { return t->t; } + +// const volatil bound functions +template +T const_volatile_func5(const volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } +template +T const_volatile_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T const_volatile_func3(const volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T const_volatile_func2(const volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_volatile_func1(const volatile Thing *t, T a0) { return t->t | a0; } +template +T const_volatile_func0(const volatile Thing *t) { return t->t; } + // function call and result verification template @@ -133,6 +175,9 @@ void test_dispatch5() { Verifier::verify5(static_func5); Verifier::verify5(&thing, &Thing::member_func5); Verifier::verify5(&thing, &bound_func5); + Verifier::verify5((const Thing*)&thing, &const_func5); + Verifier::verify5((volatile Thing*)&thing, &volatile_func5); + Verifier::verify5((const volatile Thing*)&thing, &const_volatile_func5); Callback callback(static_func5); Verifier::verify5(callback); @@ -147,6 +192,9 @@ void test_dispatch4() { Verifier::verify4(static_func4); Verifier::verify4(&thing, &Thing::member_func4); Verifier::verify4(&thing, &bound_func4); + Verifier::verify4((const Thing*)&thing, &const_func4); + Verifier::verify4((volatile Thing*)&thing, &volatile_func4); + Verifier::verify4((const volatile Thing*)&thing, &const_volatile_func4); Callback callback(static_func4); Verifier::verify4(callback); @@ -161,6 +209,9 @@ void test_dispatch3() { Verifier::verify3(static_func3); Verifier::verify3(&thing, &Thing::member_func3); Verifier::verify3(&thing, &bound_func3); + Verifier::verify3((const Thing*)&thing, &const_func3); + Verifier::verify3((volatile Thing*)&thing, &volatile_func3); + Verifier::verify3((const volatile Thing*)&thing, &const_volatile_func3); Callback callback(static_func3); Verifier::verify3(callback); @@ -175,6 +226,9 @@ void test_dispatch2() { Verifier::verify2(static_func2); Verifier::verify2(&thing, &Thing::member_func2); Verifier::verify2(&thing, &bound_func2); + Verifier::verify2((const Thing*)&thing, &const_func2); + Verifier::verify2((volatile Thing*)&thing, &volatile_func2); + Verifier::verify2((const volatile Thing*)&thing, &const_volatile_func2); Callback callback(static_func2); Verifier::verify2(callback); @@ -189,6 +243,9 @@ void test_dispatch1() { Verifier::verify1(static_func1); Verifier::verify1(&thing, &Thing::member_func1); Verifier::verify1(&thing, &bound_func1); + Verifier::verify1((const Thing*)&thing, &const_func1); + Verifier::verify1((volatile Thing*)&thing, &volatile_func1); + Verifier::verify1((const volatile Thing*)&thing, &const_volatile_func1); Callback callback(static_func1); Verifier::verify1(callback); @@ -203,6 +260,9 @@ void test_dispatch0() { Verifier::verify0(static_func0); Verifier::verify0(&thing, &Thing::member_func0); Verifier::verify0(&thing, &bound_func0); + Verifier::verify0((const Thing*)&thing, &const_func0); + Verifier::verify0((volatile Thing*)&thing, &volatile_func0); + Verifier::verify0((const volatile Thing*)&thing, &const_volatile_func0); Callback callback(static_func0); Verifier::verify0(callback);