【发布时间】:2022-01-05 15:57:59
【问题描述】:
我需要一些方法将 unit8_t 数组转换为 char 数组或 char * 字符串,以在 Python 端创建 Job 对象并打印它在从 C++ 端获取对象后字段。 SWIG 是否支持自定义访问器函数(即 get/set)或“魔术”arg 转换?
我尝试了 @ignore 功能来跳过 identifier 和 name 成员的处理并添加 %inline 访问器功能,但no result.
在 Python 中:
job = someFuncInCppCode()
print("Identifier: " + job.identifier)
调用 Python 打印时出错:
Execution error: can only concatenate str (not "SwigPyObject") to str
C++ 头文件
struct Job
{
static const int MaxIdentifierLength = 20;
static const int MaxNameLength = 40;
uint8_t identifier[MaxIdentifierLength];
uint8_t name[MaxNameLength];
uint32_t status;
};
SWIG 自动生成的代码:
SWIGINTERN PyObject *_wrap_Job_identifier_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
Job *arg1 = (Job *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject *swig_obj[1] ;
uint8_t *result = 0 ;
if (!args) SWIG_fail;
swig_obj[0] = args;
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_Xrite__Device_Cpp__Topaz__Job, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Job_identifier_get" "', argument " "1"" of type '" "Job *""'");
}
arg1 = reinterpret_cast<Job * >(argp1);
result = (uint8_t *)(uint8_t *) ((arg1)->identifier);
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_unsigned_char, 0 | 0 );
return resultobj;
fail:
return NULL;
}
【问题讨论】:
标签: python string struct char swig