[C++-sig] [python] python + phoenix

Ravi lists_ravi at lavabit.com
Sat Oct 31 05:02:37 CET 2009


On Wednesday 14 October 2009 21:59:42 troy d. straszheim wrote:
> boost::function still works, and doesn't require as<>:
> 
>    boost::function<int(int, int)> myplus = std::plus<int>();
>    def("myplus", myplus);
> 
> and old-school function objects:
> 
>    def("otherplus", std::plus<int>())
> 
> I was surprised to find that it all plays nice with boost::phoenix:
> 
>    def("plus", as<int(int, int)>(arg1 + arg2));
> 
>    def("throw_if_gt_5",
>        as<void(int)>(if_(arg1 > 5)
>                     [ throw_(std::runtime_error("too big!!")) ]
>                     .else_
>                     [ std::cout << val("okay")                ]));
> 
> And boost::phoenix plays nice with shared_ptr, which plays nice with
> boost::python:
> 
>    struct X { int x; };   typedef boost::shared_ptr<X> XPtr;
> 
>    class_<X, XPtr>("X")
>      .def_readwrite("x", &X::x)
>      .def("add7", as<void(X*)>  (arg1->*&X::x      += 7))
>      .def("add8", as<void(XPtr)>(arg1->*&X::x      += 8))
>      .def("add9", as<void(X&)>  (bind(&X::x, arg1) += 9))
>    ;
> 
> the other little thing is overload resolution, for which I propose a
> little helper function "unload" (underload? deload? resolve?  I dunno):
> 
>    void foo(int);
>    void foo(double);
> 
>    def("foo", unload<void(int)>(foo));
> 
> Which is safer than
> 
>    def("foo", (void(*)(int)) foo);
> 
> and just as safe as, and less verbose than
> 
>    void(*fooint)(int) = foo;
>    def("foo", fooint);
> 
> So that's where I'm at...  what do you make of that interface?

This is very cool and pretty intuitive. My main concern is that one needs to 
build up the function type in generic code (as opposed to mpl vectors), but we 
do have function_types to help us.

You are going to have to propose this as several small patched, before Ralf & 
Dave agree to it.

Regards,
Ravi



More information about the Cplusplus-sig mailing list