Skip to content

Commit 89d5583

Browse files
authored
Merge pull request #2190 from geky/callback-const
[api] Fix handling of const objects in Callback class
2 parents c9d8690 + 8e60fdd commit 89d5583

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

TESTS/mbed_drivers/callback/main.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,48 @@ T bound_func1(Thing<T> *t, T a0) { return t->t | a0; }
4848
template <typename T>
4949
T bound_func0(Thing<T> *t) { return t->t; }
5050

51+
// const bound functions
52+
template <typename T>
53+
T const_func5(const Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; }
54+
template <typename T>
55+
T const_func4(const Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; }
56+
template <typename T>
57+
T const_func3(const Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; }
58+
template <typename T>
59+
T const_func2(const Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; }
60+
template <typename T>
61+
T const_func1(const Thing<T> *t, T a0) { return t->t | a0; }
62+
template <typename T>
63+
T const_func0(const Thing<T> *t) { return t->t; }
64+
65+
// volatile bound functions
66+
template <typename T>
67+
T volatile_func5(volatile Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; }
68+
template <typename T>
69+
T volatile_func4(volatile Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; }
70+
template <typename T>
71+
T volatile_func3(volatile Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; }
72+
template <typename T>
73+
T volatile_func2(volatile Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; }
74+
template <typename T>
75+
T volatile_func1(volatile Thing<T> *t, T a0) { return t->t | a0; }
76+
template <typename T>
77+
T volatile_func0(volatile Thing<T> *t) { return t->t; }
78+
79+
// const volatil bound functions
80+
template <typename T>
81+
T const_volatile_func5(const volatile Thing<T> *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; }
82+
template <typename T>
83+
T const_volatile_func4(const volatile Thing<T> *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; }
84+
template <typename T>
85+
T const_volatile_func3(const volatile Thing<T> *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; }
86+
template <typename T>
87+
T const_volatile_func2(const volatile Thing<T> *t, T a0, T a1) { return t->t | a0 | a1; }
88+
template <typename T>
89+
T const_volatile_func1(const volatile Thing<T> *t, T a0) { return t->t | a0; }
90+
template <typename T>
91+
T const_volatile_func0(const volatile Thing<T> *t) { return t->t; }
92+
5193

