Chris@16: // Copyright David Abrahams 2002. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: #ifndef BUILTIN_CONVERTERS_DWA2002124_HPP Chris@16: # define BUILTIN_CONVERTERS_DWA2002124_HPP Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: // Since all we can use to decide how to convert an object to_python Chris@16: // is its C++ type, there can be only one such converter for each Chris@16: // type. Therefore, for built-in conversions we can bypass registry Chris@16: // lookups using explicit specializations of arg_to_python and Chris@16: // result_to_python. Chris@16: Chris@16: namespace boost { namespace python { Chris@16: Chris@16: namespace converter Chris@16: { Chris@16: template struct arg_to_python; Chris@16: BOOST_PYTHON_DECL PyObject* do_return_to_python(char); Chris@16: BOOST_PYTHON_DECL PyObject* do_return_to_python(char const*); Chris@16: BOOST_PYTHON_DECL PyObject* do_return_to_python(PyObject*); Chris@16: BOOST_PYTHON_DECL PyObject* do_arg_to_python(PyObject*); Chris@16: } Chris@16: Chris@16: // Provide specializations of to_python_value Chris@16: template struct to_python_value; Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: // Since there's no registry lookup, always report the existence of Chris@16: // a converter. Chris@16: struct builtin_to_python Chris@16: { Chris@16: // This information helps make_getter() decide whether to try to Chris@16: // return an internal reference or not. I don't like it much, Chris@16: // but it will have to serve for now. Chris@16: BOOST_STATIC_CONSTANT(bool, uses_registry = false); Chris@16: }; Chris@16: } Chris@16: Chris@16: // Use expr to create the PyObject corresponding to x Chris@16: # define BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T, expr, pytype)\ Chris@16: template <> struct to_python_value \ Chris@16: : detail::builtin_to_python \ Chris@16: { \ Chris@16: inline PyObject* operator()(T const& x) const \ Chris@16: { \ Chris@16: return (expr); \ Chris@16: } \ Chris@16: inline PyTypeObject const* get_pytype() const \ Chris@16: { \ Chris@16: return (pytype); \ Chris@16: } \ Chris@16: }; \ Chris@16: template <> struct to_python_value \ Chris@16: : detail::builtin_to_python \ Chris@16: { \ Chris@16: inline PyObject* operator()(T const& x) const \ Chris@16: { \ Chris@16: return (expr); \ Chris@16: } \ Chris@16: inline PyTypeObject const* get_pytype() const \ Chris@16: { \ Chris@16: return (pytype); \ Chris@16: } \ Chris@16: }; Chris@16: Chris@16: # define BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T, expr) \ Chris@16: namespace converter \ Chris@16: { \ Chris@16: template <> struct arg_to_python< T > \ Chris@16: : handle<> \ Chris@16: { \ Chris@16: arg_to_python(T const& x) \ Chris@16: : python::handle<>(expr) {} \ Chris@16: }; \ Chris@16: } Chris@16: Chris@16: // Specialize argument and return value converters for T using expr Chris@16: # define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr, pytype) \ Chris@16: BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T,expr, pytype) \ Chris@16: BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T,expr) Chris@16: Chris@16: // Specialize converters for signed and unsigned T to Python Int Chris@16: #if PY_VERSION_HEX >= 0x03000000 Chris@16: Chris@16: # define BOOST_PYTHON_TO_INT(T) \ Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, ::PyLong_FromLong(x), &PyLong_Type) \ Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned T, ::PyLong_FromUnsignedLong(x), &PyLong_Type) Chris@16: Chris@16: #else Chris@16: Chris@16: # define BOOST_PYTHON_TO_INT(T) \ Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, ::PyInt_FromLong(x), &PyInt_Type) \ Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE( \ Chris@16: unsigned T \ Chris@16: , static_cast(x) > static_cast( \ Chris@16: (std::numeric_limits::max)()) \ Chris@16: ? ::PyLong_FromUnsignedLong(x) \ Chris@16: : ::PyInt_FromLong(x), &PyInt_Type) Chris@16: #endif Chris@16: Chris@16: // Bool is not signed. Chris@16: #if PY_VERSION_HEX >= 0x02030000 Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyBool_FromLong(x), &PyBool_Type) Chris@16: #else Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyInt_FromLong(x), &PyInt_Type) Chris@16: #endif Chris@16: Chris@16: // note: handles signed char and unsigned char, but not char (see below) Chris@16: BOOST_PYTHON_TO_INT(char) Chris@16: Chris@16: BOOST_PYTHON_TO_INT(short) Chris@16: BOOST_PYTHON_TO_INT(int) Chris@16: BOOST_PYTHON_TO_INT(long) Chris@16: Chris@16: # if defined(_MSC_VER) && defined(_WIN64) && PY_VERSION_HEX < 0x03000000 Chris@16: /* Under 64-bit Windows std::size_t is "unsigned long long". To avoid Chris@16: getting a Python long for each std::size_t the value is checked before Chris@16: the conversion. A std::size_t is converted to a simple Python int Chris@16: if possible; a Python long appears only if the value is too small or Chris@16: too large to fit into a simple int. */ Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE( Chris@16: signed BOOST_PYTHON_LONG_LONG, Chris@16: ( x < static_cast( Chris@16: (std::numeric_limits::min)()) Chris@16: || x > static_cast( Chris@16: (std::numeric_limits::max)())) Chris@16: ? ::PyLong_FromLongLong(x) Chris@16: : ::PyInt_FromLong(static_cast(x)), &PyInt_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE( Chris@16: unsigned BOOST_PYTHON_LONG_LONG, Chris@16: x > static_cast( Chris@16: (std::numeric_limits::max)()) Chris@16: ? ::PyLong_FromUnsignedLongLong(x) Chris@16: : ::PyInt_FromLong(static_cast(x)), &PyInt_Type) Chris@16: // Chris@16: # elif defined(HAVE_LONG_LONG) // using Python's macro instead of Boost's Chris@16: // - we don't seem to get the config right Chris@16: // all the time. Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed BOOST_PYTHON_LONG_LONG, ::PyLong_FromLongLong(x), &PyLong_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned BOOST_PYTHON_LONG_LONG, ::PyLong_FromUnsignedLongLong(x), &PyLong_Type) Chris@16: # endif Chris@16: Chris@16: # undef BOOST_TO_PYTHON_INT Chris@16: Chris@16: #if PY_VERSION_HEX >= 0x03000000 Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x), &PyUnicode_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x), &PyUnicode_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyUnicode_FromStringAndSize(x.data(),implicit_cast(x.size())), &PyUnicode_Type) Chris@16: #else Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x), &PyString_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x), &PyString_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast(x.size())), &PyString_Type) Chris@16: #endif Chris@16: Chris@16: #if defined(Py_USING_UNICODE) && !defined(BOOST_NO_STD_WSTRING) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast(x.size())), &PyUnicode_Type) Chris@16: # endif Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, ::PyFloat_FromDouble(x), &PyFloat_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, ::PyFloat_FromDouble(x), &PyFloat_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, ::PyFloat_FromDouble(x), &PyFloat_Type) Chris@16: BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(PyObject*, converter::do_return_to_python(x), 0) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type) Chris@16: BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type) Chris@16: Chris@16: # undef BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE Chris@16: # undef BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE Chris@16: # undef BOOST_PYTHON_TO_PYTHON_BY_VALUE Chris@16: # undef BOOST_PYTHON_TO_INT Chris@16: Chris@16: namespace converter Chris@16: { Chris@16: Chris@16: void initialize_builtin_converters(); Chris@16: Chris@16: } Chris@16: Chris@16: }} // namespace boost::python::converter Chris@16: Chris@16: #endif // BUILTIN_CONVERTERS_DWA2002124_HPP