1

I'm creating a python wrapper for a C++ api, making support for some types was more or less straightfoward using templates but the api needs some variables by reference, one of them is an int and the other is float.

If I am not wrong it is done by creating a INOUT apply like:

%apply float& INOUT { float& a };
%apply int& INOUT { int& a };

But how I use that types inside my python?

Thank you in advance for any tip.

Frank Escobar
  • 368
  • 4
  • 20

2 Answers2

1

These parameters are returned from the generated python function (not totally intuitive, considering the wrapped C++ function signature). Let's say this is your exposed function:

%apply float& INOUT { float& a };
%apply int& INOUT { int& b };

void foo(float& a, int& b);

you would invoke it from python as

a = 3.14
b = 42

a, b = foo(a, b)

If you don't capture the return values, a and b remain unchanged.

lubgr
  • 37,368
  • 3
  • 66
  • 117
  • unfortunatly it doesn't worked for me a = 0 b = 0.0 a , b = mu.foo(False, a , b) TypeError: in method 'MyApi_foo', argument 3 of type 'int &' where the actual function is foo(bool, int&, float&) – Frank Escobar Apr 08 '19 at 08:15
  • 1
    Well, you were supposed to adjust the solution to the actual function you're trying to call :) As you didn't post it with the rest of the snippet, the above was an example of how this works in general. – lubgr Apr 08 '19 at 17:27
0

Dear future reader these are the droids you are looking for:

myapi.new_intp()
myapi.new_floatp()  
myapi.intp_value(a)
myapi.floatp_value(b)
myapi.delete_intp(a)
myapi.delete_floatp(b)
Frank Escobar
  • 368
  • 4
  • 20