Como usar uma lista Python para atribuir um std :: vector em C ++ usando SWIG?
Eu tenho uma classe C ++ simples que contém um membro std :: vector e uma função de membro que aceita um std :: vector como um argumento que estou envolvendo com SWIG e chamando de Python. O código de exemplo está abaixo.
Depois de compilá-lo, vou para o Python e faço:
import test
t = test.Test()
a = [1, 2, 3]
b = t.times2(a) # works fine
t.data = a # fails!
A mensagem de erro que recebo é:
TypeError: in method 'Test_data_set', argument 2 of type 'std::vector< double,std::allocator< double > > *'
Eu sei que posso apenas fazer:
t.data = test.VectorDouble([1,2,3])
Mas eu gostaria de saber como usar apenas uma lista Python diretamente na atribuição, ou pelo menos entender por que ela não funciona.
Aqui está o código de exemplo.
test.i:
%module test
%include "std_vector.i"
namespace std {
%template(VectorDouble) vector<double>;
};
%{
#include "test.hh"
%}
%include "test.hh"
test.hh:
#include <vector>
class Test {
public:
std::vector<double> data;
std::vector<double> times2(std::vector<double>);
};
test.cc:
#include "test.hh"
std::vector<double>
Test::times2(
std::vector<double> a)
{
for(int i = 0; i < a.size(); ++i) {
a[i] *= 2.0;
}
return a;
}
makefile:
_test.so: test.cc test.hh test.i
swig -python -c++ test.i
g++ -fpic -shared -o _test.so test.cc test_wrap.cxx -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -L/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/ -lpython2.7