Reputation: 707
I have a cpp function pointer:
using MyclassPtr = std::shared_ptr<Myclass>;
using UpdateCallback = std::function<void(const MyclassPtr&)>;
Now in java I have a interface:
public interface UpdateCallback {
void OnDataUpdate(MyClass data);
}
Requirement is that the cpp library should add itself as a listener to the java event. So, cpp will call setUpdateCallback(UpdateCallback) to add itself as a callback to the application events if occured.
JNI Method:
void MyCustomClassJNI::setUpdateCallback(const UpdateCallback & callback)
{.......}
How to get the function pointer from cpp and map it with the interface, so that when application class calls the interface methods, the cpp function callback is invoked??
Please help.
Upvotes: 0
Views: 306
Reputation: 12583
Your void OnDataUpdate(MyClass data);
is actually passing down the MyClass
data object to JNI.
Rather than passing down the callback interface pointer, probably, MyClass
data should be the meaningful object to your C++ layer.
From Java side
// Java code
public class MyCustomClassJNI implements UpdateCallback {
public native void notifyJni(MyClass data);
@Override
void OnDataUpdate(MyClass data) {
this.notifyJniPeer(data);
}
}
Then on C++ side, you should have a JNI method like below:
JNIEXPORT void JNICALL
Java_your_package_name_MyCustomClassJNI_notifyJni(JNIEnv *env, jobject myClassData) {
// read myClassData object.
}
Upvotes: 1