mercredi 2 octobre 2019

How to make shared lib call a callback in application?

I have a C++ application that links against a C++ shared library that I'm also making (an API). I would like for a function (in the callback of the shared library) to call a callback function in the application and pass as a parameter a class.

In my application, I instantiate a class from the library as such.

int main() {
    // ...

    LibClass libProcess = std::make_shared<MyClass>();
    (*libProcess)(); 

    // ...
}

Here is the application callback.

void app_callback(std::shared_ptr<SomeClass> sc) { ... }

And the functor of LibClass appears as follows.

int LibClass::operator()() {   
    auto data = std::make_shared<std::vector<int>>();

    thread = std::thread(&LibClass::run_task, this, data);
    thread.join();                          
}

In this thread is where I would like to make a callback to a function in the application.

void LibClass::run_task(std::shared_ptr<std::vector<int>> data) {
    // ...

    while (true) {
        std::shared_ptr<SomeClass> sc = std::make_shared<SomeClass>();
        // ...

        // Here I would like to call the applications callback and pass a shared_ptr. 
        // but this is wrong
        app_callback(sc);

        sleep(1);
    }
}

Could someone help with how this might be done? I've seen other questions similar to this as I was using C callbacks, but when I started passing classes in the callback, things broke down.

Aucun commentaire:

Enregistrer un commentaire