在没有 std_vector.i 的情况下实现对 std::vector 的支持

发布于 2024-12-28 18:59:17 字数 836 浏览 1 评论 0 原文

好吧,我已经就我的问题提出了 2 个问题,尽管事实上这些回复确实很有帮助,但我无法找到解决我问题的最佳解决方案。现在让我解释一下我的主要目标/问题。

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

虽然上述解决方案工作正常,并且我能够访问 Python 中的字符串值,但该解决方案对我来说看起来并不是最佳选择。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在巴黎塔顶看东京樱花 2025-01-04 18:59:17

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

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

#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;
}

这是我能想到的最小的实现,可以有效地练习生成的接口。

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

%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"

c_str() 有一个技巧可以避免包含 std_string.i。这个接口允许我在 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
>>>

它目前不会在 __getitem__。您可以使用 %include "exception.i"%Exception 并在 $action 周围编写您自己的 try/catch

您可能还需要提供类似的 __setitem__ 实现来使其有用。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文