annotate DEPENDENCIES/generic/include/boost/python/converter/builtin_converters.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 // Copyright David Abrahams 2002.
Chris@16 2 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 3 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 4 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 5 #ifndef BUILTIN_CONVERTERS_DWA2002124_HPP
Chris@16 6 # define BUILTIN_CONVERTERS_DWA2002124_HPP
Chris@16 7 # include <boost/python/detail/prefix.hpp>
Chris@16 8 # include <boost/python/detail/none.hpp>
Chris@16 9 # include <boost/python/handle.hpp>
Chris@16 10 # include <boost/python/ssize_t.hpp>
Chris@16 11 # include <boost/implicit_cast.hpp>
Chris@16 12 # include <string>
Chris@16 13 # include <complex>
Chris@16 14 # include <boost/limits.hpp>
Chris@16 15
Chris@16 16 // Since all we can use to decide how to convert an object to_python
Chris@16 17 // is its C++ type, there can be only one such converter for each
Chris@16 18 // type. Therefore, for built-in conversions we can bypass registry
Chris@16 19 // lookups using explicit specializations of arg_to_python and
Chris@16 20 // result_to_python.
Chris@16 21
Chris@16 22 namespace boost { namespace python {
Chris@16 23
Chris@16 24 namespace converter
Chris@16 25 {
Chris@16 26 template <class T> struct arg_to_python;
Chris@16 27 BOOST_PYTHON_DECL PyObject* do_return_to_python(char);
Chris@16 28 BOOST_PYTHON_DECL PyObject* do_return_to_python(char const*);
Chris@16 29 BOOST_PYTHON_DECL PyObject* do_return_to_python(PyObject*);
Chris@16 30 BOOST_PYTHON_DECL PyObject* do_arg_to_python(PyObject*);
Chris@16 31 }
Chris@16 32
Chris@16 33 // Provide specializations of to_python_value
Chris@16 34 template <class T> struct to_python_value;
Chris@16 35
Chris@16 36 namespace detail
Chris@16 37 {
Chris@16 38 // Since there's no registry lookup, always report the existence of
Chris@16 39 // a converter.
Chris@16 40 struct builtin_to_python
Chris@16 41 {
Chris@16 42 // This information helps make_getter() decide whether to try to
Chris@16 43 // return an internal reference or not. I don't like it much,
Chris@16 44 // but it will have to serve for now.
Chris@16 45 BOOST_STATIC_CONSTANT(bool, uses_registry = false);
Chris@16 46 };
Chris@16 47 }
Chris@16 48
Chris@16 49 // Use expr to create the PyObject corresponding to x
Chris@16 50 # define BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T, expr, pytype)\
Chris@16 51 template <> struct to_python_value<T&> \
Chris@16 52 : detail::builtin_to_python \
Chris@16 53 { \
Chris@16 54 inline PyObject* operator()(T const& x) const \
Chris@16 55 { \
Chris@16 56 return (expr); \
Chris@16 57 } \
Chris@16 58 inline PyTypeObject const* get_pytype() const \
Chris@16 59 { \
Chris@16 60 return (pytype); \
Chris@16 61 } \
Chris@16 62 }; \
Chris@16 63 template <> struct to_python_value<T const&> \
Chris@16 64 : detail::builtin_to_python \
Chris@16 65 { \
Chris@16 66 inline PyObject* operator()(T const& x) const \
Chris@16 67 { \
Chris@16 68 return (expr); \
Chris@16 69 } \
Chris@16 70 inline PyTypeObject const* get_pytype() const \
Chris@16 71 { \
Chris@16 72 return (pytype); \
Chris@16 73 } \
Chris@16 74 };
Chris@16 75
Chris@16 76 # define BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T, expr) \
Chris@16 77 namespace converter \
Chris@16 78 { \
Chris@16 79 template <> struct arg_to_python< T > \
Chris@16 80 : handle<> \
Chris@16 81 { \
Chris@16 82 arg_to_python(T const& x) \
Chris@16 83 : python::handle<>(expr) {} \
Chris@16 84 }; \
Chris@16 85 }
Chris@16 86
Chris@16 87 // Specialize argument and return value converters for T using expr
Chris@16 88 # define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr, pytype) \
Chris@16 89 BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T,expr, pytype) \
Chris@16 90 BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T,expr)
Chris@16 91
Chris@16 92 // Specialize converters for signed and unsigned T to Python Int
Chris@16 93 #if PY_VERSION_HEX >= 0x03000000
Chris@16 94
Chris@16 95 # define BOOST_PYTHON_TO_INT(T) \
Chris@16 96 BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, ::PyLong_FromLong(x), &PyLong_Type) \
Chris@16 97 BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned T, ::PyLong_FromUnsignedLong(x), &PyLong_Type)
Chris@16 98
Chris@16 99 #else
Chris@16 100
Chris@16 101 # define BOOST_PYTHON_TO_INT(T) \
Chris@16 102 BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, ::PyInt_FromLong(x), &PyInt_Type) \
Chris@16 103 BOOST_PYTHON_TO_PYTHON_BY_VALUE( \
Chris@16 104 unsigned T \
Chris@16 105 , static_cast<unsigned long>(x) > static_cast<unsigned long>( \
Chris@16 106 (std::numeric_limits<long>::max)()) \
Chris@16 107 ? ::PyLong_FromUnsignedLong(x) \
Chris@16 108 : ::PyInt_FromLong(x), &PyInt_Type)
Chris@16 109 #endif
Chris@16 110
Chris@16 111 // Bool is not signed.
Chris@16 112 #if PY_VERSION_HEX >= 0x02030000
Chris@16 113 BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyBool_FromLong(x), &PyBool_Type)
Chris@16 114 #else
Chris@16 115 BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyInt_FromLong(x), &PyInt_Type)
Chris@16 116 #endif
Chris@16 117
Chris@16 118 // note: handles signed char and unsigned char, but not char (see below)
Chris@16 119 BOOST_PYTHON_TO_INT(char)
Chris@16 120
Chris@16 121 BOOST_PYTHON_TO_INT(short)
Chris@16 122 BOOST_PYTHON_TO_INT(int)
Chris@16 123 BOOST_PYTHON_TO_INT(long)
Chris@16 124
Chris@16 125 # if defined(_MSC_VER) && defined(_WIN64) && PY_VERSION_HEX < 0x03000000
Chris@16 126 /* Under 64-bit Windows std::size_t is "unsigned long long". To avoid
Chris@16 127 getting a Python long for each std::size_t the value is checked before
Chris@16 128 the conversion. A std::size_t is converted to a simple Python int
Chris@16 129 if possible; a Python long appears only if the value is too small or
Chris@16 130 too large to fit into a simple int. */
Chris@16 131 BOOST_PYTHON_TO_PYTHON_BY_VALUE(
Chris@16 132 signed BOOST_PYTHON_LONG_LONG,
Chris@16 133 ( x < static_cast<signed BOOST_PYTHON_LONG_LONG>(
Chris@16 134 (std::numeric_limits<long>::min)())
Chris@16 135 || x > static_cast<signed BOOST_PYTHON_LONG_LONG>(
Chris@16 136 (std::numeric_limits<long>::max)()))
Chris@16 137 ? ::PyLong_FromLongLong(x)
Chris@16 138 : ::PyInt_FromLong(static_cast<long>(x)), &PyInt_Type)
Chris@16 139 BOOST_PYTHON_TO_PYTHON_BY_VALUE(
Chris@16 140 unsigned BOOST_PYTHON_LONG_LONG,
Chris@16 141 x > static_cast<unsigned BOOST_PYTHON_LONG_LONG>(
Chris@16 142 (std::numeric_limits<long>::max)())
Chris@16 143 ? ::PyLong_FromUnsignedLongLong(x)
Chris@16 144 : ::PyInt_FromLong(static_cast<long>(x)), &PyInt_Type)
Chris@16 145 //
Chris@16 146 # elif defined(HAVE_LONG_LONG) // using Python's macro instead of Boost's
Chris@16 147 // - we don't seem to get the config right
Chris@16 148 // all the time.
Chris@16 149 BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed BOOST_PYTHON_LONG_LONG, ::PyLong_FromLongLong(x), &PyLong_Type)
Chris@16 150 BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned BOOST_PYTHON_LONG_LONG, ::PyLong_FromUnsignedLongLong(x), &PyLong_Type)
Chris@16 151 # endif
Chris@16 152
Chris@16 153 # undef BOOST_TO_PYTHON_INT
Chris@16 154
Chris@16 155 #if PY_VERSION_HEX >= 0x03000000
Chris@16 156 BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x), &PyUnicode_Type)
Chris@16 157 BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x), &PyUnicode_Type)
Chris@16 158 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyUnicode_FromStringAndSize(x.data(),implicit_cast<ssize_t>(x.size())), &PyUnicode_Type)
Chris@16 159 #else
Chris@16 160 BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x), &PyString_Type)
Chris@16 161 BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x), &PyString_Type)
Chris@16 162 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<ssize_t>(x.size())), &PyString_Type)
Chris@16 163 #endif
Chris@16 164
Chris@16 165 #if defined(Py_USING_UNICODE) && !defined(BOOST_NO_STD_WSTRING)
Chris@16 166 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<ssize_t>(x.size())), &PyUnicode_Type)
Chris@16 167 # endif
Chris@16 168 BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, ::PyFloat_FromDouble(x), &PyFloat_Type)
Chris@16 169 BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, ::PyFloat_FromDouble(x), &PyFloat_Type)
Chris@16 170 BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, ::PyFloat_FromDouble(x), &PyFloat_Type)
Chris@16 171 BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(PyObject*, converter::do_return_to_python(x), 0)
Chris@16 172 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<float>, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type)
Chris@16 173 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<double>, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type)
Chris@16 174 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<long double>, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type)
Chris@16 175
Chris@16 176 # undef BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE
Chris@16 177 # undef BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE
Chris@16 178 # undef BOOST_PYTHON_TO_PYTHON_BY_VALUE
Chris@16 179 # undef BOOST_PYTHON_TO_INT
Chris@16 180
Chris@16 181 namespace converter
Chris@16 182 {
Chris@16 183
Chris@16 184 void initialize_builtin_converters();
Chris@16 185
Chris@16 186 }
Chris@16 187
Chris@16 188 }} // namespace boost::python::converter
Chris@16 189
Chris@16 190 #endif // BUILTIN_CONVERTERS_DWA2002124_HPP