Reputation: 4132
Suppose I have a single function for which I want to apply a special typemap (because it's returning binary data as a char array).
const char* returnBinaryData( int arg1, int arg2, size_t ret_length );
because there are other functions with the same return type that I don't want to touch.
const char* getName( int arg1, int arg2 );
Is there a way to apply an (out
) typemap only to a specific function (using its name, not its parameter list)?
(I'm using SWIG with Python.)
One complicating factor of this is that the function I want to wrap is a class method.
Consider this as the definition.
class A {
public:
char datum[16];
char name[32];
A( int32_t seed ) : name("sample name") {
for (int i=0;i<16;i++) datum[i] = static_cast<char>(((i*i+seed) % 64) + '0');
}
const char* getName() {
return name;
}
const char* getBinaryData( int32_t arg1, int32_t length ) {
auto s = new char[length];
for (int i=0;i<length;i++) s[i] = datum[(arg1 + i) % sizeof(datum)];
return s;
}
};
What would need to change in order to "rename" the wrapper to be used as if it were a class method?
I tried scoping the rename operations, but the wrapper code it generated didn't do what I wanted. (The docs are not entirely clear about doing this, AFAICT.)
Upvotes: 1
Views: 860
Reputation: 177755
I used a wrapper function and created a custom typemap for the special case:
test.i
%module test
%{
const char* getName(int arg1, int arg2) {
static char s[] {"something"};
return s;
}
const char* returnBinaryData(int arg1, int arg2, size_t ret_length) {
auto s = new char[ret_length];
for(size_t i = 0; i < ret_length; ++i)
s[i] = static_cast<char>(i % 256);
return s;
}
// wrapper to allow custom typemap
void mygetbin(int arg1, int arg2, size_t length, const char** output) {
*output = returnBinaryData(arg1, arg2, length);
}
%}
%include <exception.i>
// Input typemap that expects a Python integer input for this precise pair of parameters
%typemap(in) (size_t length, const char** output) (char* out = nullptr) %{
$2 = &out;
if(!PyLong_Check($input))
SWIG_exception(SWIG_TypeError, "expected integer");
$1 = PyLong_AsUnsignedLongLong($input);
%}
// A Python bytes object will be returned that Python will manage,
// so free the C++ allocation to prevent a memory leak.
%typemap(freearg) (size_t length, const char** output) %{
delete[] *$2;
%}
// Append the output argument to the return value.
%typemap(argout) (size_t length, const char** output) %{
$result = SWIG_Python_AppendOutput($result, PyBytes_FromStringAndSize(*$2, $1));
%}
%ignore returnBinaryData; // necessary if getName and returnBinaryData included via %include "someheader.h"
// Process this function normally with default handling of char* output.
// This could be %include "someheader.h" instead, and all the functions would be swigged except
// for the ignored function.
const char* getName(int arg1, int arg2);
// Explicitly rename the wrapper and process it.
%rename(returnBinaryData) mygetbin;
void mygetbin(int arg1, int arg2, size_t length, const char** output);
Demo:
>>> import test
>>> test.getName(1,2)
'something'
>>> test.returnBinaryData(1,2,10)
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t'
Upvotes: 1