Question

I want to create a function which will return a static array and wrap this function with SWIG to be able to use it in Java.

Say I created my own class and I have a C++ function which looks like

myClass* myFunction()
{
   // stuff here
}

And I want to wrap it with SWIG so as to use it in Java in this way :

myClass[] array = myFunction();

I've done some reseach but couldn't find something valuable (many answers say how to create function whose arguments are arrays but none say about arrays being returned).

Thank you for taking the time to answer me.

Was it helpful?

Solution

You have made me recall nightmares from my previous job while researching the answer to your question :).

I chose to avoid this problem instead of solving it. I'd create a dead simple C++ class like

class ArrayAdapter
{
public:
    int size();
    MyClass *getItem(int index);
};

and then

ArrayAdapter myFunction();

If you need to pass it into a Java function that expects an array or Collection, write the wrapper code in Java.

I don't claim this is the best solution, but sometimes it's nicer to write something simple and debuggable rather than relying on SWIG typemap magic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top