diff --git a/TESTS/mbed_functional/callback/main.cpp b/TESTS/mbed_functional/callback/main.cpp new file mode 100644 index 00000000000..8442448e538 --- /dev/null +++ b/TESTS/mbed_functional/callback/main.cpp @@ -0,0 +1,351 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +using namespace utest::v1; + + +// static functions +template +T static_func0() { return 0; } +template +T static_func1(T a0) { return 0 | a0; } +template +T static_func2(T a0, T a1) { return 0 | a0 | a1; } +template +T static_func3(T a0, T a1, T a2) { return 0 | a0 | a1 | a2; } +template +T static_func4(T a0, T a1, T a2, T a3) { return 0 | a0 | a1 | a2 | a3; } +template +T static_func5(T a0, T a1, T a2, T a3, T a4) { return 0 | a0 | a1 | a2 | a3 | a4; } + +// class functions +template +struct Thing { + T t; + Thing() : t(0x80) {} + + T member_func0() { return t; } + T member_func1(T a0) { return t | a0; } + T member_func2(T a0, T a1) { return t | a0 | a1; } + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } + + T const_member_func0() const { return t; } + T const_member_func1(T a0) const { return t | a0; } + T const_member_func2(T a0, T a1) const { return t | a0 | a1; } + T const_member_func3(T a0, T a1, T a2) const { return t | a0 | a1 | a2; } + T const_member_func4(T a0, T a1, T a2, T a3) const { return t | a0 | a1 | a2 | a3; } + T const_member_func5(T a0, T a1, T a2, T a3, T a4) const { return t | a0 | a1 | a2 | a3 | a4; } + + T volatile_member_func0() volatile { return t; } + T volatile_member_func1(T a0) volatile { return t | a0; } + T volatile_member_func2(T a0, T a1) volatile { return t | a0 | a1; } + T volatile_member_func3(T a0, T a1, T a2) volatile { return t | a0 | a1 | a2; } + T volatile_member_func4(T a0, T a1, T a2, T a3) volatile { return t | a0 | a1 | a2 | a3; } + T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile { return t | a0 | a1 | a2 | a3 | a4; } + + T const_volatile_member_func0() const volatile { return t; } + T const_volatile_member_func1(T a0) const volatile { return t | a0; } + T const_volatile_member_func2(T a0, T a1) const volatile { return t | a0 | a1; } + T const_volatile_member_func3(T a0, T a1, T a2) const volatile { return t | a0 | a1 | a2; } + T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile { return t | a0 | a1 | a2 | a3; } + T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile { return t | a0 | a1 | a2 | a3 | a4; } +}; + +// bound functions +template +T bound_func0(Thing *t) { return t->t; } +template +T bound_func1(Thing *t, T a0) { return t->t | a0; } +template +T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const bound functions +template +T const_func0(const Thing *t) { return t->t; } +template +T const_func1(const Thing *t, T a0) { return t->t | a0; } +template +T const_func2(const Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +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_func5(const Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// volatile bound functions +template +T volatile_func0(volatile Thing *t) { return t->t; } +template +T volatile_func1(volatile Thing *t, T a0) { return t->t | a0; } +template +T volatile_func2(volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +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_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const volatile bound functions +template +T const_volatile_func0(const volatile Thing *t) { return t->t; } +template +T const_volatile_func1(const volatile Thing *t, T a0) { return t->t | a0; } +template +T const_volatile_func2(const volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +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_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +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; } + + +// function call and result verification +template +struct Verifier { + static void verify0(Callback func) { + T result = func(); + TEST_ASSERT_EQUAL(result, 0x00); + } + + template + static void verify0(O *obj, M method) { + Callback func(obj, method); + T result = func(); + TEST_ASSERT_EQUAL(result, 0x80); + } + + static void verify1(Callback func) { + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0)); + } + + template + static void verify1(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0)); + } + + static void verify2(Callback func) { + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1)); + } + + template + static void verify2(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1)); + } + + static void verify3(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + template + static void verify3(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + static void verify4(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + template + static void verify4(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + static void verify5(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } + + template + static void verify5(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } +}; + + +// test dispatch +template +void test_dispatch0() { + Thing thing; + Verifier::verify0(static_func0); + Verifier::verify0(&thing, &Thing::member_func0); + Verifier::verify0((const Thing*)&thing, &Thing::const_member_func0); + Verifier::verify0((volatile Thing*)&thing, &Thing::volatile_member_func0); + Verifier::verify0((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify0(callback(static_func0)); + + Callback cb(static_func0); + Verifier::verify0(cb); + cb = static_func0; + Verifier::verify0(cb); + cb.attach(&thing, &bound_func0); + Verifier::verify0(&cb, &Callback::call); + Verifier::verify0((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch1() { + Thing thing; + Verifier::verify1(static_func1); + Verifier::verify1(&thing, &Thing::member_func1); + Verifier::verify1((const Thing*)&thing, &Thing::const_member_func1); + Verifier::verify1((volatile Thing*)&thing, &Thing::volatile_member_func1); + Verifier::verify1((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify1(callback(static_func1)); + + Callback cb(static_func1); + Verifier::verify1(cb); + cb = static_func1; + Verifier::verify1(cb); + cb.attach(&thing, &bound_func1); + Verifier::verify1(&cb, &Callback::call); + Verifier::verify1((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch2() { + Thing thing; + Verifier::verify2(static_func2); + Verifier::verify2(&thing, &Thing::member_func2); + Verifier::verify2((const Thing*)&thing, &Thing::const_member_func2); + Verifier::verify2((volatile Thing*)&thing, &Thing::volatile_member_func2); + Verifier::verify2((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify2(callback(static_func2)); + + Callback cb(static_func2); + Verifier::verify2(cb); + cb = static_func2; + Verifier::verify2(cb); + cb.attach(&thing, &bound_func2); + Verifier::verify2(&cb, &Callback::call); + Verifier::verify2((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch3() { + Thing thing; + Verifier::verify3(static_func3); + Verifier::verify3(&thing, &Thing::member_func3); + Verifier::verify3((const Thing*)&thing, &Thing::const_member_func3); + Verifier::verify3((volatile Thing*)&thing, &Thing::volatile_member_func3); + Verifier::verify3((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify3(callback(static_func3)); + + Callback cb(static_func3); + Verifier::verify3(cb); + cb = static_func3; + Verifier::verify3(cb); + cb.attach(&thing, &bound_func3); + Verifier::verify3(&cb, &Callback::call); + Verifier::verify3((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch4() { + Thing thing; + Verifier::verify4(static_func4); + Verifier::verify4(&thing, &Thing::member_func4); + Verifier::verify4((const Thing*)&thing, &Thing::const_member_func4); + Verifier::verify4((volatile Thing*)&thing, &Thing::volatile_member_func4); + Verifier::verify4((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify4(callback(static_func4)); + + Callback cb(static_func4); + Verifier::verify4(cb); + cb = static_func4; + Verifier::verify4(cb); + cb.attach(&thing, &bound_func4); + Verifier::verify4(&cb, &Callback::call); + Verifier::verify4((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch5() { + Thing thing; + Verifier::verify5(static_func5); + Verifier::verify5(&thing, &Thing::member_func5); + Verifier::verify5((const Thing*)&thing, &Thing::const_member_func5); + Verifier::verify5((volatile Thing*)&thing, &Thing::volatile_member_func5); + Verifier::verify5((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify5(callback(static_func5)); + + Callback cb(static_func5); + Verifier::verify5(cb); + cb = static_func5; + Verifier::verify5(cb); + cb.attach(&thing, &bound_func5); + Verifier::verify5(&cb, &Callback::call); + Verifier::verify5((void*)&cb, &Callback::thunk); +} + + +// Test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(10, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing callbacks with 0 ints", test_dispatch0), + Case("Testing callbacks with 1 ints", test_dispatch1), + Case("Testing callbacks with 2 ints", test_dispatch2), + Case("Testing callbacks with 3 ints", test_dispatch3), + Case("Testing callbacks with 4 ints", test_dispatch4), + Case("Testing callbacks with 5 ints", test_dispatch5), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +} diff --git a/TESTS/mbed_functional/callback_big/main.cpp b/TESTS/mbed_functional/callback_big/main.cpp new file mode 100644 index 00000000000..47630a3f21c --- /dev/null +++ b/TESTS/mbed_functional/callback_big/main.cpp @@ -0,0 +1,351 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +using namespace utest::v1; + + +// static functions +template +T static_func0() { return 0; } +template +T static_func1(T a0) { return 0 | a0; } +template +T static_func2(T a0, T a1) { return 0 | a0 | a1; } +template +T static_func3(T a0, T a1, T a2) { return 0 | a0 | a1 | a2; } +template +T static_func4(T a0, T a1, T a2, T a3) { return 0 | a0 | a1 | a2 | a3; } +template +T static_func5(T a0, T a1, T a2, T a3, T a4) { return 0 | a0 | a1 | a2 | a3 | a4; } + +// class functions +template +struct Thing { + T t; + Thing() : t(0x80) {} + + T member_func0() { return t; } + T member_func1(T a0) { return t | a0; } + T member_func2(T a0, T a1) { return t | a0 | a1; } + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } + + T const_member_func0() const { return t; } + T const_member_func1(T a0) const { return t | a0; } + T const_member_func2(T a0, T a1) const { return t | a0 | a1; } + T const_member_func3(T a0, T a1, T a2) const { return t | a0 | a1 | a2; } + T const_member_func4(T a0, T a1, T a2, T a3) const { return t | a0 | a1 | a2 | a3; } + T const_member_func5(T a0, T a1, T a2, T a3, T a4) const { return t | a0 | a1 | a2 | a3 | a4; } + + T volatile_member_func0() volatile { return t; } + T volatile_member_func1(T a0) volatile { return t | a0; } + T volatile_member_func2(T a0, T a1) volatile { return t | a0 | a1; } + T volatile_member_func3(T a0, T a1, T a2) volatile { return t | a0 | a1 | a2; } + T volatile_member_func4(T a0, T a1, T a2, T a3) volatile { return t | a0 | a1 | a2 | a3; } + T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile { return t | a0 | a1 | a2 | a3 | a4; } + + T const_volatile_member_func0() const volatile { return t; } + T const_volatile_member_func1(T a0) const volatile { return t | a0; } + T const_volatile_member_func2(T a0, T a1) const volatile { return t | a0 | a1; } + T const_volatile_member_func3(T a0, T a1, T a2) const volatile { return t | a0 | a1 | a2; } + T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile { return t | a0 | a1 | a2 | a3; } + T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile { return t | a0 | a1 | a2 | a3 | a4; } +}; + +// bound functions +template +T bound_func0(Thing *t) { return t->t; } +template +T bound_func1(Thing *t, T a0) { return t->t | a0; } +template +T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const bound functions +template +T const_func0(const Thing *t) { return t->t; } +template +T const_func1(const Thing *t, T a0) { return t->t | a0; } +template +T const_func2(const Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +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_func5(const Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// volatile bound functions +template +T volatile_func0(volatile Thing *t) { return t->t; } +template +T volatile_func1(volatile Thing *t, T a0) { return t->t | a0; } +template +T volatile_func2(volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +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_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const volatile bound functions +template +T const_volatile_func0(const volatile Thing *t) { return t->t; } +template +T const_volatile_func1(const volatile Thing *t, T a0) { return t->t | a0; } +template +T const_volatile_func2(const volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +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_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +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; } + + +// function call and result verification +template +struct Verifier { + static void verify0(Callback func) { + T result = func(); + TEST_ASSERT_EQUAL(result, 0x00); + } + + template + static void verify0(O *obj, M method) { + Callback func(obj, method); + T result = func(); + TEST_ASSERT_EQUAL(result, 0x80); + } + + static void verify1(Callback func) { + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0)); + } + + template + static void verify1(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0)); + } + + static void verify2(Callback func) { + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1)); + } + + template + static void verify2(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1)); + } + + static void verify3(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + template + static void verify3(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + static void verify4(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + template + static void verify4(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + static void verify5(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } + + template + static void verify5(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } +}; + + +// test dispatch +template +void test_dispatch0() { + Thing thing; + Verifier::verify0(static_func0); + Verifier::verify0(&thing, &Thing::member_func0); + Verifier::verify0((const Thing*)&thing, &Thing::const_member_func0); + Verifier::verify0((volatile Thing*)&thing, &Thing::volatile_member_func0); + Verifier::verify0((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify0(callback(static_func0)); + + Callback cb(static_func0); + Verifier::verify0(cb); + cb = static_func0; + Verifier::verify0(cb); + cb.attach(&thing, &bound_func0); + Verifier::verify0(&cb, &Callback::call); + Verifier::verify0((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch1() { + Thing thing; + Verifier::verify1(static_func1); + Verifier::verify1(&thing, &Thing::member_func1); + Verifier::verify1((const Thing*)&thing, &Thing::const_member_func1); + Verifier::verify1((volatile Thing*)&thing, &Thing::volatile_member_func1); + Verifier::verify1((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify1(callback(static_func1)); + + Callback cb(static_func1); + Verifier::verify1(cb); + cb = static_func1; + Verifier::verify1(cb); + cb.attach(&thing, &bound_func1); + Verifier::verify1(&cb, &Callback::call); + Verifier::verify1((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch2() { + Thing thing; + Verifier::verify2(static_func2); + Verifier::verify2(&thing, &Thing::member_func2); + Verifier::verify2((const Thing*)&thing, &Thing::const_member_func2); + Verifier::verify2((volatile Thing*)&thing, &Thing::volatile_member_func2); + Verifier::verify2((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify2(callback(static_func2)); + + Callback cb(static_func2); + Verifier::verify2(cb); + cb = static_func2; + Verifier::verify2(cb); + cb.attach(&thing, &bound_func2); + Verifier::verify2(&cb, &Callback::call); + Verifier::verify2((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch3() { + Thing thing; + Verifier::verify3(static_func3); + Verifier::verify3(&thing, &Thing::member_func3); + Verifier::verify3((const Thing*)&thing, &Thing::const_member_func3); + Verifier::verify3((volatile Thing*)&thing, &Thing::volatile_member_func3); + Verifier::verify3((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify3(callback(static_func3)); + + Callback cb(static_func3); + Verifier::verify3(cb); + cb = static_func3; + Verifier::verify3(cb); + cb.attach(&thing, &bound_func3); + Verifier::verify3(&cb, &Callback::call); + Verifier::verify3((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch4() { + Thing thing; + Verifier::verify4(static_func4); + Verifier::verify4(&thing, &Thing::member_func4); + Verifier::verify4((const Thing*)&thing, &Thing::const_member_func4); + Verifier::verify4((volatile Thing*)&thing, &Thing::volatile_member_func4); + Verifier::verify4((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify4(callback(static_func4)); + + Callback cb(static_func4); + Verifier::verify4(cb); + cb = static_func4; + Verifier::verify4(cb); + cb.attach(&thing, &bound_func4); + Verifier::verify4(&cb, &Callback::call); + Verifier::verify4((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch5() { + Thing thing; + Verifier::verify5(static_func5); + Verifier::verify5(&thing, &Thing::member_func5); + Verifier::verify5((const Thing*)&thing, &Thing::const_member_func5); + Verifier::verify5((volatile Thing*)&thing, &Thing::volatile_member_func5); + Verifier::verify5((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify5(callback(static_func5)); + + Callback cb(static_func5); + Verifier::verify5(cb); + cb = static_func5; + Verifier::verify5(cb); + cb.attach(&thing, &bound_func5); + Verifier::verify5(&cb, &Callback::call); + Verifier::verify5((void*)&cb, &Callback::thunk); +} + + +// Test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(10, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing callbacks with 0 uint64s", test_dispatch0), + Case("Testing callbacks with 1 uint64s", test_dispatch1), + Case("Testing callbacks with 2 uint64s", test_dispatch2), + Case("Testing callbacks with 3 uint64s", test_dispatch3), + Case("Testing callbacks with 4 uint64s", test_dispatch4), + Case("Testing callbacks with 5 uint64s", test_dispatch5), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/callback/main.cpp b/TESTS/mbed_functional/callback_small/main.cpp similarity index 52% rename from TESTS/mbed_drivers/callback/main.cpp rename to TESTS/mbed_functional/callback_small/main.cpp index 4e169a5a6c7..e5fa15b4b68 100644 --- a/TESTS/mbed_drivers/callback/main.cpp +++ b/TESTS/mbed_functional/callback_small/main.cpp @@ -8,17 +8,17 @@ using namespace utest::v1; // static functions template -T static_func5(T a0, T a1, T a2, T a3, T a4) { return a0 | a1 | a2 | a3 | a4; } +T static_func0() { return 0; } template -T static_func4(T a0, T a1, T a2, T a3) { return a0 | a1 | a2 | a3; } +T static_func1(T a0) { return 0 | a0; } template -T static_func3(T a0, T a1, T a2) { return a0 | a1 | a2; } +T static_func2(T a0, T a1) { return 0 | a0 | a1; } template -T static_func2(T a0, T a1) { return a0 | a1; } +T static_func3(T a0, T a1, T a2) { return 0 | a0 | a1 | a2; } template -T static_func1(T a0) { return a0; } +T static_func4(T a0, T a1, T a2, T a3) { return 0 | a0 | a1 | a2 | a3; } template -T static_func0() { return 0; } +T static_func5(T a0, T a1, T a2, T a3, T a4) { return 0 | a0 | a1 | a2 | a3 | a4; } // class functions template @@ -26,198 +26,214 @@ struct Thing { T t; Thing() : t(0x80) {} - T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } - T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } - T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } - T member_func2(T a0, T a1) { return t | a0 | a1; } - T member_func1(T a0) { return t | a0; } T member_func0() { return t; } + T member_func1(T a0) { return t | a0; } + T member_func2(T a0, T a1) { return t | a0 | a1; } + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } + + T const_member_func0() const { return t; } + T const_member_func1(T a0) const { return t | a0; } + T const_member_func2(T a0, T a1) const { return t | a0 | a1; } + T const_member_func3(T a0, T a1, T a2) const { return t | a0 | a1 | a2; } + T const_member_func4(T a0, T a1, T a2, T a3) const { return t | a0 | a1 | a2 | a3; } + T const_member_func5(T a0, T a1, T a2, T a3, T a4) const { return t | a0 | a1 | a2 | a3 | a4; } + + T volatile_member_func0() volatile { return t; } + T volatile_member_func1(T a0) volatile { return t | a0; } + T volatile_member_func2(T a0, T a1) volatile { return t | a0 | a1; } + T volatile_member_func3(T a0, T a1, T a2) volatile { return t | a0 | a1 | a2; } + T volatile_member_func4(T a0, T a1, T a2, T a3) volatile { return t | a0 | a1 | a2 | a3; } + T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile { return t | a0 | a1 | a2 | a3 | a4; } + + T const_volatile_member_func0() const volatile { return t; } + T const_volatile_member_func1(T a0) const volatile { return t | a0; } + T const_volatile_member_func2(T a0, T a1) const volatile { return t | a0 | a1; } + T const_volatile_member_func3(T a0, T a1, T a2) const volatile { return t | a0 | a1 | a2; } + T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile { return t | a0 | a1 | a2 | a3; } + T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile { return t | a0 | a1 | a2 | a3 | a4; } }; // bound functions template -T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } -template -T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +T bound_func0(Thing *t) { return t->t; } template -T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +T bound_func1(Thing *t, T a0) { return t->t | a0; } template T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } template -T bound_func1(Thing *t, T a0) { return t->t | a0; } +T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } template -T bound_func0(Thing *t) { return t->t; } +T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } // 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; } +T const_func0(const Thing *t) { return t->t; } template -T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +T const_func1(const Thing *t, T a0) { return t->t | a0; } 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; } +T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } template -T const_func0(const Thing *t) { return t->t; } +T const_func4(const Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +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; } // 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; } +T volatile_func0(volatile Thing *t) { return t->t; } template -T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +T volatile_func1(volatile Thing *t, T a0) { return t->t | a0; } 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; } +T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } template -T volatile_func0(volatile Thing *t) { return t->t; } - -// const volatil bound functions +T volatile_func4(volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } 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; } +T volatile_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const volatile bound functions template -T const_volatile_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +T const_volatile_func0(const volatile Thing *t) { return t->t; } template -T const_volatile_func3(const volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +T const_volatile_func1(const volatile Thing *t, T a0) { return t->t | a0; } 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; } +T const_volatile_func3(const volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } template -T const_volatile_func0(const volatile Thing *t) { return t->t; } +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_func5(const volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } // function call and result verification template struct Verifier { - static void verify5(Callback func) { - T result = func(0x01, 0x02, 0x04, 0x08, 0x10); - TEST_ASSERT_EQUAL(result, 0x1f); + static void verify0(Callback func) { + T result = func(); + TEST_ASSERT_EQUAL(result, 0x00); } template - static void verify5(O *obj, M method) { - Callback func(obj, method); - T result = func(0x01, 0x02, 0x04, 0x08, 0x10); - TEST_ASSERT_EQUAL(result, 0x9f); + static void verify0(O *obj, M method) { + Callback func(obj, method); + T result = func(); + TEST_ASSERT_EQUAL(result, 0x80); } - static void verify4(Callback func) { - T result = func(0x01, 0x02, 0x04, 0x08); - TEST_ASSERT_EQUAL(result, 0x0f); + static void verify1(Callback func) { + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0)); } template - static void verify4(O *obj, M method) { - Callback func(obj, method); - T result = func(0x01, 0x02, 0x04, 0x08); - TEST_ASSERT_EQUAL(result, 0x8f); + static void verify1(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0)); } - static void verify3(Callback func) { - T result = func(0x01, 0x02, 0x04); - TEST_ASSERT_EQUAL(result, 0x07); + static void verify2(Callback func) { + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1)); } template - static void verify3(O *obj, M method) { - Callback func(obj, method); - T result = func(0x01, 0x02, 0x04); - TEST_ASSERT_EQUAL(result, 0x87); + static void verify2(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1)); } - static void verify2(Callback func) { - T result = func(0x01, 0x02); - TEST_ASSERT_EQUAL(result, 0x03); + static void verify3(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2)); } template - static void verify2(O *obj, M method) { - Callback func(obj, method); - T result = func(0x01, 0x02); - TEST_ASSERT_EQUAL(result, 0x83); + static void verify3(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2)); } - static void verify1(Callback func) { - T result = func(0x01); - TEST_ASSERT_EQUAL(result, 0x01); + static void verify4(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); } template - static void verify1(O *obj, M method) { - Callback func(obj, method); - T result = func(0x01); - TEST_ASSERT_EQUAL(result, 0x81); + static void verify4(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); } - static void verify0(Callback func) { - T result = func(); - TEST_ASSERT_EQUAL(result, 0x00); + static void verify5(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); } template - static void verify0(O *obj, M method) { - Callback func(obj, method); - T result = func(); - TEST_ASSERT_EQUAL(result, 0x80); + static void verify5(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); } }; // test dispatch template -void test_dispatch5() { - Thing thing; - 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); - callback.attach(&thing, &bound_func5); - Verifier::verify5(&callback, &Callback::call); - Verifier::verify5((void*)&callback, &Callback::thunk); -} - -template -void test_dispatch4() { +void test_dispatch0() { Thing thing; - 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); - callback.attach(&thing, &bound_func4); - Verifier::verify4(&callback, &Callback::call); - Verifier::verify4((void*)&callback, &Callback::thunk); + Verifier::verify0(static_func0); + Verifier::verify0(&thing, &Thing::member_func0); + Verifier::verify0((const Thing*)&thing, &Thing::const_member_func0); + Verifier::verify0((volatile Thing*)&thing, &Thing::volatile_member_func0); + Verifier::verify0((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify0(callback(static_func0)); + + Callback cb(static_func0); + Verifier::verify0(cb); + cb = static_func0; + Verifier::verify0(cb); + cb.attach(&thing, &bound_func0); + Verifier::verify0(&cb, &Callback::call); + Verifier::verify0((void*)&cb, &Callback::thunk); } template -void test_dispatch3() { +void test_dispatch1() { Thing thing; - 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); - callback.attach(&thing, &bound_func3); - Verifier::verify3(&callback, &Callback::call); - Verifier::verify3((void*)&callback, &Callback::thunk); + Verifier::verify1(static_func1); + Verifier::verify1(&thing, &Thing::member_func1); + Verifier::verify1((const Thing*)&thing, &Thing::const_member_func1); + Verifier::verify1((volatile Thing*)&thing, &Thing::volatile_member_func1); + Verifier::verify1((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify1(callback(static_func1)); + + Callback cb(static_func1); + Verifier::verify1(cb); + cb = static_func1; + Verifier::verify1(cb); + cb.attach(&thing, &bound_func1); + Verifier::verify1(&cb, &Callback::call); + Verifier::verify1((void*)&cb, &Callback::thunk); } template @@ -225,66 +241,91 @@ void test_dispatch2() { Thing thing; Verifier::verify2(static_func2); Verifier::verify2(&thing, &Thing::member_func2); + Verifier::verify2((const Thing*)&thing, &Thing::const_member_func2); + Verifier::verify2((volatile Thing*)&thing, &Thing::volatile_member_func2); + Verifier::verify2((const volatile Thing*)&thing, &Thing::const_volatile_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); - callback.attach(&thing, &bound_func2); - Verifier::verify2(&callback, &Callback::call); - Verifier::verify2((void*)&callback, &Callback::thunk); + Verifier::verify2(callback(static_func2)); + + Callback cb(static_func2); + Verifier::verify2(cb); + cb = static_func2; + Verifier::verify2(cb); + cb.attach(&thing, &bound_func2); + Verifier::verify2(&cb, &Callback::call); + Verifier::verify2((void*)&cb, &Callback::thunk); } template -void test_dispatch1() { - Thing thing; - 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); - callback.attach(&thing, &bound_func1); - Verifier::verify1(&callback, &Callback::call); - Verifier::verify1((void*)&callback, &Callback::thunk); -} - -template -void test_dispatch0() { +void test_dispatch3() { Thing thing; - 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); - callback.attach(&thing, &bound_func0); - Verifier::verify0(&callback, &Callback::call); - Verifier::verify0((void*)&callback, &Callback::thunk); + Verifier::verify3(static_func3); + Verifier::verify3(&thing, &Thing::member_func3); + Verifier::verify3((const Thing*)&thing, &Thing::const_member_func3); + Verifier::verify3((volatile Thing*)&thing, &Thing::volatile_member_func3); + Verifier::verify3((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify3(callback(static_func3)); + + Callback cb(static_func3); + Verifier::verify3(cb); + cb = static_func3; + Verifier::verify3(cb); + cb.attach(&thing, &bound_func3); + Verifier::verify3(&cb, &Callback::call); + Verifier::verify3((void*)&cb, &Callback::thunk); } template -void test_fparg1() { +void test_dispatch4() { Thing thing; - FunctionPointerArg1 fp(static_func1); - Verifier::verify1(fp); - Verifier::verify1(fp.get_function()); + Verifier::verify4(static_func4); + Verifier::verify4(&thing, &Thing::member_func4); + Verifier::verify4((const Thing*)&thing, &Thing::const_member_func4); + Verifier::verify4((volatile Thing*)&thing, &Thing::volatile_member_func4); + Verifier::verify4((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify4(callback(static_func4)); + + Callback cb(static_func4); + Verifier::verify4(cb); + cb = static_func4; + Verifier::verify4(cb); + cb.attach(&thing, &bound_func4); + Verifier::verify4(&cb, &Callback::call); + Verifier::verify4((void*)&cb, &Callback::thunk); } template -void test_fparg0() { +void test_dispatch5() { Thing thing; - FunctionPointerArg1 fp(static_func0); - Verifier::verify0(fp); - Verifier::verify0(fp.get_function()); + Verifier::verify5(static_func5); + Verifier::verify5(&thing, &Thing::member_func5); + Verifier::verify5((const Thing*)&thing, &Thing::const_member_func5); + Verifier::verify5((volatile Thing*)&thing, &Thing::volatile_member_func5); + Verifier::verify5((const volatile Thing*)&thing, &Thing::const_volatile_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); + Verifier::verify5(callback(static_func5)); + + Callback cb(static_func5); + Verifier::verify5(cb); + cb = static_func5; + Verifier::verify5(cb); + cb.attach(&thing, &bound_func5); + Verifier::verify5(&cb, &Callback::call); + Verifier::verify5((void*)&cb, &Callback::thunk); } @@ -295,29 +336,12 @@ utest::v1::status_t test_setup(const size_t number_of_cases) { } Case cases[] = { - Case("Testing callbacks with 5 ints", test_dispatch5), - Case("Testing callbacks with 4 ints", test_dispatch4), - Case("Testing callbacks with 3 ints", test_dispatch3), - Case("Testing callbacks with 2 ints", test_dispatch2), - Case("Testing callbacks with 1 ints", test_dispatch1), - Case("Testing callbacks with 0 ints", test_dispatch0), - - Case("Testing callbacks with 5 uchars", test_dispatch5), - Case("Testing callbacks with 4 uchars", test_dispatch4), - Case("Testing callbacks with 3 uchars", test_dispatch3), - Case("Testing callbacks with 2 uchars", test_dispatch2), - Case("Testing callbacks with 1 uchars", test_dispatch1), Case("Testing callbacks with 0 uchars", test_dispatch0), - - Case("Testing callbacks with 5 uint64s", test_dispatch5), - Case("Testing callbacks with 4 uint64s", test_dispatch4), - Case("Testing callbacks with 3 uint64s", test_dispatch3), - 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), + Case("Testing callbacks with 1 uchars", test_dispatch1), + Case("Testing callbacks with 2 uchars", test_dispatch2), + Case("Testing callbacks with 3 uchars", test_dispatch3), + Case("Testing callbacks with 4 uchars", test_dispatch4), + Case("Testing callbacks with 5 uchars", test_dispatch5), }; Specification specification(test_setup, cases); diff --git a/TESTS/mbed_functional/functionpointer/main.cpp b/TESTS/mbed_functional/functionpointer/main.cpp new file mode 100644 index 00000000000..7a61dc0abdf --- /dev/null +++ b/TESTS/mbed_functional/functionpointer/main.cpp @@ -0,0 +1,225 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +using namespace utest::v1; + + +// static functions +template +T static_func0() { return 0; } +template +T static_func1(T a0) { return 0 | a0; } +template +T static_func2(T a0, T a1) { return 0 | a0 | a1; } +template +T static_func3(T a0, T a1, T a2) { return 0 | a0 | a1 | a2; } +template +T static_func4(T a0, T a1, T a2, T a3) { return 0 | a0 | a1 | a2 | a3; } +template +T static_func5(T a0, T a1, T a2, T a3, T a4) { return 0 | a0 | a1 | a2 | a3 | a4; } + +// class functions +template +struct Thing { + T t; + Thing() : t(0x80) {} + + T member_func0() { return t; } + T member_func1(T a0) { return t | a0; } + T member_func2(T a0, T a1) { return t | a0 | a1; } + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } + + T const_member_func0() const { return t; } + T const_member_func1(T a0) const { return t | a0; } + T const_member_func2(T a0, T a1) const { return t | a0 | a1; } + T const_member_func3(T a0, T a1, T a2) const { return t | a0 | a1 | a2; } + T const_member_func4(T a0, T a1, T a2, T a3) const { return t | a0 | a1 | a2 | a3; } + T const_member_func5(T a0, T a1, T a2, T a3, T a4) const { return t | a0 | a1 | a2 | a3 | a4; } + + T volatile_member_func0() volatile { return t; } + T volatile_member_func1(T a0) volatile { return t | a0; } + T volatile_member_func2(T a0, T a1) volatile { return t | a0 | a1; } + T volatile_member_func3(T a0, T a1, T a2) volatile { return t | a0 | a1 | a2; } + T volatile_member_func4(T a0, T a1, T a2, T a3) volatile { return t | a0 | a1 | a2 | a3; } + T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile { return t | a0 | a1 | a2 | a3 | a4; } + + T const_volatile_member_func0() const volatile { return t; } + T const_volatile_member_func1(T a0) const volatile { return t | a0; } + T const_volatile_member_func2(T a0, T a1) const volatile { return t | a0 | a1; } + T const_volatile_member_func3(T a0, T a1, T a2) const volatile { return t | a0 | a1 | a2; } + T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile { return t | a0 | a1 | a2 | a3; } + T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile { return t | a0 | a1 | a2 | a3 | a4; } +}; + +// bound functions +template +T bound_func0(Thing *t) { return t->t; } +template +T bound_func1(Thing *t, T a0) { return t->t | a0; } +template +T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const bound functions +template +T const_func0(const Thing *t) { return t->t; } +template +T const_func1(const Thing *t, T a0) { return t->t | a0; } +template +T const_func2(const Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +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_func5(const Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// volatile bound functions +template +T volatile_func0(volatile Thing *t) { return t->t; } +template +T volatile_func1(volatile Thing *t, T a0) { return t->t | a0; } +template +T volatile_func2(volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +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_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const volatile bound functions +template +T const_volatile_func0(const volatile Thing *t) { return t->t; } +template +T const_volatile_func1(const volatile Thing *t, T a0) { return t->t | a0; } +template +T const_volatile_func2(const volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +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_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +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; } + + +// function call and result verification +template +struct Verifier { + static void verify0(Callback func) { + T result = func(); + TEST_ASSERT_EQUAL(result, 0x00); + } + + template + static void verify0(O *obj, M method) { + Callback func(obj, method); + T result = func(); + TEST_ASSERT_EQUAL(result, 0x80); + } + + static void verify1(Callback func) { + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0)); + } + + template + static void verify1(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0)); + } + + static void verify2(Callback func) { + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1)); + } + + template + static void verify2(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1)); + } + + static void verify3(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + template + static void verify3(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + static void verify4(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + template + static void verify4(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + static void verify5(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } + + template + static void verify5(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } +}; + + +// test dispatch +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 +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(10, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing FunctionPointerArg1 compatibility", test_fparg1), + Case("Testing FunctionPointer compatibility", test_fparg0), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +} diff --git a/features/net/network-socket/Socket.h b/features/net/network-socket/Socket.h index c481930440f..39a89a529f4 100644 --- a/features/net/network-socket/Socket.h +++ b/features/net/network-socket/Socket.h @@ -21,6 +21,7 @@ #include "network-socket/NetworkStack.h" #include "rtos/Mutex.h" #include "Callback.h" +#include "toolchain.h" /** Abstract socket class @@ -168,10 +169,17 @@ class Socket { * * @param obj Pointer to object to call method on * @param method Method to call on state change + * + * @deprecated + * The attach function does not support cv-qualifiers. Replaced by + * attach(callback(obj, method)). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method)).") void attach(T *obj, M method) { - attach(mbed::Callback(obj, method)); + attach(mbed::callback(obj, method)); } protected: diff --git a/hal/api/CallChain.h b/hal/api/CallChain.h index 473ff12a144..7b0b20edc56 100644 --- a/hal/api/CallChain.h +++ b/hal/api/CallChain.h @@ -17,6 +17,7 @@ #define MBED_CALLCHAIN_H #include "Callback.h" +#include "toolchain.h" #include namespace mbed { @@ -87,10 +88,17 @@ class CallChain { * * @returns * The function object created for 'obj' and 'method' + * + * @deprecated + * The add function does not support cv-qualifiers. Replaced by + * add(callback(obj, method)). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The add function does not support cv-qualifiers. Replaced by " + "add(callback(obj, method)).") pFunctionPointer_t add(T *obj, M method) { - return add(Callback(obj, method)); + return add(callback(obj, method)); } /** Add a function at the beginning of the chain @@ -109,10 +117,17 @@ class CallChain { * * @returns * The function object created for 'tptr' and 'mptr' + * + * @deprecated + * The add_front function does not support cv-qualifiers. Replaced by + * add_front(callback(obj, method)). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The add_front function does not support cv-qualifiers. Replaced by " + "add_front(callback(obj, method)).") pFunctionPointer_t add_front(T *obj, M method) { - return add_front(Callback(obj, method)); + return add_front(callback(obj, method)); } /** Get the number of functions in the chain diff --git a/hal/api/Callback.h b/hal/api/Callback.h index 8ffd70164c4..fa784386551 100644 --- a/hal/api/Callback.h +++ b/hal/api/Callback.h @@ -18,6 +18,7 @@ #include #include +#include "mbed_assert.h" namespace mbed { @@ -29,15 +30,17 @@ namespace mbed { template class Callback; -/** Templated function class +/** Callback class based on template specialization + * + * @Note Synchronization level: Not protected */ -template -class Callback { +template +class Callback { public: /** Create a Callback with a static function * @param func Static function to attach */ - Callback(R (*func)(A0, A1, A2, A3, A4) = 0) { + Callback(R (*func)() = 0) { attach(func); } @@ -46,184 +49,214 @@ class Callback { * @param func Static function to attach */ template - Callback(T *obj, R (*func)(T*, A0, A1, A2, A3, A4)) { + Callback(T *obj, R (*func)(T*)) { attach(obj, func); } - /** Create a Callback with a member function - * @param obj Pointer to object to invoke member function on - * @param func Member function to attach + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach */ template - Callback(T *obj, R (T::*func)(A0, A1, A2, A3, A4)) { + Callback(const T *obj, R (*func)(const T*)) { attach(obj, func); } - /** Create a Callback with another Callback - * @param func Callback to attach - */ - Callback(const Callback &func) { - attach(func); - } - - /** Attach a static function + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function * @param func Static function to attach */ - void attach(R (*func)(A0, A1, A2, A3, A4)) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; + template + Callback(volatile T *obj, R (*func)(volatile T*)) { + attach(obj, func); } - /** Attach a static function with a bound pointer + /** Create a Callback with a static function and bound pointer * @param obj Pointer to object to bind to function * @param func Static function to attach */ - template - void attach(T *obj, R (*func)(T*, A0, A1, A2, A3, A4)) { - _obj = (void*)obj; - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; + template + Callback(const volatile T *obj, R (*func)(const volatile T*)) { + attach(obj, func); } - /** Attach a member function + /** Create a Callback with a member function * @param obj Pointer to object to invoke member function on * @param func Member function to attach */ template - void attach(T *obj, R (T::*func)(A0, A1, A2, A3, A4)) { - _obj = static_cast(obj); - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; + Callback(T *obj, R (T::*func)()) { + attach(obj, func); } - /** Attach a Callback - * @param func The Callback to attach + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; + template + Callback(const T *obj, R (T::*func)() const) { + attach(obj, func); } - /** Call the attached function + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - R call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { - if (!_thunk) { - return (R)0; - } - return _thunk(_obj, &_func, a0, a1, a2, a3, a4); + template + Callback(volatile T *obj, R (T::*func)() volatile) { + attach(obj, func); } - /** Call the attached function + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - R operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { - return call(a0, a1, a2, a3, a4); + template + Callback(const volatile T *obj, R (T::*func)() const volatile) { + attach(obj, func); } - /** Test if function has been attached + /** Attach a static function + * @param func Static function to attach */ - operator bool() const { - return _thunk; + void attach(R (*func)()) { + struct local { + static R _thunk(void*, const void *func) { + return (*static_cast(func))( + ); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = 0; + _thunk = func ? &local::_thunk : 0; } - /** Static thunk for passing as C-style function - * @param func Callback to call passed as void pointer + /** Attach a Callback + * @param func The Callback to attach */ - static R thunk(void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { - return static_cast*>(func) - ->call(a0, a1, a2, a3, a4); - } - -private: - // Internal thunks for various function types - static R _staticthunk(void*, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { - return (*reinterpret_cast(func)) - (a0, a1, a2, a3, a4); + void attach(const Callback &func) { + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func._func, sizeof func); + _obj = func._obj; + _thunk = func._thunk; } - template - static R _boundthunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { - return (*reinterpret_cast(func)) - (static_cast(obj), a0, a1, a2, a3, a4); + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(T *obj, R (*func)(T*)) { + struct local { + static R _thunk(void *obj, const void *func) { + return (*static_cast(func))( + (T*)obj); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } - template - static R _methodthunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (a0, a1, a2, a3, a4); + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const T *obj, R (*func)(const T*)) { + struct local { + static R _thunk(void *obj, const void *func) { + return (*static_cast(func))( + (const T*)obj); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } - // Stored as pointer to function and pointer to optional object - // Function pointer is stored as union of possible function types - // to garuntee proper size and alignment - struct _class; - union { - void (*_staticfunc)(); - void (*_boundfunc)(_class *); - void (_class::*_methodfunc)(); - } _func; - - void *_obj; - - // Thunk registered on attach to dispatch calls - R (*_thunk)(void*, void*, A0, A1, A2, A3, A4); -}; - -/** Templated function class - */ -template -class Callback { -public: - /** Create a Callback with a static function + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function * @param func Static function to attach */ - Callback(R (*func)(A0, A1, A2, A3) = 0) { - attach(func); + template + void attach(volatile T *obj, R (*func)(volatile T*)) { + struct local { + static R _thunk(void *obj, const void *func) { + return (*static_cast(func))( + (volatile T*)obj); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Create a Callback with a static function and bound pointer + /** Attach a static function with a bound pointer * @param obj Pointer to object to bind to function * @param func Static function to attach */ - template - Callback(T *obj, R (*func)(T*, A0, A1, A2, A3)) { - attach(obj, func); + template + void attach(const volatile T *obj, R (*func)(const volatile T*)) { + struct local { + static R _thunk(void *obj, const void *func) { + return (*static_cast(func))( + (const volatile T*)obj); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Create a Callback with a member function + /** Attach a member function * @param obj Pointer to object to invoke member function on * @param func Member function to attach */ template - Callback(T *obj, R (T::*func)(A0, A1, A2, A3)) { - attach(obj, func); - } - - /** Create a Callback with another Callback - * @param func Callback to attach - */ - Callback(const Callback &func) { - attach(func); - } - - /** Attach a static function - * @param func Static function to attach - */ - void attach(R (*func)(A0, A1, A2, A3)) { + void attach(T *obj, R (T::*func)()) { + struct local { + static R _thunk(void *obj, const void *func) { + return (((T*)obj)->* + (*static_cast(func)))( + ); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Attach a static function with a bound pointer - * @param obj Pointer to object to bind to function - * @param func Static function to attach + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - template - void attach(T *obj, R (*func)(T*, A0, A1, A2, A3)) { - _obj = (void*)obj; + template + void attach(const T *obj, R (T::*func)() const) { + struct local { + static R _thunk(void *obj, const void *func) { + return (((const T*)obj)->* + (*static_cast(func)))( + ); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; + _obj = (void*)obj; + _thunk = &local::_thunk; } /** Attach a member function @@ -231,34 +264,52 @@ class Callback { * @param func Member function to attach */ template - void attach(T *obj, R (T::*func)(A0, A1, A2, A3)) { - _obj = static_cast(obj); + void attach(volatile T *obj, R (T::*func)() volatile) { + struct local { + static R _thunk(void *obj, const void *func) { + return (((volatile T*)obj)->* + (*static_cast(func)))( + ); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Attach a Callback - * @param func The Callback to attach + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; + template + void attach(const volatile T *obj, R (T::*func)() const volatile) { + struct local { + static R _thunk(void *obj, const void *func) { + return (((const volatile T*)obj)->* + (*static_cast(func)))( + ); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } /** Call the attached function */ - R call(A0 a0, A1 a1, A2 a2, A3 a3) { - if (!_thunk) { - return (R)0; - } - return _thunk(_obj, &_func, a0, a1, a2, a3); + R call() const { + MBED_ASSERT(_thunk); + return _thunk(_obj, &_func); } /** Call the attached function */ - R operator()(A0 a0, A1 a1, A2 a2, A3 a3) { - return call(a0, a1, a2, a3); + R operator()() const { + return call(); } /** Test if function has been attached @@ -267,34 +318,27 @@ class Callback { return _thunk; } - /** Static thunk for passing as C-style function - * @param func Callback to call passed as void pointer + /** Test for equality */ - static R thunk(void *func, A0 a0, A1 a1, A2 a2, A3 a3) { - return static_cast*>(func) - ->call(a0, a1, a2, a3); + friend bool operator==(const Callback &l, const Callback &r) { + return memcmp(&l, &r, sizeof(Callback)) == 0; } -private: - // Internal thunks for various function types - static R _staticthunk(void*, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { - return (*reinterpret_cast(func)) - (a0, a1, a2, a3); - } - - template - static R _boundthunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { - return (*reinterpret_cast(func)) - (static_cast(obj), a0, a1, a2, a3); + /** Test for inequality + */ + friend bool operator!=(const Callback &l, const Callback &r) { + return !(l == r); } - template - static R _methodthunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (a0, a1, a2, a3); + /** Static thunk for passing as C-style function + * @param func Callback to call passed as void pointer + */ + static R thunk(void *func) { + return static_cast*>(func)->call( + ); } +private: // Stored as pointer to function and pointer to optional object // Function pointer is stored as union of possible function types // to garuntee proper size and alignment @@ -308,18 +352,20 @@ class Callback { void *_obj; // Thunk registered on attach to dispatch calls - R (*_thunk)(void*, void*, A0, A1, A2, A3); + R (*_thunk)(void*, const void*); }; -/** Templated function class +/** Callback class based on template specialization + * + * @Note Synchronization level: Not protected */ -template -class Callback { +template +class Callback { public: /** Create a Callback with a static function * @param func Static function to attach */ - Callback(R (*func)(A0, A1, A2) = 0) { + Callback(R (*func)(A0) = 0) { attach(func); } @@ -328,114 +374,296 @@ class Callback { * @param func Static function to attach */ template - Callback(T *obj, R (*func)(T*, A0, A1, A2)) { + Callback(T *obj, R (*func)(T*, A0)) { attach(obj, func); } - /** Create a Callback with a member function - * @param obj Pointer to object to invoke member function on - * @param func Member function to attach + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach */ template - Callback(T *obj, R (T::*func)(A0, A1, A2)) { + Callback(const T *obj, R (*func)(const T*, A0)) { attach(obj, func); } - /** Create a Callback with another Callback - * @param func Callback to attach - */ - Callback(const Callback &func) { - attach(func); - } - - /** Attach a static function + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function * @param func Static function to attach */ - void attach(R (*func)(A0, A1, A2)) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; + template + Callback(volatile T *obj, R (*func)(volatile T*, A0)) { + attach(obj, func); } - /** Attach a static function with a bound pointer + /** Create a Callback with a static function and bound pointer * @param obj Pointer to object to bind to function * @param func Static function to attach */ - template - void attach(T *obj, R (*func)(T*, A0, A1, A2)) { - _obj = (void*)obj; - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; + template + Callback(const volatile T *obj, R (*func)(const volatile T*, A0)) { + attach(obj, func); } - /** Attach a member function + /** Create a Callback with a member function * @param obj Pointer to object to invoke member function on * @param func Member function to attach */ template - void attach(T *obj, R (T::*func)(A0, A1, A2)) { - _obj = static_cast(obj); - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; + Callback(T *obj, R (T::*func)(A0)) { + attach(obj, func); } - /** Attach a Callback - * @param func The Callback to attach + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; + template + Callback(const T *obj, R (T::*func)(A0) const) { + attach(obj, func); } - /** Call the attached function + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - R call(A0 a0, A1 a1, A2 a2) { - if (!_thunk) { - return (R)0; - } - return _thunk(_obj, &_func, a0, a1, a2); + template + Callback(volatile T *obj, R (T::*func)(A0) volatile) { + attach(obj, func); } - /** Call the attached function + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - R operator()(A0 a0, A1 a1, A2 a2) { - return call(a0, a1, a2); + template + Callback(const volatile T *obj, R (T::*func)(A0) const volatile) { + attach(obj, func); } - /** Test if function has been attached + /** Attach a static function + * @param func Static function to attach */ - operator bool() const { - return _thunk; + void attach(R (*func)(A0)) { + struct local { + static R _thunk(void*, const void *func, A0 a0) { + return (*static_cast(func))( + a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = 0; + _thunk = func ? &local::_thunk : 0; } - /** Static thunk for passing as C-style function - * @param func Callback to call passed as void pointer + /** Attach a Callback + * @param func The Callback to attach */ - static R thunk(void *func, A0 a0, A1 a1, A2 a2) { - return static_cast*>(func) - ->call(a0, a1, a2); + void attach(const Callback &func) { + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func._func, sizeof func); + _obj = func._obj; + _thunk = func._thunk; } -private: - // Internal thunks for various function types - static R _staticthunk(void*, void *func, A0 a0, A1 a1, A2 a2) { - return (*reinterpret_cast(func)) - (a0, a1, a2); + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(T *obj, R (*func)(T*, A0)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0) { + return (*static_cast(func))( + (T*)obj, a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const T *obj, R (*func)(const T*, A0)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0) { + return (*static_cast(func))( + (const T*)obj, a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(volatile T *obj, R (*func)(volatile T*, A0)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0) { + return (*static_cast(func))( + (volatile T*)obj, a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0) { + return (*static_cast(func))( + (const volatile T*)obj, a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(T *obj, R (T::*func)(A0)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0) { + return (((T*)obj)->* + (*static_cast(func)))( + a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(const T *obj, R (T::*func)(A0) const) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0) { + return (((const T*)obj)->* + (*static_cast(func)))( + a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ template - static R _boundthunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { - return (*reinterpret_cast(func)) - (static_cast(obj), a0, a1, a2); + void attach(volatile T *obj, R (T::*func)(A0) volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0) { + return (((volatile T*)obj)->* + (*static_cast(func)))( + a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ template - static R _methodthunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (a0, a1, a2); + void attach(const volatile T *obj, R (T::*func)(A0) const volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0) { + return (((const volatile T*)obj)->* + (*static_cast(func)))( + a0); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Call the attached function + */ + R call(A0 a0) const { + MBED_ASSERT(_thunk); + return _thunk(_obj, &_func, a0); + } + + /** Call the attached function + */ + R operator()(A0 a0) const { + return call(a0); + } + + /** Test if function has been attached + */ + operator bool() const { + return _thunk; + } + + /** Test for equality + */ + friend bool operator==(const Callback &l, const Callback &r) { + return memcmp(&l, &r, sizeof(Callback)) == 0; + } + + /** Test for inequality + */ + friend bool operator!=(const Callback &l, const Callback &r) { + return !(l == r); + } + + /** Static thunk for passing as C-style function + * @param func Callback to call passed as void pointer + */ + static R thunk(void *func, A0 a0) { + return static_cast*>(func)->call( + a0); } +private: // Stored as pointer to function and pointer to optional object // Function pointer is stored as union of possible function types // to garuntee proper size and alignment @@ -449,10 +677,12 @@ class Callback { void *_obj; // Thunk registered on attach to dispatch calls - R (*_thunk)(void*, void*, A0, A1, A2); + R (*_thunk)(void*, const void*, A0); }; -/** Templated function class +/** Callback class based on template specialization + * + * @Note Synchronization level: Not protected */ template class Callback { @@ -473,6 +703,33 @@ class Callback { attach(obj, func); } + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(const T *obj, R (*func)(const T*, A0, A1)) { + attach(obj, func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(volatile T *obj, R (*func)(volatile T*, A0, A1)) { + attach(obj, func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1)) { + attach(obj, func); + } + /** Create a Callback with a member function * @param obj Pointer to object to invoke member function on * @param func Member function to attach @@ -482,19 +739,58 @@ class Callback { attach(obj, func); } - /** Create a Callback with another Callback - * @param func Callback to attach + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - Callback(const Callback &func) { - attach(func); + template + Callback(const T *obj, R (T::*func)(A0, A1) const) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(volatile T *obj, R (T::*func)(A0, A1) volatile) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(const volatile T *obj, R (T::*func)(A0, A1) const volatile) { + attach(obj, func); } /** Attach a static function * @param func Static function to attach */ void attach(R (*func)(A0, A1)) { + struct local { + static R _thunk(void*, const void *func, A0 a0, A1 a1) { + return (*static_cast(func))( + a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; + _obj = 0; + _thunk = func ? &local::_thunk : 0; + } + + /** Attach a Callback + * @param func The Callback to attach + */ + void attach(const Callback &func) { + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func._func, sizeof func); + _obj = func._obj; + _thunk = func._thunk; } /** Attach a static function with a bound pointer @@ -503,9 +799,74 @@ class Callback { */ template void attach(T *obj, R (*func)(T*, A0, A1)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1) { + return (*static_cast(func))( + (T*)obj, a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const T *obj, R (*func)(const T*, A0, A1)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1) { + return (*static_cast(func))( + (const T*)obj, a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(volatile T *obj, R (*func)(volatile T*, A0, A1)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1) { + return (*static_cast(func))( + (volatile T*)obj, a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0, A1)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1) { + return (*static_cast(func))( + (const volatile T*)obj, a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; + _obj = (void*)obj; + _thunk = &local::_thunk; } /** Attach a member function @@ -514,32 +875,90 @@ class Callback { */ template void attach(T *obj, R (T::*func)(A0, A1)) { - _obj = static_cast(obj); + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1) { + return (((T*)obj)->* + (*static_cast(func)))( + a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Attach a Callback - * @param func The Callback to attach + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; + template + void attach(const T *obj, R (T::*func)(A0, A1) const) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1) { + return (((const T*)obj)->* + (*static_cast(func)))( + a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(volatile T *obj, R (T::*func)(A0, A1) volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1) { + return (((volatile T*)obj)->* + (*static_cast(func)))( + a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(const volatile T *obj, R (T::*func)(A0, A1) const volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1) { + return (((const volatile T*)obj)->* + (*static_cast(func)))( + a0, a1); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } /** Call the attached function */ - R call(A0 a0, A1 a1) { - if (!_thunk) { - return (R)0; - } + R call(A0 a0, A1 a1) const { + MBED_ASSERT(_thunk); return _thunk(_obj, &_func, a0, a1); } /** Call the attached function */ - R operator()(A0 a0, A1 a1) { + R operator()(A0 a0, A1 a1) const { return call(a0, a1); } @@ -549,34 +968,27 @@ class Callback { return _thunk; } - /** Static thunk for passing as C-style function - * @param func Callback to call passed as void pointer + /** Test for equality */ - static R thunk(void *func, A0 a0, A1 a1) { - return static_cast*>(func) - ->call(a0, a1); - } - -private: - // Internal thunks for various function types - static R _staticthunk(void*, void *func, A0 a0, A1 a1) { - return (*reinterpret_cast(func)) - (a0, a1); + friend bool operator==(const Callback &l, const Callback &r) { + return memcmp(&l, &r, sizeof(Callback)) == 0; } - template - static R _boundthunk(void *obj, void *func, A0 a0, A1 a1) { - return (*reinterpret_cast(func)) - (static_cast(obj), a0, a1); + /** Test for inequality + */ + friend bool operator!=(const Callback &l, const Callback &r) { + return !(l == r); } - template - static R _methodthunk(void *obj, void *func, A0 a0, A1 a1) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (a0, a1); + /** Static thunk for passing as C-style function + * @param func Callback to call passed as void pointer + */ + static R thunk(void *func, A0 a0, A1 a1) { + return static_cast*>(func)->call( + a0, a1); } +private: // Stored as pointer to function and pointer to optional object // Function pointer is stored as union of possible function types // to garuntee proper size and alignment @@ -590,18 +1002,20 @@ class Callback { void *_obj; // Thunk registered on attach to dispatch calls - R (*_thunk)(void*, void*, A0, A1); + R (*_thunk)(void*, const void*, A0, A1); }; -/** Templated function class +/** Callback class based on template specialization + * + * @Note Synchronization level: Not protected */ -template -class Callback { +template +class Callback { public: /** Create a Callback with a static function * @param func Static function to attach */ - Callback(R (*func)(A0) = 0) { + Callback(R (*func)(A0, A1, A2) = 0) { attach(func); } @@ -610,7 +1024,34 @@ class Callback { * @param func Static function to attach */ template - Callback(T *obj, R (*func)(T*, A0)) { + Callback(T *obj, R (*func)(T*, A0, A1, A2)) { + attach(obj, func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(const T *obj, R (*func)(const T*, A0, A1, A2)) { + attach(obj, func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2)) { + attach(obj, func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2)) { attach(obj, func); } @@ -619,23 +1060,62 @@ class Callback { * @param func Member function to attach */ template - Callback(T *obj, R (T::*func)(A0)) { + Callback(T *obj, R (T::*func)(A0, A1, A2)) { attach(obj, func); } - /** Create a Callback with another Callback - * @param func Callback to attach + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - Callback(const Callback &func) { - attach(func); + template + Callback(const T *obj, R (T::*func)(A0, A1, A2) const) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(volatile T *obj, R (T::*func)(A0, A1, A2) volatile) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(const volatile T *obj, R (T::*func)(A0, A1, A2) const volatile) { + attach(obj, func); } /** Attach a static function * @param func Static function to attach */ - void attach(R (*func)(A0)) { + void attach(R (*func)(A0, A1, A2)) { + struct local { + static R _thunk(void*, const void *func, A0 a0, A1 a1, A2 a2) { + return (*static_cast(func))( + a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; + _obj = 0; + _thunk = func ? &local::_thunk : 0; + } + + /** Attach a Callback + * @param func The Callback to attach + */ + void attach(const Callback &func) { + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func._func, sizeof func); + _obj = func._obj; + _thunk = func._thunk; } /** Attach a static function with a bound pointer @@ -643,10 +1123,75 @@ class Callback { * @param func Static function to attach */ template - void attach(T *obj, R (*func)(T*, A0)) { + void attach(T *obj, R (*func)(T*, A0, A1, A2)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2) { + return (*static_cast(func))( + (T*)obj, a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const T *obj, R (*func)(const T*, A0, A1, A2)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2) { + return (*static_cast(func))( + (const T*)obj, a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(volatile T *obj, R (*func)(volatile T*, A0, A1, A2)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2) { + return (*static_cast(func))( + (volatile T*)obj, a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2) { + return (*static_cast(func))( + (const volatile T*)obj, a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; + _obj = (void*)obj; + _thunk = &local::_thunk; } /** Attach a member function @@ -654,34 +1199,92 @@ class Callback { * @param func Member function to attach */ template - void attach(T *obj, R (T::*func)(A0)) { - _obj = static_cast(obj); + void attach(T *obj, R (T::*func)(A0, A1, A2)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2) { + return (((T*)obj)->* + (*static_cast(func)))( + a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Attach a Callback - * @param func The Callback to attach + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; + template + void attach(const T *obj, R (T::*func)(A0, A1, A2) const) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2) { + return (((const T*)obj)->* + (*static_cast(func)))( + a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(volatile T *obj, R (T::*func)(A0, A1, A2) volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2) { + return (((volatile T*)obj)->* + (*static_cast(func)))( + a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(const volatile T *obj, R (T::*func)(A0, A1, A2) const volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2) { + return (((const volatile T*)obj)->* + (*static_cast(func)))( + a0, a1, a2); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } /** Call the attached function */ - R call(A0 a0) { - if (!_thunk) { - return (R)0; - } - return _thunk(_obj, &_func, a0); + R call(A0 a0, A1 a1, A2 a2) const { + MBED_ASSERT(_thunk); + return _thunk(_obj, &_func, a0, a1, a2); } /** Call the attached function */ - R operator()(A0 a0) { - return call(a0); + R operator()(A0 a0, A1 a1, A2 a2) const { + return call(a0, a1, a2); } /** Test if function has been attached @@ -690,34 +1293,27 @@ class Callback { return _thunk; } - /** Static thunk for passing as C-style function - * @param func Callback to call passed as void pointer + /** Test for equality */ - static R thunk(void *func, A0 a0) { - return static_cast*>(func) - ->call(a0); - } - -private: - // Internal thunks for various function types - static R _staticthunk(void*, void *func, A0 a0) { - return (*reinterpret_cast(func)) - (a0); + friend bool operator==(const Callback &l, const Callback &r) { + return memcmp(&l, &r, sizeof(Callback)) == 0; } - template - static R _boundthunk(void *obj, void *func, A0 a0) { - return (*reinterpret_cast(func)) - (static_cast(obj), a0); + /** Test for inequality + */ + friend bool operator!=(const Callback &l, const Callback &r) { + return !(l == r); } - template - static R _methodthunk(void *obj, void *func, A0 a0) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (a0); + /** Static thunk for passing as C-style function + * @param func Callback to call passed as void pointer + */ + static R thunk(void *func, A0 a0, A1 a1, A2 a2) { + return static_cast*>(func)->call( + a0, a1, a2); } +private: // Stored as pointer to function and pointer to optional object // Function pointer is stored as union of possible function types // to garuntee proper size and alignment @@ -731,18 +1327,20 @@ class Callback { void *_obj; // Thunk registered on attach to dispatch calls - R (*_thunk)(void*, void*, A0); + R (*_thunk)(void*, const void*, A0, A1, A2); }; -/** Templated function class +/** Callback class based on template specialization + * + * @Note Synchronization level: Not protected */ -template -class Callback { +template +class Callback { public: /** Create a Callback with a static function * @param func Static function to attach */ - Callback(R (*func)() = 0) { + Callback(R (*func)(A0, A1, A2, A3) = 0) { attach(func); } @@ -751,114 +1349,621 @@ class Callback { * @param func Static function to attach */ template - Callback(T *obj, R (*func)(T*)) { + Callback(T *obj, R (*func)(T*, A0, A1, A2, A3)) { attach(obj, func); } - /** Create a Callback with a member function - * @param obj Pointer to object to invoke member function on - * @param func Member function to attach + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach */ template - Callback(T *obj, R (T::*func)()) { + Callback(const T *obj, R (*func)(const T*, A0, A1, A2, A3)) { attach(obj, func); } - /** Create a Callback with another Callback - * @param func Callback to attach + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach */ - Callback(const Callback &func) { - attach(func); + template + Callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3)) { + attach(obj, func); } - /** Attach a static function + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function * @param func Static function to attach */ - void attach(R (*func)()) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; + template + Callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3)) { + attach(obj, func); } - /** Attach a static function with a bound pointer - * @param obj Pointer to object to bind to function - * @param func Static function to attach + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach */ - template - void attach(T *obj, R (*func)(T*)) { - _obj = (void*)obj; - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; + template + Callback(T *obj, R (T::*func)(A0, A1, A2, A3)) { + attach(obj, func); } - /** Attach a member function + /** Create a Callback with a member function * @param obj Pointer to object to invoke member function on * @param func Member function to attach */ template - void attach(T *obj, R (T::*func)()) { - _obj = static_cast(obj); + Callback(const T *obj, R (T::*func)(A0, A1, A2, A3) const) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(volatile T *obj, R (T::*func)(A0, A1, A2, A3) volatile) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(const volatile T *obj, R (T::*func)(A0, A1, A2, A3) const volatile) { + attach(obj, func); + } + + /** Attach a static function + * @param func Static function to attach + */ + void attach(R (*func)(A0, A1, A2, A3)) { + struct local { + static R _thunk(void*, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*static_cast(func))( + a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; + _obj = 0; + _thunk = func ? &local::_thunk : 0; } /** Attach a Callback * @param func The Callback to attach */ - void attach(const Callback &func) { + void attach(const Callback &func) { + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func._func, sizeof func); _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); _thunk = func._thunk; } - /** Call the attached function + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach */ - R call() { - if (!_thunk) { - return (R)0; - } - return _thunk(_obj, &_func); + template + void attach(T *obj, R (*func)(T*, A0, A1, A2, A3)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*static_cast(func))( + (T*)obj, a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Call the attached function + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach */ - R operator()() { - return call(); + template + void attach(const T *obj, R (*func)(const T*, A0, A1, A2, A3)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*static_cast(func))( + (const T*)obj, a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Test if function has been attached + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach */ - operator bool() const { - return _thunk; + template + void attach(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*static_cast(func))( + (volatile T*)obj, a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } - /** Static thunk for passing as C-style function - * @param func Callback to call passed as void pointer + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach */ - static R thunk(void *func) { - return static_cast*>(func) - ->call(); - } - -private: - // Internal thunks for various function types - static R _staticthunk(void*, void *func) { - return (*reinterpret_cast(func)) - (); + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*static_cast(func))( + (const volatile T*)obj, a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ template - static R _boundthunk(void *obj, void *func) { - return (*reinterpret_cast(func)) - (static_cast(obj)); + void attach(T *obj, R (T::*func)(A0, A1, A2, A3)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (((T*)obj)->* + (*static_cast(func)))( + a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; } - template - static R _methodthunk(void *obj, void *func) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (); - } + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(const T *obj, R (T::*func)(A0, A1, A2, A3) const) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (((const T*)obj)->* + (*static_cast(func)))( + a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(volatile T *obj, R (T::*func)(A0, A1, A2, A3) volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (((volatile T*)obj)->* + (*static_cast(func)))( + a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(const volatile T *obj, R (T::*func)(A0, A1, A2, A3) const volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (((const volatile T*)obj)->* + (*static_cast(func)))( + a0, a1, a2, a3); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Call the attached function + */ + R call(A0 a0, A1 a1, A2 a2, A3 a3) const { + MBED_ASSERT(_thunk); + return _thunk(_obj, &_func, a0, a1, a2, a3); + } + + /** Call the attached function + */ + R operator()(A0 a0, A1 a1, A2 a2, A3 a3) const { + return call(a0, a1, a2, a3); + } + + /** Test if function has been attached + */ + operator bool() const { + return _thunk; + } + + /** Test for equality + */ + friend bool operator==(const Callback &l, const Callback &r) { + return memcmp(&l, &r, sizeof(Callback)) == 0; + } + + /** Test for inequality + */ + friend bool operator!=(const Callback &l, const Callback &r) { + return !(l == r); + } + + /** Static thunk for passing as C-style function + * @param func Callback to call passed as void pointer + */ + static R thunk(void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return static_cast*>(func)->call( + a0, a1, a2, a3); + } + +private: + // Stored as pointer to function and pointer to optional object + // Function pointer is stored as union of possible function types + // to garuntee proper size and alignment + struct _class; + union { + void (*_staticfunc)(); + void (*_boundfunc)(_class *); + void (_class::*_methodfunc)(); + } _func; + + void *_obj; + + // Thunk registered on attach to dispatch calls + R (*_thunk)(void*, const void*, A0, A1, A2, A3); +}; + +/** Callback class based on template specialization + * + * @Note Synchronization level: Not protected + */ +template +class Callback { +public: + /** Create a Callback with a static function + * @param func Static function to attach + */ + Callback(R (*func)(A0, A1, A2, A3, A4) = 0) { + attach(func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(T *obj, R (*func)(T*, A0, A1, A2, A3, A4)) { + attach(obj, func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(const T *obj, R (*func)(const T*, A0, A1, A2, A3, A4)) { + attach(obj, func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3, A4)) { + attach(obj, func); + } + + /** Create a Callback with a static function and bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + Callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3, A4)) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(T *obj, R (T::*func)(A0, A1, A2, A3, A4)) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(const T *obj, R (T::*func)(A0, A1, A2, A3, A4) const) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) volatile) { + attach(obj, func); + } + + /** Create a Callback with a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + Callback(const volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) const volatile) { + attach(obj, func); + } + + /** Attach a static function + * @param func Static function to attach + */ + void attach(R (*func)(A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void*, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*static_cast(func))( + a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = 0; + _thunk = func ? &local::_thunk : 0; + } + + /** Attach a Callback + * @param func The Callback to attach + */ + void attach(const Callback &func) { + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func._func, sizeof func); + _obj = func._obj; + _thunk = func._thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(T *obj, R (*func)(T*, A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*static_cast(func))( + (T*)obj, a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const T *obj, R (*func)(const T*, A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*static_cast(func))( + (const T*)obj, a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*static_cast(func))( + (volatile T*)obj, a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a static function with a bound pointer + * @param obj Pointer to object to bind to function + * @param func Static function to attach + */ + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*static_cast(func))( + (const volatile T*)obj, a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(T *obj, R (T::*func)(A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (((T*)obj)->* + (*static_cast(func)))( + a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(const T *obj, R (T::*func)(A0, A1, A2, A3, A4) const) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (((const T*)obj)->* + (*static_cast(func)))( + a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (((volatile T*)obj)->* + (*static_cast(func)))( + a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Attach a member function + * @param obj Pointer to object to invoke member function on + * @param func Member function to attach + */ + template + void attach(const volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) const volatile) { + struct local { + static R _thunk(void *obj, const void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (((const volatile T*)obj)->* + (*static_cast(func)))( + a0, a1, a2, a3, a4); + } + }; + + memset(&_func, 0, sizeof _func); + memcpy(&_func, &func, sizeof func); + _obj = (void*)obj; + _thunk = &local::_thunk; + } + + /** Call the attached function + */ + R call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) const { + MBED_ASSERT(_thunk); + return _thunk(_obj, &_func, a0, a1, a2, a3, a4); + } + + /** Call the attached function + */ + R operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) const { + return call(a0, a1, a2, a3, a4); + } + + /** Test if function has been attached + */ + operator bool() const { + return _thunk; + } + /** Test for equality + */ + friend bool operator==(const Callback &l, const Callback &r) { + return memcmp(&l, &r, sizeof(Callback)) == 0; + } + + /** Test for inequality + */ + friend bool operator!=(const Callback &l, const Callback &r) { + return !(l == r); + } + + /** Static thunk for passing as C-style function + * @param func Callback to call passed as void pointer + */ + static R thunk(void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return static_cast*>(func)->call( + a0, a1, a2, a3, a4); + } + +private: // Stored as pointer to function and pointer to optional object // Function pointer is stored as union of possible function types // to garuntee proper size and alignment @@ -872,10 +1977,665 @@ class Callback { void *_obj; // Thunk registered on attach to dispatch calls - R (*_thunk)(void*, void*); + R (*_thunk)(void*, const void*, A0, A1, A2, A3, A4); }; - typedef Callback event_callback_t; +// Internally used event type +typedef Callback event_callback_t; + + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(R (*func)() = 0) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const Callback &func) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (*func)(T*)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (*func)(const T*)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (*func)(volatile T*)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (T::*func)()) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (T::*func)() const) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (T::*func)() volatile) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (T::*func)() const volatile) { + return Callback(obj, func); +} + + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(R (*func)(A0) = 0) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const Callback &func) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (*func)(T*, A0)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (*func)(const T*, A0)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (T::*func)(A0)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (T::*func)(A0) const) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (T::*func)(A0) volatile) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (T::*func)(A0) const volatile) { + return Callback(obj, func); +} + + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(R (*func)(A0, A1) = 0) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const Callback &func) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (*func)(T*, A0, A1)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (*func)(const T*, A0, A1)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0, A1)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (T::*func)(A0, A1)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (T::*func)(A0, A1) const) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (T::*func)(A0, A1) volatile) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (T::*func)(A0, A1) const volatile) { + return Callback(obj, func); +} + + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(R (*func)(A0, A1, A2) = 0) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const Callback &func) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (*func)(T*, A0, A1, A2)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (*func)(const T*, A0, A1, A2)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (T::*func)(A0, A1, A2)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (T::*func)(A0, A1, A2) const) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (T::*func)(A0, A1, A2) volatile) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (T::*func)(A0, A1, A2) const volatile) { + return Callback(obj, func); +} + + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(R (*func)(A0, A1, A2, A3) = 0) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const Callback &func) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (*func)(T*, A0, A1, A2, A3)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (*func)(const T*, A0, A1, A2, A3)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (T::*func)(A0, A1, A2, A3)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (T::*func)(A0, A1, A2, A3) const) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (T::*func)(A0, A1, A2, A3) volatile) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (T::*func)(A0, A1, A2, A3) const volatile) { + return Callback(obj, func); +} + + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(R (*func)(A0, A1, A2, A3, A4) = 0) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const Callback &func) { + return Callback(func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (*func)(T*, A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (*func)(const T*, A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(T *obj, R (T::*func)(A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const T *obj, R (T::*func)(A0, A1, A2, A3, A4) const) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) volatile) { + return Callback(obj, func); +} + +/** Create a callback class with type infered from the arguments + * + * @param obj Optional pointer to object to bind to function + * @param func Static function to attach + * @return Callback with infered type + */ +template +Callback callback(const volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) const volatile) { + return Callback(obj, func); +} } // namespace mbed diff --git a/hal/api/FunctionPointer.h b/hal/api/FunctionPointer.h index 1626e3e483b..af51a8c9291 100644 --- a/hal/api/FunctionPointer.h +++ b/hal/api/FunctionPointer.h @@ -43,6 +43,18 @@ class FunctionPointerArg1 : public Callback { R (*get_function())(A1) { return *reinterpret_cast(this); } + + R call(A1 a1) const { + if (!Callback::operator bool()) { + return (R)0; + } + + return Callback::call(a1); + } + + R operator()(A1 a1) const { + return Callback::call(a1); + } }; template @@ -62,6 +74,18 @@ class FunctionPointerArg1 : public Callback { R (*get_function())() { return *reinterpret_cast(this); } + + R call() const { + if (!Callback::operator bool()) { + return (R)0; + } + + return Callback::call(); + } + + R operator()() const { + return Callback::call(); + } }; typedef FunctionPointerArg1 FunctionPointer; diff --git a/hal/api/InterruptIn.h b/hal/api/InterruptIn.h index 3758ab5ca34..fcb7df4b81d 100644 --- a/hal/api/InterruptIn.h +++ b/hal/api/InterruptIn.h @@ -24,6 +24,7 @@ #include "gpio_irq_api.h" #include "Callback.h" #include "critical.h" +#include "toolchain.h" namespace mbed { @@ -88,11 +89,17 @@ class InterruptIn { * * @param obj pointer to the object to call the member function on * @param method pointer to the member function to be called + * @deprecated + * The rise function does not support cv-qualifiers. Replaced by + * rise(callback(obj, method)). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The rise function does not support cv-qualifiers. Replaced by " + "rise(callback(obj, method)).") void rise(T *obj, M method) { core_util_critical_section_enter(); - rise(Callback(obj, method)); + rise(callback(obj, method)); core_util_critical_section_exit(); } @@ -106,11 +113,17 @@ class InterruptIn { * * @param obj pointer to the object to call the member function on * @param method pointer to the member function to be called + * @deprecated + * The rise function does not support cv-qualifiers. Replaced by + * rise(callback(obj, method)). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The fall function does not support cv-qualifiers. Replaced by " + "fall(callback(obj, method)).") void fall(T *obj, M method) { core_util_critical_section_enter(); - fall(Callback(obj, method)); + fall(callback(obj, method)); core_util_critical_section_exit(); } diff --git a/hal/api/SerialBase.h b/hal/api/SerialBase.h index 1d7ae7c34dd..be792930953 100644 --- a/hal/api/SerialBase.h +++ b/hal/api/SerialBase.h @@ -23,6 +23,7 @@ #include "Stream.h" #include "Callback.h" #include "serial_api.h" +#include "toolchain.h" #if DEVICE_SERIAL_ASYNCH #include "CThunk.h" @@ -101,10 +102,16 @@ class SerialBase { * @param obj pointer to the object to call the member function on * @param method pointer to the member function to be called * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) + * @deprecated + * The attach function does not support cv-qualifiers. Replaced by + * attach(callback(obj, method), type). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method), type).") void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { - attach(Callback(obj, method), type); + attach(callback(obj, method), type); } /** Attach a member function to call whenever a serial interrupt is generated @@ -112,10 +119,16 @@ class SerialBase { * @param obj pointer to the object to call the member function on * @param method pointer to the member function to be called * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) + * @deprecated + * The attach function does not support cv-qualifiers. Replaced by + * attach(callback(obj, method), type). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method), type).") void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { - attach(Callback(obj, method), type); + attach(callback(obj, method), type); } /** Generate a break condition on the serial line diff --git a/hal/api/Ticker.h b/hal/api/Ticker.h index a7848ece4d6..5b2f97ba950 100644 --- a/hal/api/Ticker.h +++ b/hal/api/Ticker.h @@ -18,6 +18,7 @@ #include "TimerEvent.h" #include "Callback.h" +#include "toolchain.h" namespace mbed { @@ -80,10 +81,16 @@ class Ticker : public TimerEvent { * @param obj pointer to the object to call the member function on * @param method pointer to the member function to be called * @param t the time between calls in seconds + * @deprecated + * The attach function does not support cv-qualifiers. Replaced by + * attach(callback(obj, method), t). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method), t).") void attach(T *obj, M method, float t) { - attach(Callback(obj, method), t); + attach(callback(obj, method), t); } /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds @@ -101,8 +108,14 @@ class Ticker : public TimerEvent { * @param tptr pointer to the object to call the member function on * @param mptr pointer to the member function to be called * @param t the time between calls in micro-seconds + * @deprecated + * The attach_us function does not support cv-qualifiers. Replaced by + * attach_us(callback(obj, method), t). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The attach_us function does not support cv-qualifiers. Replaced by " + "attach_us(callback(obj, method), t).") void attach_us(T *obj, M method, timestamp_t t) { attach_us(Callback(obj, method), t); } diff --git a/hal/common/CAN.cpp b/hal/common/CAN.cpp index 780a106b73d..84355b02302 100644 --- a/hal/common/CAN.cpp +++ b/hal/common/CAN.cpp @@ -21,8 +21,15 @@ namespace mbed { +static void donothing() {} + CAN::CAN(PinName rd, PinName td) : _can(), _irq() { // No lock needed in constructor + + for (int i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { + _irq[i].attach(donothing); + } + can_init(&_can, rd, td); can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); } @@ -100,6 +107,7 @@ void CAN::attach(Callback func, IrqType type) { _irq[(CanIrqType)type].attach(func); can_irq_set(&_can, (CanIrqType)type, 1); } else { + _irq[(CanIrqType)type].attach(donothing); can_irq_set(&_can, (CanIrqType)type, 0); } unlock(); diff --git a/hal/common/InterruptIn.cpp b/hal/common/InterruptIn.cpp index 0e5bde9bc21..d3986e88930 100644 --- a/hal/common/InterruptIn.cpp +++ b/hal/common/InterruptIn.cpp @@ -19,11 +19,17 @@ namespace mbed { +static void donothing() {} + InterruptIn::InterruptIn(PinName pin) : gpio(), gpio_irq(), _rise(), _fall() { // No lock needed in the constructor + + _rise.attach(donothing); + _fall.attach(donothing); + gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); gpio_init_in(&gpio, pin); } @@ -50,7 +56,7 @@ void InterruptIn::rise(Callback func) { _rise.attach(func); gpio_irq_set(&gpio_irq, IRQ_RISE, 1); } else { - _rise.attach(NULL); + _rise.attach(donothing); gpio_irq_set(&gpio_irq, IRQ_RISE, 0); } core_util_critical_section_exit(); @@ -62,7 +68,7 @@ void InterruptIn::fall(Callback func) { _fall.attach(func); gpio_irq_set(&gpio_irq, IRQ_FALL, 1); } else { - _fall.attach(NULL); + _fall.attach(donothing); gpio_irq_set(&gpio_irq, IRQ_FALL, 0); } core_util_critical_section_exit(); diff --git a/hal/common/SerialBase.cpp b/hal/common/SerialBase.cpp index 891c4509751..d75e479418c 100644 --- a/hal/common/SerialBase.cpp +++ b/hal/common/SerialBase.cpp @@ -21,6 +21,8 @@ namespace mbed { +static void donothing() {}; + SerialBase::SerialBase(PinName tx, PinName rx) : #if DEVICE_SERIAL_ASYNCH _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER), @@ -29,6 +31,10 @@ SerialBase::SerialBase(PinName tx, PinName rx) : _serial(), _baud(9600) { // No lock needed in the constructor + for (int i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { + _irq[i].attach(donothing); + } + serial_init(&_serial, tx, rx); serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); } @@ -69,6 +75,7 @@ void SerialBase::attach(Callback func, IrqType type) { _irq[type].attach(func); serial_irq_set(&_serial, (SerialIrq)type, 1); } else { + _irq[type].attach(donothing); serial_irq_set(&_serial, (SerialIrq)type, 0); } core_util_critical_section_exit(); diff --git a/rtos/rtos/RtosTimer.h b/rtos/rtos/RtosTimer.h index ab698ebd36b..3f39a3999af 100644 --- a/rtos/rtos/RtosTimer.h +++ b/rtos/rtos/RtosTimer.h @@ -47,7 +47,7 @@ class RtosTimer { MBED_DEPRECATED_SINCE("mbed-os-5.1", "Replaced with RtosTimer(Callback, os_timer_type)") RtosTimer(void (*func)(void const *argument), os_timer_type type=osTimerPeriodic, void *argument=NULL) { - constructor(mbed::Callback(argument, (void (*)(void *))func), type); + constructor(mbed::callback(argument, (void (*)(void *))func), type); } /** Create timer. @@ -62,10 +62,16 @@ class RtosTimer { @param obj pointer to the object to call the member function on. @param method member function to be executed by this timer. @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic) + @deprecated + The RtosTimer constructor does not support cv-qualifiers. Replaced by + RtosTimer(callback(obj, method), os_timer_type). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The RtosTimer constructor does not support cv-qualifiers. Replaced by " + "RtosTimer(callback(obj, method), os_timer_type).") RtosTimer(T *obj, M method, os_timer_type type=osTimerPeriodic) { - constructor(mbed::Callback(obj, method), type); + constructor(mbed::callback(obj, method), type); } /** Stop the timer. diff --git a/rtos/rtos/Thread.h b/rtos/rtos/Thread.h index eb1c39beab9..9d3356a6078 100644 --- a/rtos/rtos/Thread.h +++ b/rtos/rtos/Thread.h @@ -90,7 +90,7 @@ class Thread { osPriority priority=osPriorityNormal, uint32_t stack_size=DEFAULT_STACK_SIZE, unsigned char *stack_pointer=NULL) { - constructor(mbed::Callback(obj, method), + constructor(mbed::callback(obj, method), priority, stack_size, stack_pointer); } @@ -116,7 +116,7 @@ class Thread { osPriority priority=osPriorityNormal, uint32_t stack_size=DEFAULT_STACK_SIZE, unsigned char *stack_pointer=NULL) { - constructor(mbed::Callback(obj, method), + constructor(mbed::callback(obj, method), priority, stack_size, stack_pointer); } @@ -141,7 +141,7 @@ class Thread { osPriority priority=osPriorityNormal, uint32_t stack_size=DEFAULT_STACK_SIZE, unsigned char *stack_pointer=NULL) { - constructor(mbed::Callback(argument, (void (*)(void *))task), + constructor(mbed::callback(argument, (void (*)(void *))task), priority, stack_size, stack_pointer); } @@ -155,10 +155,16 @@ class Thread { @param obj argument to task @param method function to be executed by this thread. @return status code that indicates the execution status of the function. + @deprecated + The start function does not support cv-qualifiers. Replaced by + start(callback(obj, method)). */ template + MBED_DEPRECATED_SINCE("mbed-os-5.1", + "The start function does not support cv-qualifiers. Replaced by " + "start(callback(obj, method)).") osStatus start(T *obj, M method) { - return start(mbed::Callback(obj, method)); + return start(mbed::callback(obj, method)); } /** Wait for thread to terminate