Allen Bierbaum wrote:
> void getSize(float& width, float& height);
>
> To wrap this method a user normally needs to manually write a C++
> wrapper method that will call the function and return the values in a
> boost.python.tuple. This is not difficult to do, but it can get very
> tedious in a large API.
>
> I know there have been discussions about automating this, but has anyone
> actually written code that will do it. I don't need the code integrated
> into pyplusplus or anything like that right now, I just want to know if
> someone has code they are willing to share so I don't have to start from
> scratch. :)
I've implemented something in pypp_api that I called "argument
policies". The code for that is in pypp_api/argpolicy.py (the naming is
somewhat inconsistent because I initially called it "argument
transformers" and the base class for the policies is still called
"ArgTransformerBase").
The argument policies are applied via decoration just as the normal call
policies. In your case, it would be:
cls.Method("getSize").setArgPolicy(Output(1), Output(2))
This specifies that the first and second argument of the method are
actually output values. The Python version of the method will then take
no arguments and return a tuple (width, height).
I have a few "standard" arg policies such as Output, InputArray and
OutputArray that are defined right in argpolicy.py. I've also a few
custom policies in my Maya bindings that do things like checking array
bounds or manipulating those Maya status objects.
Those arg policies are actually just specialized code creators that each
add a block of C++ code before and after the original function/method
invocation. They can also manipulate the argument list and the return value.
Everything is implemented on top of pyplusplus and currently it only
works for methods that don't already create wrappers in pyplusplus. It
would be great if this functionality could eventually moved "down" to
pyplusplus so that the normal wrapper generation also works like this.
This would provide a standard way for a user to inject custom code into
the generated wrappers. I'm currently trying to investigate how this
could be achieved because I want to make some of my wrapped code thread
safe which, I believe, is not possible with the current version of
pyplusplus and pypp_api.
- Matthias -
|