[Swig-user] An array inside a structure SWIG conversion in java

51 views
Skip to first unread message

kartheek muthyala

unread,
Mar 18, 2012, 2:26:59 AM3/18/12
to swig...@lists.sourceforge.net
Hi all,
I am using SWIG for gluing C++ to Java. I have a c++ structure like
struct MemInfo {
    int count;
    Member *mem;
}

where Member is also a structure.. You can see that structure MemInfo
captures a list of members and the count signifies the number of members.
Now, I have a function like this

MemInfo * getMembersInfo () {
  MemInfo* members;
  /*.....*/
 returns members;
}

When I expose the file containing this structure and the function in the
SWIG Interface file, it maps MemInfo* to a class MemInfo inside which it has
4 functions which are setters and getters for mem and count.

I need to get an array of Member inside the MemInfo class such that I can
access the list in java.
Is there a way in which we can do that. I know that typemaps can be used for
this. If someone out there good at typemaps can you please help me solve
this problem.

Thanks,
Kartheek

kartheek muthyala

unread,
Mar 18, 2012, 6:30:43 AM3/18/12
to swig...@lists.sourceforge.net

Thanks,
Kartheek.

}

--
View this message in context: http://old.nabble.com/An-array-inside-a-structure-SWIG-conversion-in-java-tp33525235p33525235.html
Sent from the swig-user mailing list archive at Nabble.com.


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Swig-user mailing list
Swig...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Bob Hood

unread,
Mar 18, 2012, 9:56:10 AM3/18/12
to swig...@lists.sourceforge.net
On 3/18/2012 4:30 AM, kartheek muthyala wrote:

Hi all,
I am using SWIG for gluing C++ to Java. I have a c++ structure like
struct MemInfo {
     int count;
     Member *mem; 
}

where Member is also a structure.. You can see that structure MemInfo
captures a list of members and the count signifies the number of members.
Now, I have a function like this

MemInfo * getMembersInfo () {
   MemInfo* members; 
   /*.....*/
  returns members;
}

When I expose the file containing this structure and the function in the
SWIG Interface file, it maps MemInfo* to a class MemInfo inside which it has
4 functions which are setters and getters for mem and count. 

I need to get an array of Member inside the MemInfo class such that I can
access the list in java.
Is there a way in which we can do that. I know that typemaps can be used for
this. If someone out there good at typemaps can you please help me solve
this problem.

You're going to need a typemap that converts "MemInfo *" into a Java-specific data type that contains the information.  I don't know Java, nor SWIG's Java-specific interfaces.  I can only show you an example in Python, and you'll need to take it from there.

Since you don't specify (if it's opaque to me, it's going to opaque to SWIG), I'll assume for the sake of the example that "Member *" is an array of structures that look like:

    struct Member
    {
        int age;
        char* first_name;
        char* last_name;
    };

So, the typemaps to exchange this with Python would look something like:

    %typemap(out) MemInfo * {
        // this is 'out' to Python from C++
        if(!$1)
        {
            Py_INCREF(Py_None);
            $result = Py_None;
        }
        else
        {
            // we don't need to encode 'count' to Python, as that can
            // simply be inferred by the size of the returned sequence

            PyObject* py_tuple = PyTuple_New($1->count);
            for(int i = 0;i < $1->count;++i)
            {
                PyObject* py_inner_tuple = PyTuple_New(3);
                PyTuple_SetItem(py_inner_typle, 0, PyInt_FromLong($1->mem[i].age));
                PyTuple_SetItem(py_inner_typle, 1, PyString_FromString($1->mem[i].first_name));
                PyTuple_SetItem(py_inner_typle, 2, PyString_FromString($1->mem[i].last_name));
                PyTuple_SetItem(py_tuple, i, py_inner_tuple);
            }
            $result = py_tuple;
        }
    }

    %typemap(in) MemInfo * {
        // this is 'in' to C++ from Python

        $1 = NULL;

        // the data can be in various formats, but I'll only deal with
        // sequences in this example

        if($input != Py_None)
        {
            if(!PySequence_Check($input))
            {
                PyErr_SetString(PyExc_TypeError,"Expected sequence.");
            }
            else
            {
                // extract data from the sequence (handling sub-sequences)
                // and construct a MemInfo structure to hold them...

                $1 = new MemInfo;
                $1->count = PySequence_Length($input);
                $1->mem = new Member[$1->count];
                for(int i = 0;i < $1->count; ++i)
                {
                    // populate $1->mem[i] from the data in the list

                    PyObject* elem = PySequence_GetItem($input, i);
                    if(!PySequence_Check(elem))
                    {
                        PyErr_SetString(PyExc_TypeError,"Expected sequence.");
                    }
                    else if(PySequence_Length(elem) != 3)
                    {
                        PyErr_SetString(PyExc_TypeError,"Expected 3 elements.");
                    }
                    else
                    {
                        // and so on...
                    }
                }
            }
        }
    }

This is untested, mind you, and of course you'd want to place code that you'd use more than once into its own function and call it instead of in-lining it.  However, should give you an idea of how you can exchanged your data with the target language.


Render me gone,                       |||
Bob                                 ^(===)^
---------------------------------oOO--(_)--OOo---------------------------------
    I'm not so good with advice...can I interest you in a sarcastic comment?

kartheek muthyala

unread,
Mar 18, 2012, 1:16:27 PM3/18/12
to Bob Hood, swig...@lists.sourceforge.net
Hi Bob,
Thanks you very much for the prompt reply.

I understood your logic. But, only thing that concerns me is this logic is only available in python. I am trying to find  python equivalent java swig api's. Because, you have Pytuple_new which seems to be a generic type, but I couldn't find an equivalent representation in java. All, I can find was jobjectArray which takes class type as an argument.


Hey SWIG java gurus, it would be good if you can throw me light on this issue.

Thanks,
Kartheek.

Reply all
Reply to author
Forward
0 new messages