使用SWIG类型映射通过字符串转换类型
Convert types via string using SWIG typemaps
我正在使用swig为某些类生成包装器。一个类采用一个QUuid列表(std::list)(见下文)。有一个toString,它提供一个std::字符串,python也可以使用这个字符串实例化它的uuid。我如何生成一个类型映射(或者其他更好的方法),它使用上面描述的到python的转换,反之亦然。最好是独立于目标语言(至少我也需要ruby包装器)。
感谢
IRPIC当前位置命令.h:
class IRPICurrentPositionsCommand : public CommandBase
{
Q_OBJECT
COMMAND_IMPLEMENTATION_BASICS(IRPICurrentPositionsCommand, ir)
public:
std::list<QUuid> GetAxisIDs() const
{
return m_AxisIDs;
}
void SetAxisIDs(std::list<QUuid> arg)
{
m_AxisIDs = arg;
}
std::list<double> GetAxisPostions() const
{
return m_AxisPostions;
}
void SetAxisPostions(std::list<double> arg)
{
m_AxisPostions = arg;
}
private:
std::list<QUuid> m_AxisIDs;
std::list<double> m_AxisPostions;
protected:
/** @see ora::CommandBase::Serialize(QDataStream &stream) **/
virtual void Serialize(QDataStream &stream) const;
/** @see ora::CommandBase::Deserialize(QDataStream &stream) **/
virtual void Deserialize(QDataStream &stream);
};
这里是std::list<QUuid>
对象的SWIG/Python模板。我不熟悉Ruby,但希望这能让你对类型映射有一些了解。
// To use std::string
%include "std_string.i"
%typemap(out) std::list<QUuid>
{
PyObject* outList = PyList_New(0);
int error;
std::list<QUuid>::iterator it;
for ( it=$1.begin() ; it != $1.end(); it++ )
{
PyObject* pyQUuid = SWIG_NewPointerObj(new QUuid(*it), SWIGTYPE_p_QUuid, SWIG_POINTER_OWN );
error = PyList_Append(outList, pyQUuid);
Py_DECREF(pyQUuid);
if (error) SWIG_fail;
}
$result = outList;
}
%typemap(in) std::list<QUuid>
{
//$input is the PyObject
//$1 is the parameter
if (PyList_Check($input))
{
std::list<QUuid> listTemp;
for(int i = 0; i<PyList_Size($input); i++)
{
PyObject* pyListItem = PyList_GetItem($input, i);
QUuid* arg2 = (QUuid *) 0 ;
int res1 = 0;
void *argp1;
res1 = SWIG_ConvertPtr(pyListItem, &argp1, SWIGTYPE_p_QUuid, 0 | 0);
if (!SWIG_IsOK(res1))
{
PyErr_SetString(PyExc_TypeError,"List must only contain QUuid objects");
return NULL;
}
if (!argp1)
{
PyErr_SetString(PyExc_TypeError,"Invalid null reference for object QUuid");
return NULL;
}
arg2 = reinterpret_cast< QUuid * >(argp1);
listTemp.push_back(*arg2);
}
$1 = listTemp;
}
else
{
PyErr_SetString(PyExc_TypeError,"Wrong argument type, list expected");
return NULL;
}
}
相关文章:
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 类型字符串::长度()。我得到了"Unable to resolve identifier length"
- 如何掩盖对象数组的数据类型字符串到C 中的INT
- 解析参数值 - 字符串和链接值(类型字符串的链接值)与(特定类型的)变量
- C 中的枚举类型字符串类别
- 与基本面作斗争.特别是字符[],字符*和从数组中读取.也许我应该使用类型字符串
- 矢量类型 字符串,但当用整数初始化时
- 如何从const char *类型字符串中删除新线字符
- 在 c++ 中将 ostream 分配给数据类型字符串
- Linux 编译错误 - 无法将类型"字符串"转换为类型"字符串&&"
- LPCSTR 类型字符串出现乱码或显示错误
- C 表达式必须具有类型 - 字符串到const char*
- 将值分配给字符类型字符串
- 将组合框中的类型字符串与其项目进行比较
- 如何将返回类型字符串的数据复制到C++中
- 如何处理csv文件输入流,我需要将文件的每一行中的数据划分为6个不同的变量(类型字符串和int)
- 自定义C类型字符串类的问题
- 在c++中创建自定义C类型字符串类
- 在读取.csv时将数组类型字符串转换为double
- C++将类型字符串变量中的单词转换为小写