1

I'm working with SWIG 2.0 and I'm creating a Java wrapper for an API, as part of this API it has a structure that contains a multidimentional array:

typedef struct mbuf
{
  data[2][31]
}

When it generates my proxy class it give me functions for getting the pointers to the array:

public void setData_buf_num1(int value) {
    apiJNI.MBUF_data_buf_num1_set(swigCPtr, this, value);
  }

  public int getData_buf_num1() {
    return apiJNI.MBUF_data_buf_num1_get(swigCPtr, this);
  }

I understand they are giving me back read only pointers that can be passed to other C functions and I've tried using carray.i to give me access but with no luck,

I could not get the cast to work because my functions return int as the pointer and carray functions require SWIGTYPE_p_int.

All I want to do is access the elements of the array from the proxy class properly.

2
  • Why not simply add a function singned int getData(signed int i, signed int) ? Commented Aug 23, 2012 at 13:37
  • The C you've shown is nowhere near legal. It would be helpful if you could show real C. (You're missing a semicolon in and after the struct, there's no type for the array, you have a typedef with no name). Commented Aug 23, 2012 at 13:37

2 Answers 2

1

If all you want to be able to do is read the data inside Java the simplest way is to hide the data member entirely and use %extend to add methods to read specific entries in the array. You can do this with SWIG as:

%module test

%ignore mbuf::data; 

%inline %{
struct mbuf
{
  int data[2][31];
};
%}

%extend mbuf {
  int getData(int i, int j) {
    return $self->data[i][j];
  }
}

You can add a setData in the same way if you want.

You could do more sophisticated things, for example use a pragma to provide some Java overloads that populate and set the entire array based on Java arrays. It would be possible to do this using carrays.i, but it's more cumbersome for a 2-D array than a 1-D array. It would also be possible to write some JNI, but being a 2-D array this again increases the complexity which makes the simple %extend solution more attractive.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks thats really great of you, i've been trying to do this for a couple of days. dont suppose you could give me an example if using the pragma to set the whole array just the basics i could take it from there.
0

FYI, assuming that your header file is named Test.h, here's how we could do it with JavaCPP:

import com.googlecode.javacpp.*;
import com.googlecode.javacpp.annotation.*;

@Platform(include="Test.h")
public class Test {
    public static class mbuf extends Pointer {
        static { Loader.load(); }
        public mbuf()          { allocate(); }
        public mbuf(int size)  { allocateArray(size); }
        public mbuf(Pointer p) { super(p); }
        private native void allocate();
        private native void allocateArray(int size);

        public native int data(int i, int j); 
        public native mbuf data(int i, int j, int k);
    }

    public static void main(String[] args) {
        mbuf m = new mbuf();
        m.data(1, 2, 42);
        System.out.println(m.data(1, 2));
    }
}

A bit more boilerplate, but at least its in Java :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.