Reputation: 115
Assume I've got a simple structure with a single field:
typedef struct {
MY_UNICODE value[512];
} TEST_STRUCTURE
Where MY_UNICODE
is a custom unicode implementation.
Additionally I've got two methods:
int UTF8ToMyUnicode(char *utf8, MY_UNICODE *unicode);
int MyUnicodeToUTF8(MY_UNICODE *unicode, char *utf8);
To convert from and to this custom type.
Now I can generate a Python interface for this using SWIG
.
But when I try to access TESTSTRUCTURE.value
in Python. I always get a point to an MY_UNICODE
object.
My question is: How do I wrap the access to the member of the struct such that I do get python strings and can set the value using python strings?
I know the documentation of SWIG says something about the memberin
typemap.
But my example does not work:
%module test
%include "typemaps.i"
// This is the header file, where the structure and the functions are defined
%include "test.h"
%typemap(memberin) MY_UNICODE [512] {
if(UTF8ToMyUnicode($1, $input) != 0) {
return NULL;
}
}
%typemap(memberout) MY_UNICODE [512] {
if(MyUnicodeToUTF8($1, $input) != 0) {
return NULL;
}
}
In the generated wrapper file, the map has not been applied.
Any help would be appreciated! Thanks!
PS: I'm using swig 2.0.10
Upvotes: 1
Views: 998
Reputation: 115
I've solved the problem myself. The important thing is that the typemaps need to be defined BEFORE the structures are defined in the interface. The documentation is not clear about that (or I have not seen it). Additionally the "in" and "out" typemaps can than be used to transform the values. This example works for me:
%module test
%include "typemaps.i"
%typemap(in) MY_UNICODE [512] {
if(UTF8ToMyUnicode($1, $input) != 0) {
return NULL;
}
}
%typemap(out) MY_UNICODE [512] {
if(MyUnicodeToUTF8($1, $result) != 0) {
return NULL;
}
}
// Now include the header file
%include "test.h"
Upvotes: 2