Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:37

0001 // Copyright David Abrahams 2002.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
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 // Since all we can use to decide how to convert an object to_python
0017 // is its C++ type, there can be only one such converter for each
0018 // type. Therefore, for built-in conversions we can bypass registry
0019 // lookups using explicit specializations of arg_to_python and
0020 // result_to_python.
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 // Provide specializations of to_python_value
0034 template <class T> struct to_python_value;
0035 
0036 namespace detail
0037 {
0038   // Since there's no registry lookup, always report the existence of
0039   // a converter.
0040   struct builtin_to_python
0041   {
0042       // This information helps make_getter() decide whether to try to
0043       // return an internal reference or not. I don't like it much,
0044       // but it will have to serve for now.
0045       BOOST_STATIC_CONSTANT(bool, uses_registry = false);
0046   };
0047 }
0048 
0049 // Use expr to create the PyObject corresponding to x
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 // Specialize argument and return value converters for T using expr
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 // Specialize converters for signed and unsigned T to Python Int
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 // Bool is not signed.
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 // note: handles signed char and unsigned char, but not char (see below)
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 /* Under 64-bit Windows std::size_t is "unsigned long long". To avoid
0127    getting a Python long for each std::size_t the value is checked before
0128    the conversion. A std::size_t is converted to a simple Python int
0129    if possible; a Python long appears only if the value is too small or
0130    too large to fit into a simple int. */
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) // using Python's macro instead of Boost's
0147                                // - we don't seem to get the config right
0148                                // all the time.
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 }} // namespace boost::python::converter
0189 
0190 #endif // BUILTIN_CONVERTERS_DWA2002124_HPP