using unknown member function pointers

18 Oct 2011

Hello. Haven't been around for a while and I'm now getting back into my projects trying to solve some problems that have been plaguing me.

I'm trying to write a class that has a method very similar to the attach(), rise(), fall(), etc. methods available to many classes in the mbed library that allow interrupt handlers to be called for their various events. I would like to overload my method for each of the types of handlers, including member functions.

The overload for the static functions is easy to figure out, all we need to do is copy the pointer of an unknown void func(void) and then call it whenever the event is raised.

The overload for the member functions is a bit more tricky. Somehow we need to store the pointer to an unknown class and a pointer to a member function of that class (the user's class). Has anyone been able to do this? I would very much like some help for this part of my class.

I've seen "Feedback wanted from anyone using attach()/rise()/etc with pointer to member functions?" which was started by Simon, and it looks like he's found a better implementation. So far I haven't got ANY implementation, so I'll take any help I can get.

Also, I really don't want to mess with templating my class or turning it into a base class with virtual functions. I'm really looking for behavior similar to InterruptIn and Serial.

Thank you.

18 Oct 2011

Hi Thomas,

Perhaps this tutorial that Andy wrote will help give you some ideas:

Hope that helps, Simon

19 Oct 2011

Hi Thomas,

I had a same problem with yours( maybe, sorry if not so ) for a long time. It was my headache. Your post made me try again and my problem was solved just now. The problem was a template version attach issue. You can try code like this.

CAUTION

The line "fp.attach( optr, fptr );" in template function must be in Foo.h file with Foo class definition. You will get "Undefined symbol ..." form Compailer if you move it to Foo.cpp file.

#include "mbed.h"

class Foo {
public:
    void attach( void ( *fptr )( void ) ) {
        fp.attach( fptr );
    }
    template<typename T> void attach( T *optr, void ( T::*fptr )( void ) ) {
        fp.attach( optr, fptr );
    }
protected:
    FunctionPointer fp;
};

class Bar {
public:
    Bar( void ) {    // Constructor
        foo.attach( this, &Bar::func );
    }
protected:
    Foo foo;
    void func( void ) {
        // do something.
    }
};

Bar bar;    // Global object

int main() {
    // do something.
}

Now you can use "fp.call()" anywhere in Foo function to invoke "Bar.funk()";

REGARDS, K.Shibata

19 Oct 2011

Thank you very much for your responses!

Hmm... The last time I was working on this (about a year ago) the FunctionPointer class did not exist. It is now avoiding my question; the FunctionPointer class is hiding all of the stuff that I wanted to know about! Hahaha

My question is really about the implementation of FunctionPointer. I want to know how/why it works. While I could use it to serve my purposes easily, I still want to learn how to do it myself (for my own enrichment). I've already had to rely on my self for many of my programming needs for this project (the mbed didn't have libraries for them). I would rather not be forced to depend on the existence of libraries in the future, this could cause big problems (and already has).

Is there anyone who might still help out with this? I'm not asking to share your code, I just want help learning how to solve this problem of calling unknown member function pointers. Thank you very much.

19 Oct 2011

Hi Thomas,

Have a look at the implementation in http://mbed.org/cookbook/FPointer

This shows you how to make your own!

Simon