1

I have a header file with "interface" structures that contain solely function pointers. I want SWIG to (automatically?) generate a matching single method Java interface for each of these function pointers. I want to set & get objects that implement these interfaces on the wrapped C struct (=Java proxy class). The setter should have additional logic that links the Java callback to the specific instance it is set on (ie through a map).

On the C/JNI side, a C function implementation should be stored in the struct's function pointer that callbacks into a static Java method of the proxy java class. This static method's implementation can then use it's given arguments and use the link between the callback & the object instance, to invoke the correct java callback.

This question got me started: How should I write the .i file to wrap callbacks in Java or C#

However it demonstrates how to do it with globally declared function pointers & in my case I can/don't want to store the jobject+JNIEnv globally on the C side.

What I got so far is a (miserable) macro for a callback C function with 3 arguments:

%define CALLBACK_3(jni_cls, jni_mtd_name, jni_mtd_signature, c_mtd_name, c_arg0_type, c_arg_type1, c_arg_type2)

void c_mtd_name ( c_arg0_type c_arg0, c_arg1_type c_arg1, c_arg2_type c_arg2) {
    JNIEnv *env;
    GET_ENV(env);

    jclass j_callback_cls =  JCALL1(FindClass, env, jni_cls);
    jmethodID j_callback_mtd = JCALL3(GetStaticMethodID,env,j_callback_cls,#jni_mtd_name,#jni_mtd_signature);

    //?
    $typemap(jni,c_arg0_type) j_arg0 = 0;
    $typemap(jni,c_arg1_type) j_arg1 = 0;
    $typemap(jni,c_arg2_type) j_arg2 = 0;

    $typemap(out,1=c_arg0,result=j_arg0);
    $typemap(out,1=c_arg1,result=j_arg1);
    $typemap(out,1=c_arg2,result=j_arg2);

    //?
    JCALL3(CallStaticVoidMethod,env,j_callback_cls,j_callback_mtd,j_arg0,j_arg1,j_arg2);
}

%enddef

So before struggling on, I would like to know if I'm taking the right approach here and if so, how do I tell inside my macro to convert a given C variable into a corresponding C JNI type?

Community
  • 1
  • 1
Zubzub
  • 782
  • 7
  • 18

0 Answers0