|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#ifndef TO_PYTHON_VALUE_DWA200221_HPP |
| 7 | +# define TO_PYTHON_VALUE_DWA200221_HPP |
| 8 | + |
| 9 | +# include <boost/type_traits/transform_traits.hpp> |
| 10 | +# include <boost/python/converter/type_id.hpp> |
| 11 | +# include <boost/python/converter/registry.hpp> |
| 12 | +# include <boost/python/converter/to_python_function.hpp> |
| 13 | +# include <boost/python/converter/builtin_to_python_converters.hpp> |
| 14 | + |
| 15 | +namespace boost { namespace python { |
| 16 | + |
| 17 | +template <class T> |
| 18 | +struct to_python_value |
| 19 | +{ |
| 20 | + typedef typename add_reference< |
| 21 | + typename add_const<T>::type |
| 22 | + >::type argument_type; |
| 23 | + |
| 24 | + static bool convertible(); |
| 25 | + PyObject* operator()(argument_type) const; |
| 26 | + |
| 27 | + private: |
| 28 | + // Note that this is a pointer to a function pointer |
| 29 | + static converter::to_python_value_function const* fconvert; |
| 30 | +}; |
| 31 | + |
| 32 | + |
| 33 | +template <class T> |
| 34 | +converter::to_python_value_function const* |
| 35 | +to_python_value<T>::fconvert |
| 36 | + = &converter::registry::to_python_function(converter::undecorated_type_id<T>()); |
| 37 | + |
| 38 | + |
| 39 | +template <class T> |
| 40 | +bool to_python_value<T>::convertible() |
| 41 | +{ |
| 42 | + // if this assert fires, our static variable hasn't been set up yet. |
| 43 | + assert(fconvert != 0); |
| 44 | + return *fconvert != 0; |
| 45 | +} |
| 46 | + |
| 47 | +template <class T> |
| 48 | +PyObject* to_python_value<T>::operator()(argument_type x) const |
| 49 | +{ |
| 50 | + // This might be further optimized on platforms which dynamically |
| 51 | + // link without specific imports/exports |
| 52 | + converter::to_python_value_function f = *fconvert; |
| 53 | + return f(&x); |
| 54 | +} |
| 55 | + |
| 56 | +}} // namespace boost::python |
| 57 | + |
| 58 | +#endif // TO_PYTHON_VALUE_DWA200221_HPP |
0 commit comments