5294
// function call and result verification
5395
template <typename T>
@@ -133,6 +175,9 @@ void test_dispatch5() {
133175
Verifier<T>::verify5(static_func5<T>);
134176
Verifier<T>::verify5(&thing, &Thing<T>::member_func5);
135177
Verifier<T>::verify5(&thing, &bound_func5<T>);
178+
Verifier<T>::verify5((const Thing<T>*)&thing, &const_func5<T>);
179+
Verifier<T>::verify5((volatile Thing<T>*)&thing, &volatile_func5<T>);
180+
Verifier<T>::verify5((const volatile Thing<T>*)&thing, &const_volatile_func5<T>);
136181

137182
Callback<T(T,T,T,T,T)> callback(static_func5);
138183
Verifier<T>::verify5(callback);
@@ -147,6 +192,9 @@ void test_dispatch4() {
147192
Verifier<T>::verify4(static_func4<T>);
148193
Verifier<T>::verify4(&thing, &Thing<T>::member_func4);
149194
Verifier<T>::verify4(&thing, &bound_func4<T>);
195+
Verifier<T>::verify4((const Thing<T>*)&thing, &const_func4<T>);
196+
Verifier<T>::verify4((volatile Thing<T>*)&thing, &volatile_func4<T>);
197+
Verifier<T>::verify4((const volatile Thing<T>*)&thing, &const_volatile_func4<T>);
150198

151199
Callback<T(T,T,T,T)> callback(static_func4);
152200
Verifier<T>::verify4(callback);
@@ -161,6 +209,9 @@ void test_dispatch3() {
161209
Verifier<T>::verify3(static_func3<T>);
162210
Verifier<T>::verify3(&thing, &Thing<T>::member_func3);
163211
Verifier<T>::verify3(&thing, &bound_func3<T>);
212+
Verifier<T>::verify3((const Thing<T>*)&thing, &const_func3<T>);
213+
Verifier<T>::verify3((volatile Thing<T>*)&thing, &volatile_func3<T>);
214+
Verifier<T>::verify3((const volatile Thing<T>*)&thing, &const_volatile_func3<T>);
164215

165216
Callback<T(T,T,T)> callback(static_func3);
166217
Verifier<T>::verify3(callback);
@@ -175,6 +226,9 @@ void test_dispatch2() {
175226
Verifier<T>::verify2(static_func2<T>);
176227
Verifier<T>::verify2(&thing, &Thing<T>::member_func2);
177228
Verifier<T>::verify2(&thing, &bound_func2<T>);
229+
Verifier<T>::verify2((const Thing<T>*)&thing, &const_func2<T>);
230+
Verifier<T>::verify2((volatile Thing<T>*)&thing, &volatile_func2<T>);
231+
Verifier<T>::verify2((const volatile Thing<T>*)&thing, &const_volatile_func2<T>);
178232

179233
Callback<T(T,T)> callback(static_func2);
180234
Verifier<T>::verify2(callback);
@@ -189,6 +243,9 @@ void test_dispatch1() {
189243
Verifier<T>::verify1(static_func1<T>);
190244
Verifier<T>::verify1(&thing, &Thing<T>::member_func1);
191245
Verifier<T>::verify1(&thing, &bound_func1<T>);
246+
Verifier<T>::verify1((const Thing<T>*)&thing, &const_func1<T>);
247+
Verifier<T>::verify1((volatile Thing<T>*)&thing, &volatile_func1<T>);
248+
Verifier<T>::verify1((const volatile Thing<T>*)&thing, &const_volatile_func1<T>);
192249

193250
Callback<T(T)> callback(static_func1);
194251
Verifier<T>::verify1(callback);
@@ -203,6 +260,9 @@ void test_dispatch0() {
203260
Verifier<T>::verify0(static_func0<T>);
204261
Verifier<T>::verify0(&thing, &Thing<T>::member_func0);
205262
Verifier<T>::verify0(&thing, &bound_func0<T>);
263+
Verifier<T>::verify0((const Thing<T>*)&thing, &const_func0<T>);
264+
Verifier<T>::verify0((volatile Thing<T>*)&thing, &volatile_func0<T>);
265+
Verifier<T>::verify0((const volatile Thing<T>*)&thing, &const_volatile_func0<T>);
206266

207267
Callback<T()> callback(static_func0);
208268
Verifier<T>::verify0(callback);

hal/api/Callback.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Callback<R(A0, A1, A2, A3, A4)> {
8080
*/
8181
template <typename T>
8282
void attach(T *obj, R (*func)(T*, A0, A1, A2, A3, A4)) {
83-
_obj = static_cast<void*>(obj);
83+
_obj = (void*)obj;
8484
memcpy(&_func, &func, sizeof func);
8585
_thunk = &Callback::_boundthunk<T>;
8686
}
@@ -221,7 +221,7 @@ class Callback<R(A0, A1, A2, A3)> {
221221
*/
222222
template <typename T>
223223
void attach(T *obj, R (*func)(T*, A0, A1, A2, A3)) {
224-
_obj = static_cast<void*>(obj);
224+
_obj = (void*)obj;
225225
memcpy(&_func, &func, sizeof func);
226226
_thunk = &Callback::_boundthunk<T>;
227227
}
@@ -362,7 +362,7 @@ class Callback<R(A0, A1, A2)> {
362362
*/
363363
template <typename T>
364364
void attach(T *obj, R (*func)(T*, A0, A1, A2)) {
365-
_obj = static_cast<void*>(obj);
365+
_obj = (void*)obj;
366366
memcpy(&_func, &func, sizeof func);
367367
_thunk = &Callback::_boundthunk<T>;
368368
}
@@ -503,7 +503,7 @@ class Callback<R(A0, A1)> {
503503
*/
504504
template <typename T>
505505
void attach(T *obj, R (*func)(T*, A0, A1)) {
506-
_obj = static_cast<void*>(obj);
506+
_obj = (void*)obj;
507507
memcpy(&_func, &func, sizeof func);
508508
_thunk = &Callback::_boundthunk<T>;
509509
}
@@ -644,7 +644,7 @@ class Callback<R(A0)> {
644644
*/
645645
template <typename T>
646646
void attach(T *obj, R (*func)(T*, A0)) {
647-
_obj = static_cast<void*>(obj);
647+
_obj = (void*)obj;
648648
memcpy(&_func, &func, sizeof func);
649649
_thunk = &Callback::_boundthunk<T>;
650650
}
@@ -785,7 +785,7 @@ class Callback<R()> {
785785
*/
786786
template <typename T>
787787
void attach(T *obj, R (*func)(T*)) {
788-
_obj = static_cast<void*>(obj);
788+
_obj = (void*)obj;
789789
memcpy(&_func, &func, sizeof func);
790790
_thunk = &Callback::_boundthunk<T>;
791791
}

0 commit comments

Comments
 (0)