jeudi 21 décembre 2017

Trouble mapping to vector

I'm trying to extend a python wrapper for a third-party library. I'm new to swig and fairly rusty at c++ so any help is appreciated.

The swig interface file has code as follows:

%include "std_list.i"
%include "std_map.i"
%include "std_vector.i"
%include "std_string.i"
%include "std_pair.i"
%include "std_shared_ptr.i"

%shared_ptr(std::vector<std::string>)

%define VECTORTEMPLATE_WRAP(vectorname, T)
%feature("ignore") vector<T>::append;
// a bunch more %feature("ignore")
%template(vector ## vectorname) vector<T>;
%enddef

VECTORTEMPLATE_WRAP(String, std::string)

Which I think the last part is just a more general way of saying

%template(vectorString) vector<std::string>

Then in the header file this function is declared:

class OBCONV OBConversion
{
int FullConvert(std::vector<std::string>& FileList, std::string&
                OutputFileName, std::vector<std::string>& OutputFileList);
}

However when I build the whole project and call this function in python:

conv = ob.OBConversion()
conv.FullConvert([],'',[])

I get the following error:

TypeError: in method 'OBConversion_FullConvert', argument 2 of type 'std::vector< std::string,std::allocator< std::string > > &'

Which it seems to me is skipping the %template directive completely, probably due to the "allocator" in the type parameter.

Indeed if I look in the swig-generated c++ code for this function I have

SWIGINTERN PyObject *_wrap_OBConversion_FullConvert(PyObject
     *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0;
OpenBabel::OBConversion *arg1 = (OpenBabel::OBConversion *) 0 ;
std::vector< std::string,std::allocator< std::string > > *arg2 = 0 ;
std::string *arg3 = 0 ;
std::vector< std::string,std::allocator< std::string > > *arg4 = 0 ;
// ...
}

I'm building using c++11, which I don't think the project was originally developed for, so that may have something to do with it.

What is the allocator and how do I get swig to map the vector to a python list?

Aucun commentaire:

Enregistrer un commentaire