Okay, I’ve already asked 2 questions about my problem and despite the fact that the replies were really helpful, I am not able to find an optimal solution for my problem. Let me explain my main objective/problem now.
Due to some constraints I can’t use std_vector.i in my swig interface, but I need to use a C++ object of (vector of vectors of string)vector<vector<string>> in Python. I implemented a solution where I am converting whole vector<vector<string> > to Python “List of Lists” wherein I am doing the following conversions:
each C++ string to Python String using PyString_FromString()
each vector<string> to Python Lists l1, l2, l3, l4…
and finally vector<vector<string> > to a Python List containing l1, l2, l3, l4.. as elements.
Although, the above solution works fine and I am able to access the string values in Python but this solution doesn’t look optimal to me.
I would prefer a class (without using std_vector.i) whose object I can pass as a function argument to be populated with values and after returning from the function I should be able to access the values using ob[0][0] etc. In this way I will have to make only one conversion (C++ string to python string) ,for each value accessed, in __getitem__. But I don’t know how to define a class representing vector<vector<string> > in Python without using %template.
I’ve put together an example of a minimal wrapper for
std::vector<std::vector<std::string > >which works without including any extra SWIG files (e.g. std_vector.i and std_string.i).I also put together a small header file to test my implementation with:
It’s the smallest implementation I could think of that usefully exercises the generated interface.
The SWIG interface I wrote provides a skeleton definition of
std::vector– just enough to persuade SWIG to actually wrap the thing. We also extend it for the two cases we care about to provide an implementation of__getitem__, the minimum requirement for theobj[x][y]syntax you want to be able to use.There’s a trick there with
c_str()to avoid including std_string.i. This interface allows me to do things like this in Python:It currently doesn’t raise the correct type of Python exception in
__getitem__. You can do that either with%include "exception.i"or with%exceptionand writing your owntry/catcharound$action.You’ll probably also want to provide a similar implementation of
__setitem__to make this useful.This is probably no faster than std_vector.i, or your home brew typemap that converts to Python list of list directly. In general though I don’t think doing it like this is a good idea — using the existing std_vector.i implementation instead of reinventing the wheel seems far more logical.