Vadim Zeitlin
2015-07-29 22:04:14 UTC
Hello,
As http://www.swig.org/Doc3.0/Library.html#Library_nn10 explains, SWIG
provides convenient (char* STRING, size_t LENGTH) typemaps for passing
binary data. Unfortunately I have two problems with it:
The first one is relatively trivial and is that in my case I have a
function taking (as is not uncommon IME) "unsigned char* data, size_t
length" parameters and doing
%apply (char *STRING, size_t LENGTH) { (unsigned char* data, size_t length) };
results in uncompilable code (at least for Java, but probably other
languages too) because the generated code tries to assign "char*" to an
"unsigned char*" which can't be done in C++. I could work around this by
doing
%extend {
void MyFunc(char* data, size_t length) {
self->MyFunc(reinterpret_cast<unsigned char*>(data), length);
}
}
which works but is rather ugly.
The second problem is that this typemap is not available in all languages,
notably not in C# so currently this doesn't work at all there.
I'd like to fix both problems, especially the latter one but also the
former one if there are no objections to extending the typemap to unsigned
case. The trouble is that I'm not sure about how to do it because the
existing typemap seems to be defined in a number of places:
1. Lib/cdata.i
2. In various files under Lib/{chicken,go,guile,java,ocaml,php,pyke,r}
3. Lib/typemaps/string.swg (via Lib/typemaps/strings.swg included by it)
It looks like (3) is supposed to be the new way to do it, but why then
does Java do it in its own way? To be even more clear, my question is
whether C# should follow Java or use the generic typemaps library?
Thanks in advance for any hints,
VZ
As http://www.swig.org/Doc3.0/Library.html#Library_nn10 explains, SWIG
provides convenient (char* STRING, size_t LENGTH) typemaps for passing
binary data. Unfortunately I have two problems with it:
The first one is relatively trivial and is that in my case I have a
function taking (as is not uncommon IME) "unsigned char* data, size_t
length" parameters and doing
%apply (char *STRING, size_t LENGTH) { (unsigned char* data, size_t length) };
results in uncompilable code (at least for Java, but probably other
languages too) because the generated code tries to assign "char*" to an
"unsigned char*" which can't be done in C++. I could work around this by
doing
%extend {
void MyFunc(char* data, size_t length) {
self->MyFunc(reinterpret_cast<unsigned char*>(data), length);
}
}
which works but is rather ugly.
The second problem is that this typemap is not available in all languages,
notably not in C# so currently this doesn't work at all there.
I'd like to fix both problems, especially the latter one but also the
former one if there are no objections to extending the typemap to unsigned
case. The trouble is that I'm not sure about how to do it because the
existing typemap seems to be defined in a number of places:
1. Lib/cdata.i
2. In various files under Lib/{chicken,go,guile,java,ocaml,php,pyke,r}
3. Lib/typemaps/string.swg (via Lib/typemaps/strings.swg included by it)
It looks like (3) is supposed to be the new way to do it, but why then
does Java do it in its own way? To be even more clear, my question is
whether C# should follow Java or use the generic typemaps library?
Thanks in advance for any hints,
VZ