SWIG: getters/setters for an array of struct don't return/expect an array of proxy class in Java

我的梦境 提交于 2019-12-22 00:25:46

问题


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.

来源:https://stackoverflow.com/questions/35203463/swig-getters-setters-for-an-array-of-struct-dont-return-expect-an-array-of-pro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!