Skip to content

Commit 08ecdb2

Browse files
committed
Merge pull request #139 from geky/callback-fixes
Small fixes for issues with Callback class
2 parents b98152e + 753720a commit 08ecdb2

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

hal/api/CAN.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,20 @@ class CAN {
218218
* @param method pointer to the member function to be called
219219
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
220220
*/
221-
template<typename T>
222-
void attach(T* obj, void (T::*method)(void), IrqType type=RxIrq) {
221+
template<typename T>
222+
void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
223+
attach(Callback<void()>(obj, method), type);
224+
}
225+
226+
/** Attach a member function to call whenever a CAN frame received interrupt
227+
* is generated.
228+
*
229+
* @param obj pointer to the object to call the member function on
230+
* @param method pointer to the member function to be called
231+
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
232+
*/
233+
template<typename T>
234+
void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
223235
attach(Callback<void()>(obj, method), type);
224236
}
225237

hal/api/Callback.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class Callback<R(A0, A1, A2, A3, A4)> {
106106
/** Call the attached function
107107
*/
108108
R call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
109+
if (!_thunk) {
110+
return (R)0;
111+
}
109112
return _thunk(_obj, &_func, a0, a1, a2, a3, a4);
110113
}
111114

@@ -244,6 +247,9 @@ class Callback<R(A0, A1, A2, A3)> {
244247
/** Call the attached function
245248
*/
246249
R call(A0 a0, A1 a1, A2 a2, A3 a3) {
250+
if (!_thunk) {
251+
return (R)0;
252+
}
247253
return _thunk(_obj, &_func, a0, a1, a2, a3);
248254
}
249255

@@ -382,6 +388,9 @@ class Callback<R(A0, A1, A2)> {
382388
/** Call the attached function
383389
*/
384390
R call(A0 a0, A1 a1, A2 a2) {
391+
if (!_thunk) {
392+
return (R)0;
393+
}
385394
return _thunk(_obj, &_func, a0, a1, a2);
386395
}
387396

@@ -520,6 +529,9 @@ class Callback<R(A0, A1)> {
520529
/** Call the attached function
521530
*/
522531
R call(A0 a0, A1 a1) {
532+
if (!_thunk) {
533+
return (R)0;
534+
}
523535
return _thunk(_obj, &_func, a0, a1);
524536
}
525537

@@ -658,6 +670,9 @@ class Callback<R(A0)> {
658670
/** Call the attached function
659671
*/
660672
R call(A0 a0) {
673+
if (!_thunk) {
674+
return (R)0;
675+
}
661676
return _thunk(_obj, &_func, a0);
662677
}
663678

@@ -796,7 +811,7 @@ class Callback<R()> {
796811
/** Call the attached function
797812
*/
798813
R call() {
799-
if (NULL == _thunk) {
814+
if (!_thunk) {
800815
return (R)0;
801816
}
802817
return _thunk(_obj, &_func);

hal/api/SerialBase.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,19 @@ class SerialBase {
100100
* @param method pointer to the member function to be called
101101
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
102102
*/
103-
template<typename T, typename M>
104-
void attach(T *obj, M method, IrqType type=RxIrq) {
103+
template<typename T>
104+
void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
105+
attach(Callback<void()>(obj, method), type);
106+
}
107+
108+
/** Attach a member function to call whenever a serial interrupt is generated
109+
*
110+
* @param obj pointer to the object to call the member function on
111+
* @param method pointer to the member function to be called
112+
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
113+
*/
114+
template<typename T>
115+
void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) {
105116
attach(Callback<void()>(obj, method), type);
106117
}
107118

net/ESP8266Interface/ESP8266/ATParser/BufferedSerial/BufferedSerial.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,18 @@ class BufferedSerial : public RawSerial
149149
* @param method pointer to the member function to call
150150
* @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
151151
*/
152-
template <typename T, typename M>
153-
void attach(T *obj, M method, IrqType type=RxIrq) {
152+
template <typename T>
153+
void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
154+
attach(Callback<void()>(obj, method), type);
155+
}
156+
157+
/** Attach a member function to call whenever a serial interrupt is generated
158+
* @param obj pointer to the object to call the member function on
159+
* @param method pointer to the member function to call
160+
* @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
161+
*/
162+
template <typename T>
163+
void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) {
154164
attach(Callback<void()>(obj, method), type);
155165
}
156166
};

0 commit comments

Comments
 (0)