说实话,我在这个网站上阅读了很多关于“struct”主题的文章,但我需要你的帮助。我有C风格的结构体。
struct Time
{
uint16_t year; // year with four digits like 2016
uint8_t month; // 1 .. 12
uint8_t day; // 1 .. 31
uint8_t hour; // 0 .. 23, 24 hour representation
uint8_t minute; // 0 .. 59
uint8_t second; // 0 .. 59
};
使用类和成员函数,并将其实现在DLL中。
class DeviceInterface {
virtual uint32_t getTime(Time& time) = 0;
};
uint32_t值是状态码。
以下是自动生成的SWIG C++代码:
SWIGINTERN PyObject *_wrap_getTime(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
DeviceInterface *arg1 = (DeviceInterface *) 0 ;
Time *arg2 = 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
void *argp2 = 0 ;
int res2 = 0 ;
PyObject *swig_obj[2] ;
uint32_t result;
if (!SWIG_Python_UnpackTuple(args, "getTime", 2, 2, swig_obj)) SWIG_fail;
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_DeviceInterface, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "getTime" "', argument " "1"" of type '" "DeviceInterface *""'");
}
arg1 = reinterpret_cast< DeviceInterface * >(argp1);
res2 = SWIG_ConvertPtr(swig_obj[1], &argp2, SWIGTYPE_p_Time, 0 );
if (!SWIG_IsOK(res2)) {
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "getTime" "', argument " "2"" of type '" "Time &""'");
}
if (!argp2) {
SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "getTime" "', argument " "2"" of type '" "Time &""'");
}
arg2 = reinterpret_cast< Time * >(argp2);
result = (uint32_t)(arg1)->getTime(*arg2);
resultobj = SWIG_From_unsigned_SS_int(static_cast< unsigned int >(result));
return resultobj;
fail:
return NULL;
}
从上面的代码中,我看不到时间值,即arg2变量,是如何返回的。那么我需要在SWIG接口文件中编写什么来获取状态码和时间?
Py_BuildValue
打包的元组而不是由SWIG_Python_AppendOutput
创建的列表吗? - devaskimSWIG_Python_AppendOutput
是智能的。如果正确处理void返回值和多个类型映射的实例,就像您之前提出的问题有两个输出参数一样。如果您考虑构建时间元组而不是代理时间对象,请注意Time代理可以传递给其他需要Time对象的函数,而元组必须再次转换为Time对象。 - Mark Tolonenstatus_code, time = d.getTime()
,而不是返回状态码列表和时间对象。 - devaskim