SWIG: getters/setters for an array of struct don&#

2020-07-27 05:37发布

问题:

I try to generate an adequate interface for Java of a C++ interface with help of SWIG. In general it works quite nice, but now I have a problem with an (bound) array of structs.

I have following two structures:

typedef struct
{
    char* street;
    char* city;
} Address;

typedef struct
{
    char* firstname;
    char* lastname;
    Address addresses[4];
} Person;

The getters and setters for addresses field of Person struct is:

public Address getAddresses() { /*...*/ }
public void setAddresses(Address value) { /*...*/ }

With these accessors I have no chance to get or set the second, third or fourth address in the array. What I am doing wrong? How can I access the array items by index?

The best solution would be Java generated code like this:

public class Person {
    /* here: ctor, cPtr, finalize method, ... */
    /* here: getters and setters for firstname and lastname */ 

    private final java.util.List<Address> addresses = new java.util.AbstractList<Address>() {
        @Override
        public int size() { return 4; }

        @Override
        public Address set(int index, Address address) {
            Address result = get(i);

            /* call adequate MyModuleJNI method 
               for setting the address at specified index */

            return result;
        }

        @Override
        public Address get(int index) {
            return /* call adequate MyModuleJNI 
                      for getting the address at specified index */;
        }
    }

    public List<Address> getAddresses() {
        return addresses;
    }
}

How can I achieve this such a result? (Which typemaps are involved? How can I get the array size for size method, ...)

It would be nice if someone could guide me to the right direction.

标签: java c++ swig