Skip to content

Commit c3d9192

Browse files
committed
Merge pull request #1793 from theotherjimmy/callback
mv #1783(Improve FunctionPointer class) + minor compatibility fix
2 parents cd9f933 + 48fd7d7 commit c3d9192

14 files changed

+1025
-281
lines changed

hal/api/CAN.h

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "can_api.h"
2424
#include "can_helper.h"
25-
#include "FunctionPointer.h"
25+
#include "Callback.h"
2626

2727
namespace mbed {
2828

@@ -206,34 +206,40 @@ class CAN {
206206
/** Attach a function to call whenever a CAN frame received interrupt is
207207
* generated.
208208
*
209-
* @param fptr A pointer to a void function, or 0 to set as none
209+
* @param func A pointer to a void function, or 0 to set as none
210210
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
211211
*/
212-
void attach(void (*fptr)(void), IrqType type=RxIrq);
212+
void attach(Callback<void()> func, IrqType type=RxIrq);
213213

214214
/** Attach a member function to call whenever a CAN frame received interrupt
215215
* is generated.
216216
*
217-
* @param tptr pointer to the object to call the member function on
218-
* @param mptr pointer to the member function to be called
217+
* @param obj pointer to the object to call the member function on
218+
* @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* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
223-
if((mptr != NULL) && (tptr != NULL)) {
224-
_irq[type].attach(tptr, mptr);
225-
can_irq_set(&_can, (CanIrqType)type, 1);
226-
}
227-
else {
228-
can_irq_set(&_can, (CanIrqType)type, 0);
229-
}
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) {
235+
attach(Callback<void()>(obj, method), type);
230236
}
231237

232238
static void _irq_handler(uint32_t id, CanIrqType type);
233239

234240
protected:
235-
can_t _can;
236-
FunctionPointer _irq[9];
241+
can_t _can;
242+
Callback<void()> _irq[9];
237243
};
238244

239245
} // namespace mbed

hal/api/CallChain.h

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef MBED_CALLCHAIN_H
1717
#define MBED_CALLCHAIN_H
1818

19-
#include "FunctionPointer.h"
19+
#include "Callback.h"
2020
#include <string.h>
2121

2222
namespace mbed {
@@ -57,7 +57,7 @@ namespace mbed {
5757
* @endcode
5858
*/
5959

60-
typedef FunctionPointer* pFunctionPointer_t;
60+
typedef Callback<void()> *pFunctionPointer_t;
6161

6262
class CallChain {
6363
public:
@@ -70,34 +70,34 @@ class CallChain {
7070

7171
/** Add a function at the end of the chain
7272
*
73-
* @param function A pointer to a void function
73+
* @param func A pointer to a void function
7474
*
7575
* @returns
76-
* The function object created for 'function'
76+
* The function object created for 'func'
7777
*/
78-
pFunctionPointer_t add(void (*function)(void));
78+
pFunctionPointer_t add(Callback<void()> func);
7979

8080
/** Add a function at the end of the chain
8181
*
82-
* @param tptr pointer to the object to call the member function on
83-
* @param mptr pointer to the member function to be called
82+
* @param obj pointer to the object to call the member function on
83+
* @param method pointer to the member function to be called
8484
*
8585
* @returns
86-
* The function object created for 'tptr' and 'mptr'
86+
* The function object created for 'obj' and 'method'
8787
*/
88-
template<typename T>
89-
pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
90-
return common_add(new FunctionPointer(tptr, mptr));
88+
template<typename T, typename M>
89+
pFunctionPointer_t add(T *obj, M method) {
90+
return add(Callback<void()>(obj, method));
9191
}
9292

9393
/** Add a function at the beginning of the chain
9494
*
95-
* @param function A pointer to a void function
95+
* @param func A pointer to a void function
9696
*
9797
* @returns
98-
* The function object created for 'function'
98+
* The function object created for 'func'
9999
*/
100-
pFunctionPointer_t add_front(void (*function)(void));
100+
pFunctionPointer_t add_front(Callback<void()> func);
101101

102102
/** Add a function at the beginning of the chain
103103
*
@@ -107,9 +107,9 @@ class CallChain {
107107
* @returns
108108
* The function object created for 'tptr' and 'mptr'
109109
*/
110-
template<typename T>
111-
pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) {
112-
return common_add_front(new FunctionPointer(tptr, mptr));
110+
template<typename T, typename M>
111+
pFunctionPointer_t add_front(T *obj, M method) {
112+
return add_front(Callback<void()>(obj, method));
113113
}
114114

115115
/** Get the number of functions in the chain
@@ -162,8 +162,6 @@ class CallChain {
162162

163163
private:
164164
void _check_size();
165-
pFunctionPointer_t common_add(pFunctionPointer_t pf);
166-
pFunctionPointer_t common_add_front(pFunctionPointer_t pf);
167165

168166
pFunctionPointer_t* _chain;
169167
int _size;

0 commit comments

Comments
 (0)