vendredi 22 juillet 2016

C++ std::function bind callback between libraries without exposing method API

I have looked at quite a few links before asking this question & read quite a lot around std::function, std:bind & callbacks in C++. As I understand, the concept is mostly for event handlers to notify listeners after a certain event has happened. But, I am struggling to find the right implementation when it applies across library boundaries as I am a newbie into this style of programming.

Here is the situation or the design I need to implement: I have a library A which has a private function in a class which accepts certain data in & does some processing. There is a libraryB which provides the same data which the function in library A needs, but libraryB exposes a std::function to receive that data. Other library needs to bind its function to its callback to receive the data. So, I need to bind libraryA's 'func' to libraryB's std::function in my Application class.

Inside libraryA {

Class A {

private:
    AA objAA;
}

Class AA {

private:
    void func(int x) {
        //this is the function I want to tie to calback in library libB without exposing the method api
        //How can I expose a public api to return the address of this function so that the app can bind it to libraryB's callback ?
    }
}

}

Inside libraryB {

Class B {
public:
    void registerCallback(std::function<void (int)> callmePls) {
        m_callback = callmePls;
    }
private:
    typedef std::function<void (int)> theCallback;
    theCallback m_callback;
}

}


Inside my Application which uses both libraryA & libraryB {
//How can I bind/assign “func” in libraryA to the callback in libraryB
}

Can someone please suggest.. Thanks

Aucun commentaire:

Enregistrer un commentaire