mercredi 28 octobre 2015

Send function as a parameter to another class

I didn't really know how to search for this, so I'm going to ask the question here. I know how to pass a function as a parameter, but this time, the situation is a bit different and I'm a bit lost.

I will simplify the classes for readability reasons, but let me know if I miss something that makes it confusing.

I have a class that calls a function inside another:

class Class1 : public ofBaseApp, JSMethodHandler {
public:
    void setup() {
        class2.bindMethod(&Class1::thatFunction);
    }

    void thatFunction(Signature signature) {}

    Class2 class2;
}

Then Class2:

#include "js_delegate.h"
class Class2 : JSMethodHandler {
public:
    void bindMethod(void (ofBaseApp::*func)(Signature signature)) {
        JSDelegate(this, func);
    } 
}

and the definition of JSDelegate:

template < class X, class Y >
FastDelegate2(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) ) {
    m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }

If I want to do it this way, I get an error in Class1 saying that ofApp::* is incompatible with ofBaseApp::*.

I want to make bindMethod generic so I can pass functions from every class that inherits from ofBaseApp. Can this be done with the current configuration, or can you suggest another way of approaching this?

Cheers

Aucun commentaire:

Enregistrer un commentaire