我想从Swig 2-> 3升级,现在我遇到了类型图定义中的问题。有一件事是,到目前为止我对swig不是很熟悉,只是遇到了用typemaps_string
定义内容的旧遗留代码。
代码似乎在QString(C ++)和Perl之间进行了一些类型映射。
swig> = 3.0.0的命令/宏正在抱怨消息“错误:宏'%typemaps_string'需要11个参数”是
%typemaps_string(%checkcode(UNISTRING), %checkcode(UNICHAR),
QChar, QChar,
SWIG_AsQtChPtrAndSize,
SWIG_FromQtChPtrAndSize,
Qt_init,
"<qt_string.h>", (-2147483647 - 1), 2147483647);
不幸的是我找不到有关%typemaps_string宏的任何信息。此更改完全由Swig 2.0.12到Swig 3.0.0的更新发生。在更改日志中,我也无法获得任何有用的信息(但这可能是我对Swig缺乏了解的结果)。
此宏用于什么信息以及自Swig3.x以来发生了哪些变化?
以下是发生这种情况的文件的完整代码:
%include <perlstrings.swg>
%fragment("<qt_string.h>","header")
%{
#include <QtCore/qstring.h>
#include <QtCore/qbytearray.h>
%}
%fragment("Qt_init", "header",fragment="<qt_string.h>") {
SWIGINTERN int
Qt_init(const QChar *val)
{
QString qTemp(val);
std::wstring str = qTemp.toStdWString();
return qTemp.length();
}
}
%fragment("SWIG_AsQtChPtrAndSize","header",fragment="SWIG_AsCharPtrAndSize",fragment="Qt_init") {
SWIGINTERN int
SWIG_AsQtChPtrAndSize(SV *obj, QChar **val, size_t* psize, int *alloc)
{
if (!val) {
return SWIG_AsCharPtrAndSize(obj, 0, 0, 0);
} else {
size_t size;
char *cptr = 0;
int calloc = SWIG_OLDOBJ;
int res = SWIG_AsCharPtrAndSize(obj, &cptr, &size, &calloc);
if (SWIG_IsOK(res)) {
STRLEN length = size - 1;
if (SvUTF8(obj)) {
int charsEaten = 0;
unsigned char* sizes = %new_array(size, unsigned char);
*val = %new_array(size, QChar);
QString qVal = QString::fromUtf8(cptr);
for (int i = 0; i < qVal.length(); i++)
{
(*val)[i] = qVal.at(i);
}
// indicate the end of the string
(*val)[qVal.length()] = '\0';
} else {
QString qVal = QString::fromLocal8Bit(cptr);
int val_size = qVal.length();
if (val_size)
{
*val = new QChar[val_size+1];
if (*val)
{
for (size_t i = 0; i < val_size; ++i) {
(*val)[i] = qVal.at(i);
}
(*val)[val_size] = 0;
}
}
else
*val = NULL;
}
if (psize) *psize = size;
if (alloc) *alloc = SWIG_NEWOBJ;
if (calloc == SWIG_NEWOBJ) %delete_array(cptr);
return SWIG_NEWOBJ;
} else {
return res;
}
}
}
}
%fragment("SWIG_FromQtChPtrAndSize","header",fragment="Qt_init") {
SWIGINTERNINLINE SV *
SWIG_FromQtChPtrAndSize(const QChar* input, size_t size)
{
if (size <= 0)
return NULL;
SV *output = sv_newmortal();
int charsEaten = 0;
int length = size; // string length
unsigned char* res = %new_array(length * 13, unsigned char); // output string
QString qVal(input, size);
QByteArray qBytes = qVal.toUtf8();
char *data = qBytes.data();
char* pRes = (char*)&res[0];
while (*data) {
*pRes = *data;
++data;
pRes++;
}
res[qBytes.size()] = '\0';
sv_setpv((SV*)output, (char *)res );
SvUTF8_on((SV*)output);
%delete_array(res);
return output;
}
}
%include <typemaps/strings.swg>
%typemaps_string(%checkcode(UNISTRING), %checkcode(UNICHAR),
QChar, QChar,
SWIG_AsQtChPtrAndSize,
SWIG_FromQtChPtrAndSize,
Qt_init,
"<qt_string.h>", (-2147483647 - 1), 2147483647);