Skip to content

Fixes to FunctionPointer and friends #28

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

Merged
merged 3 commits into from
Jun 10, 2015
Merged
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
2 changes: 1 addition & 1 deletion mbed/FunctionPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace mbed {
/** A class for storing and calling a pointer to a static or member void function
*/
template <typename R>
class FunctionPointer0 : protected FunctionPointerBase<R>{
class FunctionPointer0 : public FunctionPointerBase<R>{
public:
typedef R(*static_fp)(void);
typedef struct arg_struct{
Expand Down
35 changes: 23 additions & 12 deletions mbed/FunctionPointerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,34 @@ class FunctionPointerBase {
return (_membercaller != NULL) && (_object != NULL);
}

/**
* Clears the current function pointer assignment
* After clear(), this instance will point to nothing (NULL)
*/
virtual void clear() {
_membercaller = NULL;
_object = NULL;
}

protected:
struct ArgOps {
void (*constructor)(void *, va_list);
void (*copy_args)(void *, void *);
void (*destructor)(void *);
};
void * _object; // object Pointer/function pointer
R (*_membercaller)(void *, uintptr_t *, void *);
// aligned raw member function pointer storage - converted back by registered _membercaller
uintptr_t _member[4];
static const struct ArgOps _nullops;

protected:
FunctionPointerBase():_object(NULL), _membercaller(NULL) {}
FunctionPointerBase(const FunctionPointerBase<R> & fp) {
copy(&fp);
}
virtual ~FunctionPointerBase() {
}

/**
* Calls the member pointed to by object::member or (function)object
Expand All @@ -43,18 +66,6 @@ class FunctionPointerBase {
inline R call(void* arg) {
return _membercaller(_object, _member, arg);
}
protected:
FunctionPointerBase():_object(NULL), _membercaller(NULL) {}
FunctionPointerBase(const FunctionPointerBase<R> & fp) {
copy(&fp);
}
static const struct ArgOps _nullops;

protected:
void * _object; // object Pointer/function pointer
R (*_membercaller)(void *, uintptr_t *, void *);
// aligned raw member function pointer storage - converted back by registered _membercaller
uintptr_t _member[4];

void copy(const FunctionPointerBase<R> * fp) {
_object = fp->_object;
Expand Down
22 changes: 19 additions & 3 deletions mbed/FunctionPointerBind.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
namespace mbed{

template<typename R>
class FunctionPointerBind : FunctionPointerBase<R> {
class FunctionPointerBind : public FunctionPointerBase<R> {
public:
// Call the Event
inline R call() {
Expand All @@ -46,12 +46,13 @@ class FunctionPointerBind : FunctionPointerBase<R> {
FunctionPointerBase<R>(),
_ops(&FunctionPointerBase<R>::_nullops)
{}

FunctionPointerBind(const FunctionPointerBind<R> & fp):
FunctionPointerBase<R>(),
_ops(&FunctionPointerBase<R>::_nullops)
{
_ops(&FunctionPointerBase<R>::_nullops) {
*this = fp;
}

~FunctionPointerBind() {
_ops->destructor(_storage);
}
Expand All @@ -66,6 +67,21 @@ class FunctionPointerBind : FunctionPointerBase<R> {
return *this;
}

R operator ()() {
return call();
}

/**
* Clears the current binding, making this instance unbound
*/
virtual void clear() {
if (_ops != &FunctionPointerBase<R>::_nullops) {
_ops->destructor(_storage);
}
_ops = &FunctionPointerBase<R>::_nullops;
FunctionPointerBase<R>::clear();
}

template<typename S>
FunctionPointerBind<R> & bind(const struct FunctionPointerBase<R>::ArgOps * ops , S * argStruct, FunctionPointerBase<R> *fp, ...) {
MBED_STATIC_ASSERT(sizeof(S) <= sizeof(_storage), ERROR: Arguments too large for FunctionPointerBind internal storage)
Expand Down