swig,从列表到非标准向量
swig, from list to non-standard vector
我想设置typemap,这样我们就可以传递Python列表,而不是非标准向量。
在C++中,我有
template<typename T>
class mm_vector
{
void set_mm_vector(const mm_vector * copy);
}
我希望能够将Python列表作为参数传递,所以我在.I文件中指定:
// python list into vec_of_ints: from Python to C++
%typemap(in) AMM::mm_vector<int>*
{
int i;
if (!PyList_Check($input))
{
PyErr_SetString(PyExc_ValueError, "Expecting a list");
return NULL;
}
Py_ssize_t size = PyList_Size($input); //get size of the list
for (i = 0; i < size; i++)
{
PyObject *s = PyList_GetItem($input,i);
if (!PyInt_Check(s))
{
PyErr_SetString(PyExc_ValueError, "List items must be integers");
return NULL;
}
$1->push_back((int)PyInt_AS_LONG(s)); //put the value into the array
}
}
当我试着运行这些线路
l=[0,1]
v = mm.vec_of_ints()
v.set_mm_vector(l)
我有以下错误:
File "...", line 1295, in set_mm_vector
def set_mm_vector(self, *args): return _pyamt.vec_of_ints_set_mm_vector(self, *args)
ValueError: Expecting a list
如果有任何建议,我将不胜感激!!!
SWIG内置了对向量和模板的支持,因此您不必从头开始实现它。这里有一个简短的例子:
%module vec
// Include the built-in support for std::vector
%include <std_vector.i>
// Tell SWIG about the templates you will use.
%template() std::vector<int>;
// %inline adds the following code to the wrapper and exposes its interface via SWIG.
%inline %{
class Test {
std::vector<int> m_v;
public:
void set(const std::vector<int>& v) { m_v = v;}
std::vector<int> get() const {return m_v;}
};
%}
输出:
>>> import vec
>>> t=vec.Test()
>>> t.set([1,2,3])
>>> t.get()
(1, 2, 3)
相关文章:
- 标准::向量声明中使用的模板参数
- C4204:使用的非标准扩展:非常量聚合初始值设定项
- 在双重继承的情况下如何处理非标准构造函数
- CMake 不编译具有非标准扩展名的文件
- 定义具有非标准签名的自定义映射比较器
- 我想获取点的属性,它报告错误 C3867:"point::output_x":非标准语法;使用"&"创建指向成员的指针
- 非标准语法,使用 & 创建指向成员的指针
- 在Qt中链接具有非标准名称的lib文件
- 使用指针向量和非指针向量有什么区别
- 非标准语法使用 '&' 创建指向成员 C++ 的指针
- 在使用标准向量函数时引发'std::bad_alloc'实例后调用的终止
- 在标准向量中将元素分配给 Eigen::Vector2d 会引发错误
- WINAPI EnumWindowsProc:非标准语法;使用 & 创建指向成员的点
- Vulkan-hpp 是reinterpret_casting非标准布局类到另一个类.这合法吗?
- 'cv::Mat::type':非标准语法;使用 '&' 创建指向成员的指针
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 在priority_queue上进行interpret_cast,以遍历非标准的底层向量
- swig,从列表到非标准向量
- 在unique_ptrs向量中搜索时出现非标准扩展警告
- 是否有一个向量可以处理非标准位长度的整数