Skip to content

Improve FunctionPointer class #1783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions hal/api/CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "can_api.h"
#include "can_helper.h"
#include "FunctionPointer.h"
#include "Callback.h"

namespace mbed {

Expand Down Expand Up @@ -206,34 +206,40 @@ class CAN {
/** Attach a function to call whenever a CAN frame received interrupt is
* generated.
*
* @param fptr A pointer to a void function, or 0 to set as none
* @param func A pointer to a void function, or 0 to set as none
* @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)
*/
void attach(void (*fptr)(void), IrqType type=RxIrq);
void attach(Callback<void()> func, IrqType type=RxIrq);

/** Attach a member function to call whenever a CAN frame received interrupt
* is generated.
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
* @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)
*/
template<typename T>
void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
if((mptr != NULL) && (tptr != NULL)) {
_irq[type].attach(tptr, mptr);
can_irq_set(&_can, (CanIrqType)type, 1);
}
else {
can_irq_set(&_can, (CanIrqType)type, 0);
}
template<typename T>
void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
attach(Callback<void()>(obj, method), type);
}

/** Attach a member function to call whenever a CAN frame received interrupt
* is generated.
*
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
* @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)
*/
template<typename T>
void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
attach(Callback<void()>(obj, method), type);
}

static void _irq_handler(uint32_t id, CanIrqType type);

protected:
can_t _can;
FunctionPointer _irq[9];
can_t _can;
Callback<void()> _irq[9];
};

} // namespace mbed
Expand Down
36 changes: 17 additions & 19 deletions hal/api/CallChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#ifndef MBED_CALLCHAIN_H
#define MBED_CALLCHAIN_H

#include "FunctionPointer.h"
#include "Callback.h"
#include <string.h>

namespace mbed {
Expand Down Expand Up @@ -57,7 +57,7 @@ namespace mbed {
* @endcode
*/

typedef FunctionPointer* pFunctionPointer_t;
typedef Callback<void()> *pFunctionPointer_t;

class CallChain {
public:
Expand All @@ -70,34 +70,34 @@ class CallChain {

/** Add a function at the end of the chain
*
* @param function A pointer to a void function
* @param func A pointer to a void function
*
* @returns
* The function object created for 'function'
* The function object created for 'func'
*/
pFunctionPointer_t add(void (*function)(void));
pFunctionPointer_t add(Callback<void()> func);

/** Add a function at the end of the chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
* The function object created for 'obj' and 'method'
*/
template<typename T>
pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
return common_add(new FunctionPointer(tptr, mptr));
template<typename T, typename M>
pFunctionPointer_t add(T *obj, M method) {
return add(Callback<void()>(obj, method));
}

/** Add a function at the beginning of the chain
*
* @param function A pointer to a void function
* @param func A pointer to a void function
*
* @returns
* The function object created for 'function'
* The function object created for 'func'
*/
pFunctionPointer_t add_front(void (*function)(void));
pFunctionPointer_t add_front(Callback<void()> func);

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

/** Get the number of functions in the chain
Expand Down Expand Up @@ -162,8 +162,6 @@ class CallChain {

private:
void _check_size();
pFunctionPointer_t common_add(pFunctionPointer_t pf);
pFunctionPointer_t common_add_front(pFunctionPointer_t pf);

pFunctionPointer_t* _chain;
int _size;
Expand Down
Loading