问题
Here is my c++ code:
struct Impl
{
DT* data_ptr_;
Impl(void* data_ptr)
: data_ptr_((DT*)data_ptr)
{
//do something to decipher data
}
};
Impl class takes a void pointer, as a input parameter.
What I want to do is just pass a Python string(binary string as in Python 2.x) as parameter:
data = "I'm a stirng data!"
impl = Impl(data)
But Swig generated module raises this
TypeError: in method 'new_Impl', argument 1 of type 'void *'
I'm new to swig and have searched in SWIG documentation for a whole day now. The only thing worked for me is in swig document 8.3 C String Handling. But my function don't really take a size integer.
I think my problem is rather simple, I must missed something, please help.
Thanks!
回答1:
I found a solution myself.
As I only needed the void* pointer as input:
%typemap(in) void* = char*;
will do the trick.
Swig accept char* parameter as string type, and take void* just as an pointer. so use the char* input type will be okay.
来源:https://stackoverflow.com/questions/12686400/how-to-use-swig-to-treat-a-void-function-parameter-as-python-string