File indexing completed on 2025-01-18 09:50:37
0001
0002
0003
0004
0005 #ifndef BUILTIN_CONVERTERS_DWA2002124_HPP
0006 # define BUILTIN_CONVERTERS_DWA2002124_HPP
0007 # include <boost/python/detail/prefix.hpp>
0008 # include <boost/python/detail/none.hpp>
0009 # include <boost/python/handle.hpp>
0010 # include <boost/python/ssize_t.hpp>
0011 # include <boost/implicit_cast.hpp>
0012 # include <string>
0013 # include <complex>
0014 # include <boost/limits.hpp>
0015
0016
0017
0018
0019
0020
0021
0022 namespace boost { namespace python {
0023
0024 namespace converter
0025 {
0026 template <class T> struct arg_to_python;
0027 BOOST_PYTHON_DECL PyObject* do_return_to_python(char);
0028 BOOST_PYTHON_DECL PyObject* do_return_to_python(char const*);
0029 BOOST_PYTHON_DECL PyObject* do_return_to_python(PyObject*);
0030 BOOST_PYTHON_DECL PyObject* do_arg_to_python(PyObject*);
0031 }
0032
0033
0034 template <class T> struct to_python_value;
0035
0036 namespace detail
0037 {
0038
0039
0040 struct builtin_to_python
0041 {
0042
0043
0044
0045 BOOST_STATIC_CONSTANT(bool, uses_registry = false);
0046 };
0047 }
0048
0049
0050 # define BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T, expr, pytype)\
0051 template <> struct to_python_value<T&> \
0052 : detail::builtin_to_python \
0053 { \
0054 inline PyObject* operator()(T const& x) const \
0055 { \
0056 return (expr); \
0057 } \
0058 inline PyTypeObject const* get_pytype() const \
0059 { \
0060 return (pytype); \
0061 } \
0062 }; \
0063 template <> struct to_python_value<T const&> \
0064 : detail::builtin_to_python \
0065 { \
0066 inline PyObject* operator()(T const& x) const \
0067 { \
0068 return (expr); \
0069 } \
0070 inline PyTypeObject const* get_pytype() const \
0071 { \
0072 return (pytype); \
0073 } \
0074 };
0075
0076 # define BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T, expr) \
0077 namespace converter \
0078 { \
0079 template <> struct arg_to_python< T > \
0080 : handle<> \
0081 { \
0082 arg_to_python(T const& x) \
0083 : python::handle<>(expr) {} \
0084 }; \
0085 }
0086
0087
0088 # define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr, pytype) \
0089 BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T,expr, pytype) \
0090 BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T,expr)
0091
0092
0093 #if PY_VERSION_HEX >= 0x03000000
0094
0095 # define BOOST_PYTHON_TO_INT(T) \
0096 BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, ::PyLong_FromLong(x), &PyLong_Type) \
0097 BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned T, ::PyLong_FromUnsignedLong(x), &PyLong_Type)
0098
0099 #else
0100
0101 # define BOOST_PYTHON_TO_INT(T) \
0102 BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, ::PyInt_FromLong(x), &PyInt_Type) \
0103 BOOST_PYTHON_TO_PYTHON_BY_VALUE( \
0104 unsigned T \
0105 , static_cast<unsigned long>(x) > static_cast<unsigned long>( \
0106 (std::numeric_limits<long>::max)()) \
0107 ? ::PyLong_FromUnsignedLong(x) \
0108 : ::PyInt_FromLong(x), &PyInt_Type)
0109 #endif
0110
0111
0112 #if PY_VERSION_HEX >= 0x02030000
0113 BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyBool_FromLong(x), &PyBool_Type)
0114 #else
0115 BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyInt_FromLong(x), &PyInt_Type)
0116 #endif
0117
0118
0119 BOOST_PYTHON_TO_INT(char)
0120
0121 BOOST_PYTHON_TO_INT(short)
0122 BOOST_PYTHON_TO_INT(int)
0123 BOOST_PYTHON_TO_INT(long)
0124
0125 # if defined(_MSC_VER) && defined(_WIN64) && PY_VERSION_HEX < 0x03000000
0126
0127
0128
0129
0130
0131 BOOST_PYTHON_TO_PYTHON_BY_VALUE(
0132 signed BOOST_PYTHON_LONG_LONG,
0133 ( x < static_cast<signed BOOST_PYTHON_LONG_LONG>(
0134 (std::numeric_limits<long>::min)())
0135 || x > static_cast<signed BOOST_PYTHON_LONG_LONG>(
0136 (std::numeric_limits<long>::max)()))
0137 ? ::PyLong_FromLongLong(x)
0138 : ::PyInt_FromLong(static_cast<long>(x)), &PyInt_Type)
0139 BOOST_PYTHON_TO_PYTHON_BY_VALUE(
0140 unsigned BOOST_PYTHON_LONG_LONG,
0141 x > static_cast<unsigned BOOST_PYTHON_LONG_LONG>(
0142 (std::numeric_limits<long>::max)())
0143 ? ::PyLong_FromUnsignedLongLong(x)
0144 : ::PyInt_FromLong(static_cast<long>(x)), &PyInt_Type)
0145
0146 # elif defined(HAVE_LONG_LONG)
0147
0148
0149 BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed BOOST_PYTHON_LONG_LONG, ::PyLong_FromLongLong(x), &PyLong_Type)
0150 BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned BOOST_PYTHON_LONG_LONG, ::PyLong_FromUnsignedLongLong(x), &PyLong_Type)
0151 # endif
0152
0153 # undef BOOST_TO_PYTHON_INT
0154
0155 #if PY_VERSION_HEX >= 0x03000000
0156 BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x), &PyUnicode_Type)
0157 BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x), &PyUnicode_Type)
0158 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyUnicode_FromStringAndSize(x.data(),implicit_cast<ssize_t>(x.size())), &PyUnicode_Type)
0159 #else
0160 BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x), &PyString_Type)
0161 BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x), &PyString_Type)
0162 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<ssize_t>(x.size())), &PyString_Type)
0163 #endif
0164
0165 #if defined(Py_USING_UNICODE) && !defined(BOOST_NO_STD_WSTRING)
0166 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<ssize_t>(x.size())), &PyUnicode_Type)
0167 # endif
0168 BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, ::PyFloat_FromDouble(x), &PyFloat_Type)
0169 BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, ::PyFloat_FromDouble(x), &PyFloat_Type)
0170 BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, ::PyFloat_FromDouble(x), &PyFloat_Type)
0171 BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(PyObject*, converter::do_return_to_python(x), 0)
0172 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<float>, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type)
0173 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<double>, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type)
0174 BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<long double>, ::PyComplex_FromDoubles(x.real(), x.imag()), &PyComplex_Type)
0175
0176 # undef BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE
0177 # undef BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE
0178 # undef BOOST_PYTHON_TO_PYTHON_BY_VALUE
0179 # undef BOOST_PYTHON_TO_INT
0180
0181 namespace converter
0182 {
0183
0184 void initialize_builtin_converters();
0185
0186 }
0187
0188 }}
0189
0190 #endif