SWIG Pointers and Java Arrays

The SWIG documentation explains how various types of input are in C, for example:

void spam1(Foo *x);      // Pass by pointer
void spam2(Foo &x);      // Pass by reference
void spam3(Foo x);       // Pass by value
void spam4(Foo x[]);     // Array of objects

... everyone will take one type of argument in Java, for example:

Foo f = new Foo();  // Create a Foo
example.spam1(f);   // Ok. Pointer
example.spam2(f);   // Ok. Reference
example.spam3(f);   // Ok. Value.
example.spam4(f);   // Ok. Array (1 element)

Similarly, for return types in C:

Foo *spam5();
Foo &spam6();
Foo  spam7();

... all three functions will return a pointer to some Foo object that will be assigned to the Java object variable, the last of which requires the allocation of a value type that will take care of Java garbage collection after release.

But suppose spam5 () returns a pointer to an array. In Java, I have to use array semantics to access individual elements, but I don't think I can just do this:

Foo foo[] = spam5();

I don’t even think that the compiler will accept the cast to (Foo []), since this works in SWIG?

+3
source share
3

. , .

, SWIG , , , , Java. , (, ) - typemaps .

-, C out, , Java. , carrays.i, C Java.

+4

JNI:

, xyz

public class demo{
  public native vecteur[] returnArray();

  ....
}

cxx swig wrap add func, java vect

JNIEXPORT jobjectArray JNICALL 
               Java_demo_returnArray
  (JNIEnv *env, jobject jobj){

    jobjectArray ret;
    int i;
    jclass objClass;
    jmethodID mid;
    jobject myobj;
    jmethodID setX;
   objClass = env->FindClass("vect");
   if (!objClass)
   {
      printf("class not found\n");
      exit(0);
   }
    ret= (jobjectArray)env->NewObjectArray(5, // change with the size of your array or a variable 
         objClass,
         env->NewStringUTF(""));//FIXME
    // call javap -s myclass to know the names
    mid=env->GetMethodID( objClass, "<init>", "()V"); // looking for the vect class constructor
    if (!mid)
      {
        printf("vect() not found\n");
        exit(0);
      }


    for(i=0;i<5;i++) {
         myobj=env->NewObject( objClass, mid); // myobj = new vect()

         // get vect::setX method
         setX=env->GetMethodID( objClass, "setX", "(F)V"); // looking for vect::setX method
         if(!setX)
           {
             printf("method vect::setX not found\n");
             exit(0);
           }
         // call setX method with param i
         env->CallVoidMethod(myobj, setX,(float)i); // change i with your array value
         env->SetObjectArrayElement(
                                   ret,i,myobj);
        }
    return(ret);
  }

, ,

// main.java
public class main {
  static {
    System.loadLibrary("myclass");
  }

  public static void main(String argv[]) {
      demo l = new demo();
      vecteur f[] = l.returnArray();    
      System.out.println("array size : "+f.length);
      for (int i = 0;i < f.length;i++)
          System.out.println(f[i].getX());

  }
}
0

In java, an array is an object, so if spam5()return Object, then the compiler will let you pass this to the Foo array. This is valid Java:

    class Bar {
        static class Foo {}
        Foo[] foo = {new Foo(), new Foo()};
        Object o = foo;
        // ...
        Foo[] bar = (Foo[])o;
    }
-1
source

Source: https://habr.com/ru/post/1719449/


All Articles