|
5 | 5 | // to its suitability for any purpose. |
6 | 6 | #ifndef BUILTIN_CONVERTERS_DWA2002124_HPP |
7 | 7 | # define BUILTIN_CONVERTERS_DWA2002124_HPP |
| 8 | +# include <boost/python/detail/wrap_python.hpp> |
| 9 | +# include <string> |
8 | 10 |
|
9 | | -namespace boost { namespace python { namespace converter { |
| 11 | +namespace boost { namespace python { |
10 | 12 |
|
11 | | -void initialize_builtin_converters(); |
| 13 | +// Provide specializations of to_python_value |
| 14 | +template <class T> struct to_python_value; |
12 | 15 |
|
13 | | -}}} // namespace boost::python::converter |
| 16 | +namespace detail |
| 17 | +{ |
| 18 | + struct builtin_to_python |
| 19 | + { |
| 20 | + static bool convertible() { return true; } |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +# define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr) \ |
| 25 | + template <> struct to_python_value<T&> \ |
| 26 | + : detail::builtin_to_python \ |
| 27 | + { \ |
| 28 | + PyObject* operator()(T const& x) const \ |
| 29 | + { \ |
| 30 | + return (expr); \ |
| 31 | + } \ |
| 32 | + }; \ |
| 33 | + template <> struct to_python_value<T const&> \ |
| 34 | + : detail::builtin_to_python \ |
| 35 | + { \ |
| 36 | + PyObject* operator()(T const& x) const \ |
| 37 | + { \ |
| 38 | + return (expr); \ |
| 39 | + } \ |
| 40 | + }; |
| 41 | + |
| 42 | + |
| 43 | +# define BOOST_PYTHON_TO_INT(T) \ |
| 44 | + BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, PyInt_FromLong(x)) \ |
| 45 | + BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned T, PyInt_FromLong(x)) |
| 46 | + |
| 47 | +BOOST_PYTHON_TO_INT(char) |
| 48 | +BOOST_PYTHON_TO_INT(short) |
| 49 | +BOOST_PYTHON_TO_INT(int) |
| 50 | +BOOST_PYTHON_TO_INT(long) |
| 51 | +# undef BOOST_TO_PYTHON_INT |
| 52 | + |
| 53 | +BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, PyString_FromString(x)) |
| 54 | +BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, PyString_FromString(x.c_str())) |
| 55 | +BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, PyFloat_FromDouble(x)) |
| 56 | +BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, PyFloat_FromDouble(x)) |
| 57 | +BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, PyFloat_FromDouble(x)) |
| 58 | +BOOST_PYTHON_TO_PYTHON_BY_VALUE(PyObject*, x) |
| 59 | + |
| 60 | +namespace converter |
| 61 | +{ |
| 62 | + |
| 63 | + void initialize_builtin_converters(); |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +}} // namespace boost::python::converter |
14 | 68 |
|
15 | 69 | #endif // BUILTIN_CONVERTERS_DWA2002124_HPP |
0 commit comments