builtin_converters.hpp 8.6 KB

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