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