Question

I want to access a template List of C++ program from a Perl script and use those values.

Example code:

typedef list < Struct1  * > sturct1_list;

struct Struct2
{
    int i;
    struct1_list List1;
}

struct Struct1
{
    int j;
}

I used one swig generated api and did the following:

$myList = Struct2_struct1List_get 
print "Reference type: " . ref($myList) ;

now this prints as:

Reference type:  \_p\_std\_\_listTutils\_\_Struct1\_p\_t 

how to get the values from the structure using this?

Update from duplicate question:

in interface file i put

%template(ListStruct1) std::list< Struct1 * >;

after i generate the ".pm" file. I checked the APIs available this list.

I found

ListStuct1_size
ListStuct1_empty
ListStuct1_clear
ListStuct1_push.

I was able to use those elements. But i dont know how to access individual elements of the list using these API? or am I missing something in interface file?

UPDATED:

Is typemap possible to return the list as array here??

Was it helpful?

Solution

First of all, general info

This tutorial shows how to do the wrapper for templates.

The same tutorial shows how to use the module from Perl, but the perl example doesn't touch templates.

This SO article shows how to do that with a Vector

Here's a general SWIG STL documentation that seems to mention std_list.i interface.

Second, regarding lists

  1. You can not "access" C++ list like a Perl array, by a subscript. If you wanted that, you must use a Vector as underlying type.

  2. As an alternate, create a class extending List, give it a new method which returns an element by an index, and expose that method in an interface.

  3. If you wish to access the list by finding an element, like in C++, you need to write a List interface that exposes find() method - the default one does not from reading the source code.

OTHER TIPS

In your interface, try:

%include "std_list.i"

%template(ListStruct1) std::list< Struct1 * >;

The std library is kinda funny, there's no actual binary object called list that swig can just wrap, it's all templates - so swig needs some extra help to figure out what's going on.

That should add insert, remove, and a bunch of other list specific functions to the wrapper.

If the above doesn't work, try adding:

%define SWIG_EXPORT_ITERATOR_METHODS 

UPDATE: Of course, I neglected to mention (or even realize) that this works great for python, java, and a few others, but is totally broken in perl...

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