在没有 std_vector.i 的情况下实现对 std::vector 的支持 [英] Implement support for std::vector without std_vector.i

查看:44
本文介绍了在没有 std_vector.i 的情况下实现对 std::vector 的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经问了 2 个关于我的问题的问题,尽管答复确实很有帮助,但我无法找到解决问题的最佳解决方案.现在让我解释一下我的主要目标/问题.

由于某些限制,我无法在 swig 界面中使用 std_vector.i,但我需要使用 C++ 对象(字符串向量的向量)vector> 在 Python 中.我实现了一个解决方案,我将整个 vector> 到 Python列表列表",其中我正在执行以下转换:使用 PyString_FromString() 将每个 C++ 字符串转换为 Python 字符串每个 vector 到 Python 列表 l1、l2、l3、l4...最后 vector> 到包含 l1、l2、l3、l4.. 作为元素的 Python 列表.

虽然,上述解决方案工作正常,我能够在 Python 中访问字符串值,但这个解决方案对我来说并不理想.

我更喜欢一个类(不使用 std_vector.i),它的对象我可以作为函数参数传递以填充值,并且在从函数返回后,我应该能够使用 ob[0 访问这些值][0] 等.通过这种方式,对于在 __getitem__ 中访问的每个值,我将只需要进行一次转换(C++ 字符串到 python 字符串).但我不知道如何定义一个表示 vector 的类.> 在 Python 中不使用 %template.

解决方案

我已经为 std::vector<std::vector<std::string > 整理了一个最小包装器的示例.> 无需包含任何额外的 SWIG 文件(例如 std_vector.i 和 std_string.i).

我还整理了一个小头文件来测试我的实现:

#include #include <字符串>#include <算法>#include <迭代器>#include 内联 void print_vec(const std::vector<std::string>& v) {std::copy(v.begin(),v.end(), std::ostream_iterator(std::cout, "\n"));}内联 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测试1;静态 std::vector测试2;静态 std::vector>退;test1.push_back("你好");test2.push_back("世界");test2.push_back("另一个");ret.push_back(test1);ret.push_back(test2);返回 ret;}

这是我能想到的最小的实现,可以有效地练习生成的界面.

我编写的 SWIG 接口提供了 std::vector 的骨架定义 - 足以说服 SWIG 实际包装这个东西.我们还针对我们关心的两种情况进行了扩展,以提供 __getitem__ 的实现,这是您希望能够实现的 obj[x][y] 语法的最低要求使用.

%module 测试%{#include "test.hh"%}命名空间标准{模板 类向量{};}%extend std::vector>{std::vector__getitem__(unsigned i) throw(std::out_of_range) {返回 $self->at(i);}}%extend std::vector{const char * __getitem__(unsigned i) throw(std::out_of_range) {返回 $self->at(i).c_str();}}%template (VecString) std::vector;%template (VecVecString) std::vector>;%include "test.hh"

c_str() 有一个技巧可以避免包含 std_string.i.这个接口允许我在 Python 中做这样的事情:

Python 2.7.1+ (r271:86832, 2011 年 4 月 11 日,18:05:24)[GCC 4.5.2] 在 linux2 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>导入测试>>>t=Test.make()>>>打印 t[0][0]你好>>>

它目前没有在 中引发正确类型的 Python 异常__getitem__.您可以使用 %include "exception.i"%exception 并围绕 $action 编写自己的 try/catch.

您可能还想提供 __setitem__ 的类似实现以使其有用.

这可能不比 std_vector.i 快,或者你的自制类型映射直接转换为 Python 列表列表.总的来说,虽然我不认为这样做是个好主意——使用现有的 std_vector.i 实现而不是重新发明轮子似乎更合乎逻辑.

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:

#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.

这篇关于在没有 std_vector.i 的情况下实现对 std::vector 的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