diff --git a/TESTS/mbed_drivers/callback/main.cpp b/TESTS/mbed_drivers/callback/main.cpp index 4e169a5a6c..d4f0e9af57 100644 --- a/TESTS/mbed_drivers/callback/main.cpp +++ b/TESTS/mbed_drivers/callback/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; } -template -T static_func4(T a0, T a1, T a2, T a3) { return a0 | a1 | a2 | a3; } -template -T static_func3(T a0, T a1, T a2) { return a0 | a1 | a2; } -template -T static_func2(T a0, T a1) { return a0 | a1; } -template -T static_func1(T a0) { return a0; } -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 @@ -26,134 +26,95 @@ 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; } -template -T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } -template -T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } +T bound_func0(Thing *t) { return t->t; } template T bound_func1(Thing *t, T a0) { return t->t | a0; } template -T bound_func0(Thing *t) { return t->t; } +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_func5(const Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } -template -T const_func4(const Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } -template -T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } -template -T const_func2(const Thing *t, T a0, T a1) { return t->t | a0 | a1; } +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_func0(const Thing *t) { return t->t; } +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_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } -template -T volatile_func4(volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } -template -T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } -template -T volatile_func2(volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +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_func0(volatile Thing *t) { return t->t; } +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 volatil bound functions +// const volatile bound functions template -T const_volatile_func5(const volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } -template -T const_volatile_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } -template -T const_volatile_func3(const volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } -template -T const_volatile_func2(const volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +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_func0(const volatile Thing *t) { return t->t; } +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 verify5(Callback func) { - T result = func(0x01, 0x02, 0x04, 0x08, 0x10); - TEST_ASSERT_EQUAL(result, 0x1f); - } - - 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 verify4(Callback func) { - T result = func(0x01, 0x02, 0x04, 0x08); - TEST_ASSERT_EQUAL(result, 0x0f); - } - - 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 verify3(Callback func) { - T result = func(0x01, 0x02, 0x04); - TEST_ASSERT_EQUAL(result, 0x07); - } - - 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(Callback func) { - T result = func(0x01, 0x02); - TEST_ASSERT_EQUAL(result, 0x03); - } - - template - static void verify2(O *obj, M method) { - Callback func(obj, method); - T result = func(0x01, 0x02); - TEST_ASSERT_EQUAL(result, 0x83); - } - - static void verify1(Callback func) { - T result = func(0x01); - TEST_ASSERT_EQUAL(result, 0x01); - } - - template - static void verify1(O *obj, M method) { - Callback func(obj, method); - T result = func(0x01); - TEST_ASSERT_EQUAL(result, 0x81); - } - static void verify0(Callback func) { T result = func(); TEST_ASSERT_EQUAL(result, 0x00); @@ -165,76 +126,97 @@ struct Verifier { 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_dispatch5() { +void test_dispatch0() { 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); + 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)); + Verifier::verify0(callback(&thing, &Thing::member_func0)); + Verifier::verify0(callback((const Thing*)&thing, &Thing::const_member_func0)); + Verifier::verify0(callback((volatile Thing*)&thing, &Thing::volatile_member_func0)); + Verifier::verify0(callback((const volatile Thing*)&thing, &Thing::const_volatile_member_func0)); + Verifier::verify0(callback(&thing, &bound_func0)); + Verifier::verify0(callback((const Thing*)&thing, &const_func0)); + Verifier::verify0(callback((volatile Thing*)&thing, &volatile_func0)); + Verifier::verify0(callback((const volatile Thing*)&thing, &const_volatile_func0)); - 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() { - 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); -} - -template -void test_dispatch3() { - 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); -} - -template -void test_dispatch2() { - Thing thing; - Verifier::verify2(static_func2); - Verifier::verify2(&thing, &Thing::member_func2); - Verifier::verify2(&thing, &bound_func2); - Verifier::verify2((const Thing*)&thing, &const_func2); - Verifier::verify2((volatile Thing*)&thing, &volatile_func2); - Verifier::verify2((const volatile Thing*)&thing, &const_volatile_func2); - - Callback callback(static_func2); - Verifier::verify2(callback); - callback.attach(&thing, &bound_func2); - Verifier::verify2(&callback, &Callback::call); - Verifier::verify2((void*)&callback, &Callback::thunk); + Callback callback(static_func0); + Verifier::verify0(callback); + callback.attach(&thing, &bound_func0); + Verifier::verify0(&callback, &Callback::call); + Verifier::verify0((void*)&callback, &Callback::thunk); } template @@ -242,10 +224,22 @@ 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)); + Verifier::verify1(callback(&thing, &Thing::member_func1)); + Verifier::verify1(callback((const Thing*)&thing, &Thing::const_member_func1)); + Verifier::verify1(callback((volatile Thing*)&thing, &Thing::volatile_member_func1)); + Verifier::verify1(callback((const volatile Thing*)&thing, &Thing::const_volatile_member_func1)); + Verifier::verify1(callback(&thing, &bound_func1)); + Verifier::verify1(callback((const Thing*)&thing, &const_func1)); + Verifier::verify1(callback((volatile Thing*)&thing, &volatile_func1)); + Verifier::verify1(callback((const volatile Thing*)&thing, &const_volatile_func1)); Callback callback(static_func1); Verifier::verify1(callback); @@ -255,20 +249,119 @@ void test_dispatch1() { } template -void test_dispatch0() { +void test_dispatch2() { 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); + 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)); + Verifier::verify2(callback(&thing, &Thing::member_func2)); + Verifier::verify2(callback((const Thing*)&thing, &Thing::const_member_func2)); + Verifier::verify2(callback((volatile Thing*)&thing, &Thing::volatile_member_func2)); + Verifier::verify2(callback((const volatile Thing*)&thing, &Thing::const_volatile_member_func2)); + Verifier::verify2(callback(&thing, &bound_func2)); + Verifier::verify2(callback((const Thing*)&thing, &const_func2)); + Verifier::verify2(callback((volatile Thing*)&thing, &volatile_func2)); + Verifier::verify2(callback((const volatile Thing*)&thing, &const_volatile_func2)); - Callback callback(static_func0); - Verifier::verify0(callback); - callback.attach(&thing, &bound_func0); - Verifier::verify0(&callback, &Callback::call); - Verifier::verify0((void*)&callback, &Callback::thunk); + Callback callback(static_func2); + Verifier::verify2(callback); + callback.attach(&thing, &bound_func2); + Verifier::verify2(&callback, &Callback::call); + Verifier::verify2((void*)&callback, &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)); + Verifier::verify3(callback(&thing, &Thing::member_func3)); + Verifier::verify3(callback((const Thing*)&thing, &Thing::const_member_func3)); + Verifier::verify3(callback((volatile Thing*)&thing, &Thing::volatile_member_func3)); + Verifier::verify3(callback((const volatile Thing*)&thing, &Thing::const_volatile_member_func3)); + Verifier::verify3(callback(&thing, &bound_func3)); + Verifier::verify3(callback((const Thing*)&thing, &const_func3)); + Verifier::verify3(callback((volatile Thing*)&thing, &volatile_func3)); + Verifier::verify3(callback((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); +} + +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)); + Verifier::verify4(callback(&thing, &Thing::member_func4)); + Verifier::verify4(callback((const Thing*)&thing, &Thing::const_member_func4)); + Verifier::verify4(callback((volatile Thing*)&thing, &Thing::volatile_member_func4)); + Verifier::verify4(callback((const volatile Thing*)&thing, &Thing::const_volatile_member_func4)); + Verifier::verify4(callback(&thing, &bound_func4)); + Verifier::verify4(callback((const Thing*)&thing, &const_func4)); + Verifier::verify4(callback((volatile Thing*)&thing, &volatile_func4)); + Verifier::verify4(callback((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); +} + +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)); + Verifier::verify5(callback(&thing, &Thing::member_func5)); + Verifier::verify5(callback((const Thing*)&thing, &Thing::const_member_func5)); + Verifier::verify5(callback((volatile Thing*)&thing, &Thing::volatile_member_func5)); + Verifier::verify5(callback((const volatile Thing*)&thing, &Thing::const_volatile_member_func5)); + Verifier::verify5(callback(&thing, &bound_func5)); + Verifier::verify5(callback((const Thing*)&thing, &const_func5)); + Verifier::verify5(callback((volatile Thing*)&thing, &volatile_func5)); + Verifier::verify5(callback((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 @@ -295,26 +388,26 @@ 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 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), - 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 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), - 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 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), Case("Testing FunctionPointerArg1 compatibility", test_fparg1), Case("Testing FunctionPointer compatibility", test_fparg0), diff --git a/features/net/network-socket/Socket.h b/features/net/network-socket/Socket.h index c481930440..39a89a529f 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 @@ public: * * @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 473ff12a14..7b0b20edc5 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 @@ public: * * @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 @@ public: * * @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 8ffd70164c..8a415bb6c4 100644 --- a/hal/api/Callback.h +++ b/hal/api/Callback.h @@ -29,711 +29,6 @@ namespace mbed { template class Callback; -/** Templated function class - */ -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 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 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, A4)) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; - } - - /** 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)) { - _obj = (void*)obj; - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; - } - - /** 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)) { - _obj = static_cast(obj); - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; - } - - /** Attach a Callback - * @param func The Callback to attach - */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; - } - - /** Call the attached function - */ - 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); - } - - /** Call the attached function - */ - R operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { - return call(a0, a1, a2, a3, a4); - } - - /** Test if function has been attached - */ - operator bool() const { - return _thunk; - } - - /** 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: - // 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); - } - - 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); - } - - 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); - } - - // 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 - * @param func Static function to attach - */ - Callback(R (*func)(A0, A1, A2, A3) = 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)) { - 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)) { - 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)) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; - } - - /** 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)) { - _obj = (void*)obj; - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; - } - - /** 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)) { - _obj = static_cast(obj); - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; - } - - /** Attach a Callback - * @param func The Callback to attach - */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._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); - } - - /** Call the attached function - */ - R operator()(A0 a0, A1 a1, A2 a2, A3 a3) { - return call(a0, a1, a2, a3); - } - - /** Test if function has been attached - */ - operator bool() const { - return _thunk; - } - - /** 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: - // 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); - } - - 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); - } - - // 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); -}; - -/** Templated function class - */ -template -class Callback { -public: - /** Create a Callback with a static function - * @param func Static function to attach - */ - Callback(R (*func)(A0, A1, A2) = 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)) { - 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)) { - 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)) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; - } - - /** 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)) { - _obj = (void*)obj; - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; - } - - /** 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)) { - _obj = static_cast(obj); - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; - } - - /** Attach a Callback - * @param func The Callback to attach - */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; - } - - /** Call the attached function - */ - R call(A0 a0, A1 a1, A2 a2) { - if (!_thunk) { - return (R)0; - } - return _thunk(_obj, &_func, a0, a1, a2); - } - - /** Call the attached function - */ - R operator()(A0 a0, A1 a1, A2 a2) { - return call(a0, a1, a2); - } - - /** Test if function has been attached - */ - operator bool() const { - return _thunk; - } - - /** 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: - // 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); - } - - template - static R _boundthunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { - return (*reinterpret_cast(func)) - (static_cast(obj), a0, a1, a2); - } - - template - static R _methodthunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (a0, a1, a2); - } - - // 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); -}; - -/** Templated function class - */ -template -class Callback { -public: - /** Create a Callback with a static function - * @param func Static function to attach - */ - Callback(R (*func)(A0, A1) = 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)) { - 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)) { - 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)) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; - } - - /** 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)) { - _obj = (void*)obj; - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; - } - - /** 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)) { - _obj = static_cast(obj); - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; - } - - /** Attach a Callback - * @param func The Callback to attach - */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; - } - - /** Call the attached function - */ - R call(A0 a0, A1 a1) { - if (!_thunk) { - return (R)0; - } - return _thunk(_obj, &_func, a0, a1); - } - - /** Call the attached function - */ - R operator()(A0 a0, A1 a1) { - return call(a0, a1); - } - - /** Test if function has been attached - */ - operator bool() const { - return _thunk; - } - - /** 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: - // Internal thunks for various function types - static R _staticthunk(void*, void *func, A0 a0, A1 a1) { - return (*reinterpret_cast(func)) - (a0, a1); - } - - template - static R _boundthunk(void *obj, void *func, A0 a0, A1 a1) { - return (*reinterpret_cast(func)) - (static_cast(obj), a0, a1); - } - - template - static R _methodthunk(void *obj, void *func, A0 a0, A1 a1) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (a0, a1); - } - - // 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); -}; - -/** Templated function class - */ -template -class Callback { -public: - /** Create a Callback with a static function - * @param func Static function to attach - */ - Callback(R (*func)(A0) = 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)) { - 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)) { - 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)) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; - } - - /** 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)) { - _obj = (void*)obj; - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; - } - - /** 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)) { - _obj = static_cast(obj); - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; - } - - /** Attach a Callback - * @param func The Callback to attach - */ - void attach(const Callback &func) { - _obj = func._obj; - memcpy(&_func, &func._func, sizeof _func); - _thunk = func._thunk; - } - - /** Call the attached function - */ - R call(A0 a0) { - if (!_thunk) { - return (R)0; - } - return _thunk(_obj, &_func, a0); - } - - /** Call the attached function - */ - R operator()(A0 a0) { - return call(a0); - } - - /** Test if function has been attached - */ - operator bool() const { - return _thunk; - } - - /** 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: - // Internal thunks for various function types - static R _staticthunk(void*, void *func, A0 a0) { - return (*reinterpret_cast(func)) - (a0); - } - - template - static R _boundthunk(void *obj, void *func, A0 a0) { - return (*reinterpret_cast(func)) - (static_cast(obj), a0); - } - - template - static R _methodthunk(void *obj, void *func, A0 a0) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (a0); - } - - // 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); -}; - /** Templated function class */ template @@ -746,6 +41,13 @@ public: attach(func); } + /** Create a Callback with another Callback + * @param func Callback to attach + */ + Callback(const Callback &func) { + 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 @@ -755,6 +57,21 @@ public: attach(obj, func); } + template + Callback(const T *obj, R (*func)(const T*)) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (*func)(volatile T*)) { + attach(obj, func); + } + + template + Callback(const volatile T *obj, R (*func)(const volatile 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 @@ -764,41 +81,34 @@ public: attach(obj, func); } - /** Create a Callback with another Callback - * @param func Callback to attach - */ - Callback(const Callback &func) { - attach(func); + template + Callback(const T *obj, R (T::*func)() const) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (T::*func)() volatile) { + attach(obj, func); + } + + template + Callback(const volatile T *obj, R (T::*func)() const volatile) { + attach(obj, func); } /** Attach a static function * @param func Static function to attach */ void attach(R (*func)()) { - memcpy(&_func, &func, sizeof func); - _thunk = func ? &Callback::_staticthunk : 0; - } + struct local { + static R _thunk(void*, void *func) { + return (*(R (**)())func)( + ); + } + }; - /** 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*)) { - _obj = (void*)obj; memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_boundthunk; - } - - /** 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)()) { - _obj = static_cast(obj); - memcpy(&_func, &func, sizeof func); - _thunk = &Callback::_methodthunk; + _thunk = func ? &local::_thunk : 0; } /** Attach a Callback @@ -810,6 +120,126 @@ public: _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*)) { + struct local { + static R _thunk(void *obj, void *func) { + return (*(R (**)(T*))func)( + (T*)obj); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (*func)(const T*)) { + struct local { + static R _thunk(void *obj, void *func) { + return (*(R (**)(const T*))func)( + (const T*)obj); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (*func)(volatile T*)) { + struct local { + static R _thunk(void *obj, void *func) { + return (*(R (**)(volatile T*))func)( + (volatile T*)obj); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (*func)(const volatile T*)) { + struct local { + static R _thunk(void *obj, void *func) { + return (*(R (**)(const volatile T*))func)( + (const volatile T*)obj); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _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)()) { + struct local { + static R _thunk(void *obj, void *func) { + return (((T*)obj)->*(*(R (T::**)())func))( + ); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (T::*func)() const) { + struct local { + static R _thunk(void *obj, void *func) { + return (((const T*)obj)->*(*(R (T::**)() const)func))( + ); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (T::*func)() volatile) { + struct local { + static R _thunk(void *obj, void *func) { + return (((volatile T*)obj)->*(*(R (T::**)() volatile)func))( + ); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (T::*func)() const volatile) { + struct local { + static R _thunk(void *obj, void *func) { + return (((const volatile T*)obj)->*(*(R (T::**)() const volatile)func))( + ); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + /** Call the attached function */ R call() { @@ -835,30 +265,11 @@ public: * @param func Callback to call passed as void pointer */ static R thunk(void *func) { - return static_cast*>(func) - ->call(); + return static_cast*>(func)->call( + ); } private: - // Internal thunks for various function types - static R _staticthunk(void*, void *func) { - return (*reinterpret_cast(func)) - (); - } - - template - static R _boundthunk(void *obj, void *func) { - return (*reinterpret_cast(func)) - (static_cast(obj)); - } - - template - static R _methodthunk(void *obj, void *func) { - return (static_cast(obj)->* - (*reinterpret_cast(func))) - (); - } - // 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 +283,1596 @@ private: void *_obj; // Thunk registered on attach to dispatch calls - R (*_thunk)(void*, void*); + R (*_thunk)(void*, void*); }; - typedef Callback event_callback_t; +/** Templated function class + */ +template +class Callback { +public: + /** Create a Callback with a static function + * @param func Static function to attach + */ + Callback(R (*func)(A0) = 0) { + attach(func); + } + + /** Create a Callback with another Callback + * @param func Callback to attach + */ + Callback(const Callback &func) { + 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)) { + attach(obj, func); + } + + template + Callback(const T *obj, R (*func)(const T*, A0)) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (*func)(volatile T*, A0)) { + attach(obj, func); + } + + template + Callback(const volatile T *obj, R (*func)(const volatile 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 + */ + template + Callback(T *obj, R (T::*func)(A0)) { + attach(obj, func); + } + + template + Callback(const T *obj, R (T::*func)(A0) const) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (T::*func)(A0) volatile) { + attach(obj, func); + } + + template + Callback(const volatile T *obj, R (T::*func)(A0) const volatile) { + attach(obj, func); + } + + /** Attach a static function + * @param func Static function to attach + */ + void attach(R (*func)(A0)) { + struct local { + static R _thunk(void*, void *func, A0 a0) { + return (*(R (**)(A0))func)( + a0); + } + }; + + memcpy(&_func, &func, sizeof func); + _thunk = func ? &local::_thunk : 0; + } + + /** Attach a Callback + * @param func The Callback to attach + */ + void attach(const Callback &func) { + _obj = func._obj; + memcpy(&_func, &func._func, sizeof _func); + _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)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0) { + return (*(R (**)(T*, A0))func)( + (T*)obj, a0); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (*func)(const T*, A0)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0) { + return (*(R (**)(const T*, A0))func)( + (const T*)obj, a0); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (*func)(volatile T*, A0)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0) { + return (*(R (**)(volatile T*, A0))func)( + (volatile T*)obj, a0); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0) { + return (*(R (**)(const volatile T*, A0))func)( + (const volatile T*)obj, a0); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _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, void *func, A0 a0) { + return (((T*)obj)->*(*(R (T::**)(A0))func))( + a0); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (T::*func)(A0) const) { + struct local { + static R _thunk(void *obj, void *func, A0 a0) { + return (((const T*)obj)->*(*(R (T::**)(A0) const)func))( + a0); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (T::*func)(A0) volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0) { + return (((volatile T*)obj)->*(*(R (T::**)(A0) volatile)func))( + a0); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (T::*func)(A0) const volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0) { + return (((const volatile T*)obj)->*(*(R (T::**)(A0) const volatile)func))( + a0); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + /** Call the attached function + */ + R call(A0 a0) { + if (!_thunk) { + return (R)0; + } + return _thunk(_obj, &_func, a0); + } + + /** Call the attached function + */ + R operator()(A0 a0) { + return call(a0); + } + + /** Test if function has been attached + */ + operator bool() const { + return _thunk; + } + + /** 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 + 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); +}; + +/** Templated function class + */ +template +class Callback { +public: + /** Create a Callback with a static function + * @param func Static function to attach + */ + Callback(R (*func)(A0, A1) = 0) { + attach(func); + } + + /** Create a Callback with another Callback + * @param func Callback to attach + */ + Callback(const Callback &func) { + 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)) { + attach(obj, func); + } + + template + Callback(const T *obj, R (*func)(const T*, A0, A1)) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (*func)(volatile T*, A0, A1)) { + attach(obj, func); + } + + 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 + */ + template + Callback(T *obj, R (T::*func)(A0, A1)) { + attach(obj, func); + } + + template + Callback(const T *obj, R (T::*func)(A0, A1) const) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (T::*func)(A0, A1) volatile) { + attach(obj, func); + } + + 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*, void *func, A0 a0, A1 a1) { + return (*(R (**)(A0, A1))func)( + a0, a1); + } + }; + + memcpy(&_func, &func, sizeof func); + _thunk = func ? &local::_thunk : 0; + } + + /** Attach a Callback + * @param func The Callback to attach + */ + void attach(const Callback &func) { + _obj = func._obj; + memcpy(&_func, &func._func, sizeof _func); + _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)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1) { + return (*(R (**)(T*, A0, A1))func)( + (T*)obj, a0, a1); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (*func)(const T*, A0, A1)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1) { + return (*(R (**)(const T*, A0, A1))func)( + (const T*)obj, a0, a1); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (*func)(volatile T*, A0, A1)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1) { + return (*(R (**)(volatile T*, A0, A1))func)( + (volatile T*)obj, a0, a1); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0, A1)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1) { + return (*(R (**)(const volatile T*, A0, A1))func)( + (const volatile T*)obj, a0, a1); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _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)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1) { + return (((T*)obj)->*(*(R (T::**)(A0, A1))func))( + a0, a1); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (T::*func)(A0, A1) const) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1) { + return (((const T*)obj)->*(*(R (T::**)(A0, A1) const)func))( + a0, a1); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (T::*func)(A0, A1) volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1) { + return (((volatile T*)obj)->*(*(R (T::**)(A0, A1) volatile)func))( + a0, a1); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (T::*func)(A0, A1) const volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1) { + return (((const volatile T*)obj)->*(*(R (T::**)(A0, A1) const volatile)func))( + a0, a1); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + /** Call the attached function + */ + R call(A0 a0, A1 a1) { + if (!_thunk) { + return (R)0; + } + return _thunk(_obj, &_func, a0, a1); + } + + /** Call the attached function + */ + R operator()(A0 a0, A1 a1) { + return call(a0, a1); + } + + /** Test if function has been attached + */ + operator bool() const { + return _thunk; + } + + /** 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 + 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); +}; + +/** Templated function class + */ +template +class Callback { +public: + /** Create a Callback with a static function + * @param func Static function to attach + */ + Callback(R (*func)(A0, A1, A2) = 0) { + attach(func); + } + + /** Create a Callback with another Callback + * @param func Callback to attach + */ + Callback(const Callback &func) { + 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)) { + attach(obj, func); + } + + template + Callback(const T *obj, R (*func)(const T*, A0, A1, A2)) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2)) { + attach(obj, func); + } + + template + Callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2)) { + 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)) { + attach(obj, func); + } + + template + Callback(const T *obj, R (T::*func)(A0, A1, A2) const) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (T::*func)(A0, A1, A2) volatile) { + attach(obj, func); + } + + 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, A1, A2)) { + struct local { + static R _thunk(void*, void *func, A0 a0, A1 a1, A2 a2) { + return (*(R (**)(A0, A1, A2))func)( + a0, a1, a2); + } + }; + + memcpy(&_func, &func, sizeof func); + _thunk = func ? &local::_thunk : 0; + } + + /** Attach a Callback + * @param func The Callback to attach + */ + void attach(const Callback &func) { + _obj = func._obj; + memcpy(&_func, &func._func, sizeof _func); + _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)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { + return (*(R (**)(T*, A0, A1, A2))func)( + (T*)obj, a0, a1, a2); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (*func)(const T*, A0, A1, A2)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { + return (*(R (**)(const T*, A0, A1, A2))func)( + (const T*)obj, a0, a1, a2); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (*func)(volatile T*, A0, A1, A2)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { + return (*(R (**)(volatile T*, A0, A1, A2))func)( + (volatile T*)obj, a0, a1, a2); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { + return (*(R (**)(const volatile T*, A0, A1, A2))func)( + (const volatile T*)obj, a0, a1, a2); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _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)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { + return (((T*)obj)->*(*(R (T::**)(A0, A1, A2))func))( + a0, a1, a2); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (T::*func)(A0, A1, A2) const) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { + return (((const T*)obj)->*(*(R (T::**)(A0, A1, A2) const)func))( + a0, a1, a2); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (T::*func)(A0, A1, A2) volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { + return (((volatile T*)obj)->*(*(R (T::**)(A0, A1, A2) volatile)func))( + a0, a1, a2); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (T::*func)(A0, A1, A2) const volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2) { + return (((const volatile T*)obj)->*(*(R (T::**)(A0, A1, A2) const volatile)func))( + a0, a1, a2); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + /** Call the attached function + */ + R call(A0 a0, A1 a1, A2 a2) { + if (!_thunk) { + return (R)0; + } + return _thunk(_obj, &_func, a0, a1, a2); + } + + /** Call the attached function + */ + R operator()(A0 a0, A1 a1, A2 a2) { + return call(a0, a1, a2); + } + + /** Test if function has been attached + */ + operator bool() const { + return _thunk; + } + + /** 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 + 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); +}; + +/** Templated function class + */ +template +class Callback { +public: + /** Create a Callback with a static function + * @param func Static function to attach + */ + Callback(R (*func)(A0, A1, A2, A3) = 0) { + attach(func); + } + + /** Create a Callback with another Callback + * @param func Callback to attach + */ + Callback(const Callback &func) { + 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)) { + attach(obj, func); + } + + template + Callback(const T *obj, R (*func)(const T*, A0, A1, A2, A3)) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3)) { + attach(obj, func); + } + + template + Callback(const volatile T *obj, R (*func)(const volatile 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 + */ + template + Callback(T *obj, R (T::*func)(A0, A1, A2, A3)) { + attach(obj, func); + } + + template + Callback(const T *obj, R (T::*func)(A0, A1, A2, A3) const) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (T::*func)(A0, A1, A2, A3) volatile) { + attach(obj, func); + } + + 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*, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*(R (**)(A0, A1, A2, A3))func)( + a0, a1, a2, a3); + } + }; + + memcpy(&_func, &func, sizeof func); + _thunk = func ? &local::_thunk : 0; + } + + /** Attach a Callback + * @param func The Callback to attach + */ + void attach(const Callback &func) { + _obj = func._obj; + memcpy(&_func, &func._func, sizeof _func); + _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)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*(R (**)(T*, A0, A1, A2, A3))func)( + (T*)obj, a0, a1, a2, a3); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (*func)(const T*, A0, A1, A2, A3)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*(R (**)(const T*, A0, A1, A2, A3))func)( + (const T*)obj, a0, a1, a2, a3); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*(R (**)(volatile T*, A0, A1, A2, A3))func)( + (volatile T*)obj, a0, a1, a2, a3); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (*(R (**)(const volatile T*, A0, A1, A2, A3))func)( + (const volatile T*)obj, a0, a1, a2, a3); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _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)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (((T*)obj)->*(*(R (T::**)(A0, A1, A2, A3))func))( + a0, a1, a2, a3); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (T::*func)(A0, A1, A2, A3) const) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (((const T*)obj)->*(*(R (T::**)(A0, A1, A2, A3) const)func))( + a0, a1, a2, a3); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (T::*func)(A0, A1, A2, A3) volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (((volatile T*)obj)->*(*(R (T::**)(A0, A1, A2, A3) volatile)func))( + a0, a1, a2, a3); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (T::*func)(A0, A1, A2, A3) const volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3) { + return (((const volatile T*)obj)->*(*(R (T::**)(A0, A1, A2, A3) const volatile)func))( + a0, a1, a2, a3); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _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); + } + + /** Call the attached function + */ + R operator()(A0 a0, A1 a1, A2 a2, A3 a3) { + return call(a0, a1, a2, a3); + } + + /** Test if function has been attached + */ + operator bool() const { + return _thunk; + } + + /** 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*, void*, A0, A1, A2, A3); +}; + +/** Templated function class + */ +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 another Callback + * @param func Callback to attach + */ + Callback(const Callback &func) { + 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); + } + + template + Callback(const T *obj, R (*func)(const T*, A0, A1, A2, A3, A4)) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3, A4)) { + attach(obj, func); + } + + 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); + } + + template + Callback(const T *obj, R (T::*func)(A0, A1, A2, A3, A4) const) { + attach(obj, func); + } + + template + Callback(volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) volatile) { + attach(obj, func); + } + + 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*, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*(R (**)(A0, A1, A2, A3, A4))func)( + a0, a1, a2, a3, a4); + } + }; + + memcpy(&_func, &func, sizeof func); + _thunk = func ? &local::_thunk : 0; + } + + /** Attach a Callback + * @param func The Callback to attach + */ + void attach(const Callback &func) { + _obj = func._obj; + memcpy(&_func, &func._func, sizeof _func); + _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, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*(R (**)(T*, A0, A1, A2, A3, A4))func)( + (T*)obj, a0, a1, a2, a3, a4); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (*func)(const T*, A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*(R (**)(const T*, A0, A1, A2, A3, A4))func)( + (const T*)obj, a0, a1, a2, a3, a4); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*(R (**)(volatile T*, A0, A1, A2, A3, A4))func)( + (volatile T*)obj, a0, a1, a2, a3, a4); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3, A4)) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (*(R (**)(const volatile T*, A0, A1, A2, A3, A4))func)( + (const volatile T*)obj, a0, a1, a2, a3, a4); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _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, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (((T*)obj)->*(*(R (T::**)(A0, A1, A2, A3, A4))func))( + a0, a1, a2, a3, a4); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const T *obj, R (T::*func)(A0, A1, A2, A3, A4) const) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (((const T*)obj)->*(*(R (T::**)(A0, A1, A2, A3, A4) const)func))( + a0, a1, a2, a3, a4); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (((volatile T*)obj)->*(*(R (T::**)(A0, A1, A2, A3, A4) volatile)func))( + a0, a1, a2, a3, a4); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + template + void attach(const volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) const volatile) { + struct local { + static R _thunk(void *obj, void *func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return (((const volatile T*)obj)->*(*(R (T::**)(A0, A1, A2, A3, A4) const volatile)func))( + a0, a1, a2, a3, a4); + } + }; + + _obj = (void*)obj; + memcpy(&_func, &func, sizeof func); + _thunk = &local::_thunk; + } + + /** Call the attached function + */ + 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); + } + + /** Call the attached function + */ + R operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) { + return call(a0, a1, a2, a3, a4); + } + + /** Test if function has been attached + */ + operator bool() const { + return _thunk; + } + + /** 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 + 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); +}; + +typedef Callback event_callback_t; + + +/** 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 type infered from arguments + */ +template +Callback callback(R (*func)() = 0) { + return Callback(func); +} + +template +Callback callback(const Callback &func) { + return Callback(func); +} +template +Callback callback(T *obj, R (*func)(T*)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (*func)(const T*)) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (*func)(volatile T*)) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*)) { + return Callback(obj, func); +} + +template +Callback callback(T *obj, R (T::*func)()) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (T::*func)() const) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (T::*func)() volatile) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (T::*func)() const volatile) { + return Callback(obj, func); +} + +template +Callback callback(R (*func)(A0) = 0) { + return Callback(func); +} + +template +Callback callback(const Callback &func) { + return Callback(func); +} +template +Callback callback(T *obj, R (*func)(T*, A0)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (*func)(const T*, A0)) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0)) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0)) { + return Callback(obj, func); +} + +template +Callback callback(T *obj, R (T::*func)(A0)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (T::*func)(A0) const) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (T::*func)(A0) volatile) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (T::*func)(A0) const volatile) { + return Callback(obj, func); +} + +template +Callback callback(R (*func)(A0, A1) = 0) { + return Callback(func); +} + +template +Callback callback(const Callback &func) { + return Callback(func); +} +template +Callback callback(T *obj, R (*func)(T*, A0, A1)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (*func)(const T*, A0, A1)) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0, A1)) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1)) { + return Callback(obj, func); +} + +template +Callback callback(T *obj, R (T::*func)(A0, A1)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (T::*func)(A0, A1) const) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (T::*func)(A0, A1) volatile) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (T::*func)(A0, A1) const volatile) { + return Callback(obj, func); +} + +template +Callback callback(R (*func)(A0, A1, A2) = 0) { + return Callback(func); +} + +template +Callback callback(const Callback &func) { + return Callback(func); +} +template +Callback callback(T *obj, R (*func)(T*, A0, A1, A2)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (*func)(const T*, A0, A1, A2)) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2)) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2)) { + return Callback(obj, func); +} + +template +Callback callback(T *obj, R (T::*func)(A0, A1, A2)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (T::*func)(A0, A1, A2) const) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (T::*func)(A0, A1, A2) volatile) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (T::*func)(A0, A1, A2) const volatile) { + return Callback(obj, func); +} + +template +Callback callback(R (*func)(A0, A1, A2, A3) = 0) { + return Callback(func); +} + +template +Callback callback(const Callback &func) { + return Callback(func); +} +template +Callback callback(T *obj, R (*func)(T*, A0, A1, A2, A3)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (*func)(const T*, A0, A1, A2, A3)) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3)) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3)) { + return Callback(obj, func); +} + +template +Callback callback(T *obj, R (T::*func)(A0, A1, A2, A3)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (T::*func)(A0, A1, A2, A3) const) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (T::*func)(A0, A1, A2, A3) volatile) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (T::*func)(A0, A1, A2, A3) const volatile) { + return Callback(obj, func); +} + +template +Callback callback(R (*func)(A0, A1, A2, A3, A4) = 0) { + return Callback(func); +} + +template +Callback callback(const Callback &func) { + return Callback(func); +} +template +Callback callback(T *obj, R (*func)(T*, A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (*func)(const T*, A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (*func)(volatile T*, A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +template +Callback callback(const volatile T *obj, R (*func)(const volatile T*, A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +template +Callback callback(T *obj, R (T::*func)(A0, A1, A2, A3, A4)) { + return Callback(obj, func); +} + +template +Callback callback(const T *obj, R (T::*func)(A0, A1, A2, A3, A4) const) { + return Callback(obj, func); +} + +template +Callback callback(volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) volatile) { + return Callback(obj, func); +} + +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/InterruptIn.h b/hal/api/InterruptIn.h index 3758ab5ca3..fcb7df4b81 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 @@ public: * * @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 @@ public: * * @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 1d7ae7c34d..be79293095 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 @@ public: * @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 @@ public: * @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 a7848ece4d..5b2f97ba95 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 @@ public: * @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 @@ public: * @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/rtos/rtos/RtosTimer.h b/rtos/rtos/RtosTimer.h index ab698ebd36..3f39a3999a 100644 --- a/rtos/rtos/RtosTimer.h +++ b/rtos/rtos/RtosTimer.h @@ -47,7 +47,7 @@ public: 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 @@ public: @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 eb1c39beab..9d3356a607 100644 --- a/rtos/rtos/Thread.h +++ b/rtos/rtos/Thread.h @@ -90,7 +90,7 @@ public: 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 @@ public: 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 @@ public: 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 @@ public: @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