[C++-sig] Boost.Python call policies at runtime...

Roman Yakovenko roman.yakovenko at gmail.com
Thu Nov 9 17:39:02 CET 2006


On 11/9/06, Kevin Jones <investtcartier at yahoo.com> wrote:
> Hi all,
>
> Given a class called Variant that can (at runtime)
> contain either an int or a T*.
>
> I.E.
>   class Variant {
>     public:
>       int type;
>       union {
>         int i;
>         T* t;
>       };
>   };
>
> How can I create a call policy that uses
> return_by_value when Variant is an int or
> manage_new_object when Variant is a T* (based on the
> value of "type" at runtime)?
>
> Note: I need to wrap either the int as a python object
> or T* as a python object and not Variant.
>
> Any help or sugestions would be most appreciated.

It should not be too difficult to create one.
Take a look on next files:
    return_value_policy.hpp
    manage_new_object.hpp
    to_python_indirect.hpp


Basically you should create new class
struct my_policy{

    struct object_maker{
        PyObject* operator()( Variant const& var ){
            if( var contains T* ){
                return manage_new_object::apply::type()( var.t );
            }
            else{
                return object( var.i );
            }
        }
    };
     typedef object_maker apply;
};

def( "get_x", & get_x, return_value_policy< object_maker >() );

Something like this.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list