Re: [pygccxml-development] How to manage wrapper classes?
Brought to you by:
mbaas,
roman_yakovenko
From: Roman Y. <rom...@gm...> - 2006-03-26 20:37:25
|
On 3/26/06, Matthias Baas <ba...@ir...> wrote: > Roman Yakovenko wrote: > > Lets skip the rest. Here is what I propouse to you: > > http://boost.org/libs/python/doc/v2/faq.html#custom_string > > Oops, I missed that one! > I've just tried it out and it works like a charm. Thanks! My pleasure. > In the Maya SDK error handling is done with MStatus objects which carry > error information (C++ exceptions are never used). There are two > possibilities how a SDK function returns a result object. It either > returns a MStatus object as in the following example: > > MStatus select(MObject& object); > > or it expects an optional pointer to a MStatus object as input which it > uses to set its status information: > > bool isSelected(MObject& object, MStatus* ReturnStatus=3DNULL); > > So far, the Python bindings just work like the C++ version. But what I > would actually like to have is that the Python version may raise an > exception when an error occurs, no matter if the user provided a MStatus > object or not. > I suppose in the first case where the function returns a MStatus object > I could probably "abuse" a result converter to check for an error > instead of converting the value (I haven't tried that out yet). But for > the second case I think it requires a wrapper because I need to place > code before and after the actual function call, something like: > > bool isSelected_wrapped(MObject& object, MStatus* ReturnStatus=3DNULL) > { > MStatus internalstat; > MStatus* finalstat=3D&internalstat; > // If the user provided a MStatus object then use this one... > if (ReturnStatus!=3DNULL) > { > finalstat =3D ReturnStatus; > } > > // Call the original function... > bool res =3D isSelected(object, finalstat); > > // Check for errors... > if (exceptions_enabled && finalstat->error()) > { > throw AnAppropriateException(); > } > return res; > } First of all I think you should be able to create custom call policies. Take a look on this link: http://boost.org/libs/python/doc/v2/CallPolicies.html#CallPolicies-concept May be, this will be enough, I am not sure. > So how should that be accomplished? There are 2 other ways to solve this problem. 1. Create small wrappers in Python. I do not know exactly how it should be = done. But, I feel that this is the right way to go. I hope some days pyplusplus will have 3'rd tree - Python code creators ( they will generate Python code ). 2. Create small wrapper in C++, in the way you did it. Just put that declar= ation in some namespace and add it to using add_code functions. Using this approach you should be carefull. There are so many cases, while exporti= ng some function to Python. By the way, do we talk about pure virtual functions or overloaded ones? >(and how can it be guaranteed that > the user's wrapper code doesn't interfere with the wrapper code that > might already get generated by pyplusplus?) 1. Do you still want to expose functions that returns, take by reference MStatus object? If the answer is not then we can add replace_code function to decl_wrapper_= t class. Then pyplusplus will generate that code. If the answer is yes, then you have a problem. It seems to me that pypluspl= us contains all tools in order to solve this problem, may be it does not expose good interface ( we will find out, right :-) ). If I were you I'd ask on boost.python mailing list about this problem and it's solution using call policies. > - Matthias - -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |