Implement support for std::vector without std_vector.i

别等时光非礼了梦想. 提交于 2019-12-08 08:23:44

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:

#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <iostream>

inline void print_vec(const std::vector<std::string>& v) {
  std::copy(v.begin(),v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}

inline void print_vec_vec(const std::vector<std::vector<std::string> >& v) {
  std::for_each(v.begin(),v.end(),print_vec);
}

std::vector<std::vector<std::string> > make() {
  static std::vector<std::string> test1;
  static std::vector<std::string> test2;

  static std::vector<std::vector<std::string> > ret;
  test1.push_back("hello");
  test2.push_back("world");
  test2.push_back("another");
  ret.push_back(test1);
  ret.push_back(test2);
  return ret;
}

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 the obj[x][y] syntax you want to be able to use.

%module Test

%{
#include "test.hh"
%}

namespace std {
template <typename T>
class vector {
};
}

%extend std::vector<std::vector<std::string> > {
  std::vector<std::string> __getitem__(unsigned i) throw(std::out_of_range) {
    return $self->at(i);
  }
}

%extend std::vector<std::string> {
  const char * __getitem__(unsigned i) throw(std::out_of_range) {
    return $self->at(i).c_str();
  }
}

%template (VecString) std::vector<std::string>;
%template (VecVecString) std::vector<std::vector<std::string> >;

%include "test.hh"

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:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Test
>>> t=Test.make()
>>> print t[0][0]
hello
>>>

It currently doesn't raise the correct type of Python exception in __getitem__. You can do that either with %include "exception.i" or with %exception and writing your own try/catch around $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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!