Hello,
I am new to JavaCPP and trying to do a do a callback from C++ to Java.
I would like to implement the callback function in Java, and I to set up everything from the Java side, while the callback is executed from the C++ side.
Here's how my Java calling code could look like:
Caller caller = new Caller();
Callback callback = new JavaCallback();
caller.setCallback(callback);
caller.call(); // calls C++ which does the callback in Java
where JavaCallback is a Java class with my implementation of the callback:
class JavaCallback extends Callback {
@Override
public void run() {
System.out.println("this is my java-side callback");
}
}
And the C++ side that executes the callback would look like this:
// C++
class Callback {
public:
virtual void run() { ... }
};
class Caller {
private:
Callback *_callback;
public:
Caller(): _callback(0) {}
void setCallback(Callback *cb) { _callback = cb; }
void call() { _callback->run(); }
};
The above code is taken from this
SWIG callback example.
Your help is very much appreciated!