making an attach()

24 Sep 2010

hello,

I'm working on a project that requires the incorporation of InterruptIn in a class.  I need to wrap attach in a member function, and the user must pass in the address to the object and that of the object's member function to attach.

How do you do this?  What data type does the function prototype specify for its object address and function address arguments?  The InterruptIn class has a "T" pointer in the API.

I would like to do this:

void attach2(T* objectptr, void (T::*mptr)())
{
    do stuff
    call InterruptIn.attach
}
but I don't have access to the "T" datatype.

I would also like to use this to store the addresses for use by other members, but again, the "T" eludes me.

Thank you for the help.

24 Sep 2010

Hi Thomas,

In this, the T is actually a "template" parameter, which means the compiler can generate the code for any different type T you use.

Although I don't understand exactly the structure you are after, take a look at the following PwmIn class I wrote:

PwmIn is a class that includes an InterruptIn, and in the constructor it attaches member functions of PwmIn to the rise and fall interrupts. In this case, I therefore uses "this" as the object pointer, but it could be anything else.

I hope this helps. Please post back your solution if it does to show others, else ask a few more questions and someone should be able to solve it for you!

Simon

24 Sep 2010 . Edited: 29 Sep 2010

Thanks you for your replies, I will try and explain the structure better.

29 Sep 2010

After educating myself on templates, I'm going to try and explain myself better.  What I need is to store the user's member function pointer so that I can call that pointer in my code automatically at a later time.

In this code, I tried to make a template struct to store the necessary data

class myclass
{
    private:
        template struct storage
        {
            T* UserObject;
            void (T::*MemberFunction)();
        };

        storage userdata;
            //i know this is wrong, but it's for illustration

    public:
        myclass();

        template void attach2(T* objectptr, void (T::*mptr)(), float arg)
        {
            userdata.UserObject = objectptr;
            userdata.MemberFunction = mptr;
                //store pointers to call member function later

            doSomething(arg);
            doSomethingElse();

            InterruptIn.rise(objectptr, mptr);
        }

        void somefunction()
        {
            userdata.UserObject->userdata.MemberFunction();
        }
};
but this obviously won't work because userdata doesn't know what type to use.  The only time it can know this is if it is used to create an object within the attach2 block when the user passes the type in.  That won't work though, because the data is lost when the object goes out of scope when attach2 terminates.

How do the classes "Serial" and "InterruptIn" accomplish this?  I'm sure they need to store tptr and mptr somehow (unless I'm mistaken again).  Does anyone know how to do this?

Thank you to everyone who helps.