toast python library


Detailed Description

We love Boost.Python! But it doesn't suit our needs for callbacks and there isn't great support for wrapping containers. So this module contains some extensions that make our lives easier in those respects. Take a look at toast::python::signal_connect and toast::python::register_container.


Files

file  callback.hpp
 This file contains the toast::python::signal_connect function.
file  container.hpp
 This file contains the toast::python::register_container helper.
file  make_callback.hpp
 This file contains the make_callback function.
file  optional.hpp
 This file contains utility functions for writing boost::python wrappers that make use of boost::optional.
file  wrapper.hpp
 This file contains a utility template for wrapping pure virtual functions.

Classes

struct  toast::python::register_container< T >
 Register a container to be automatically wrapped in python with Boost.Python. More...
struct  toast::python::object_from_python< T, TfromPy >
struct  toast::python::register_python_conversion< T, TtoPy, TfromPy >
struct  toast::python::register_optional< T >
 Registers a boost::optional<Type> for in Python, making empty optionals into 'None' in Python. More...
struct  toast::python::register_optional< double >
struct  toast::python::wrapper< T >
 Base class for python bindings that helps wrapping pure virtual functions. More...

Functions

template<typename Signature, typename Connection, typename T, typename Slot>
boost::python::object toast::python::signal_connect (Connection(T::*f)(Slot))
 Wraps a signal/slot style connect function to be used with Boost.Python.
template<typename Signature>
boost::function< Signature > make_callback (boost::python::object const &callback)
 Makes a C++ boost::function<Signature> object from a callable python object.


Function Documentation

template<typename Signature>
boost::function<Signature> make_callback ( boost::python::object const &  callback  )  [inline]

Makes a C++ boost::function<Signature> object from a callable python object.

The returned boost::function will print any exception thrown during the execution of the 'callback' to stderr

template<typename Signature, typename Connection, typename T, typename Slot>
boost::python::object toast::python::signal_connect ( Connection(T::*)(Slot)  f  )  [inline]

Wraps a signal/slot style connect function to be used with Boost.Python.

Used something like '.def("onUpdate", signal_connect<void ()>(&Class::onUpdate)' the resulting python method will take a python callable and bind any following arguments to the callback. It will also return a connection which if you need to be able to use it needs to be wrapped with Boost.Python. This has worked well for use with Boost signals and Sigc++ signals as well as another variant.

It is important to note that arguments will be passed to python as the signature describes. If you use 'signal_connect<void (T &)>' then a reference will be passed to python. You will be able to modify T in python and the effect will be visible in C++. Also, if the object goes away in C++ you will have a dangling reference in python!! You probably don't want to store these objects on the python side. If you want to simplify your life you can copy the object by using 'signal_connect<void (T)>' instead, but this could be a large copy, and you won't effect the object on the C++ side.


SourceForge.net Logo