好吧,我已经就我的问题提出了 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
.
发布评论
评论(1)
我已经为
std::vector 整理了一个最小包装器的示例。 >
无需包含任何额外的 SWIG 文件(例如 std_vector.i 和 std_string.i)即可工作。我还整理了一个小头文件来测试我的实现:
这是我能想到的最小的实现,可以有效地练习生成的接口。
我编写的 SWIG 接口提供了 std::vector 的骨架定义 - 足以说服 SWIG 真正包装该东西。我们还针对我们关心的两种情况进行了扩展,以提供 __getitem__ 的实现,这是您希望能够实现的 obj[x][y] 语法的最低要求使用。
c_str()
有一个技巧可以避免包含 std_string.i。这个接口允许我在 Python 中执行类似的操作:它目前不会在
__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:
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%exception
and writing your owntry
/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.