I'm using SWIG to wrap c++ in python and need to be using typemaps in order to keep my python script as simple as possible. As a first attempt, I'm just sending in 2 lists, converting them into vector's, adding the two vectors, then returning the result back into a new list.
My issue is that I've been finding the SWIG manual not very instructive, hard to follow, and not giving any solid, complete examples as to how I can write my own typemap.
My questions are:
- How would I go about ensuring my lists are properly converted to vector's, then back again?
- Are there any better tutorials/references out there for how to write typemaps, and what all the syntax/functions mean?
Here's my code:
add_array.h
#include <vector>
#include <functional>
std::vector<int> add_array(std::vector<int> src1, std::vector<int> src2);
add_array.i
%module add_array
%{
#include "add_array.h"
%}
%include std_vector.i
%template(vectorInt) std::vector<int>;
%include "add_array.h"
add_array.cpp
#include "add_array.h"
#include <cassert>
#include <cstring>
std::vector<int> add_array(std::vector<int> src1, std::vector<int> src2) {
assert(src1.size() == src2.size());
std::vector<int> dst;
dst.resize(src1.size());
for (size_t i = 0; i < src1.size(); i++) {
dst[i] = src1[i] + src2[i];
}
return dst;
}
Makefile
all:
rm -f *.so *.o *_wrap.* *.pyc *.gch add_array.py
swig -c++ -python add_array.i
g++ -fpic -c add_array_wrap.cxx add_array.h add_array.cpp -I/home/tools/anaconda3/pkgs/python-3.7.3-h0371630_0/include/python3.7m/
g++ -shared add_array_wrap.o add_array.o -o _add_array.so
array.py (this is the file I'm running)
import add_array
a = [1, 2, 3, 4, 5, 6]
b = [5, 6, 7, 8, 9, 10]
c = add_array.add_array(a, b)
print(c)
Output: (6, 8, 10, 12, 14, 16)
This is coming out as a tuple (I'd like it to be a list). It looks like I'm just lucky that it can convert the input lists to vectors (while not so lucky the other direction), but I'd really like to know how that's happening and how I can change that for future code if need be.
Thanks!
I don't know if there is a specific reason, but the included
std_vector.iconverts output vectors into tuples instead of lists. If you want a list, you'll need to write a custom typemap.Example (no error checking):
Output:
As far as tutorials, I've only ever read the SWIG documentation and the language-specific documentation for C language extensions. It's actually pretty good as documentation goes, but you can't just pick and choose what to read. Study the first dozen or so sections for the basics and then skip to the language-specific section (such as Python). There is an Examples directory under the SWIG installation as well.
References:
You'll have to look through SWIG sources to get any info about SWIG_Python_AppendOutput. Or just google for others examples.