Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/python/converter/builtin_converters.hpp @ 35

Last change on this file since 35 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 6.1 KB
Line 
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
22namespace boost { namespace python {
23
24namespace 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
34template <class T> struct to_python_value;
35
36namespace 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)        \
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    };                                                          \
59    template <> struct to_python_value<T const&>                \
60        : detail::builtin_to_python                             \
61    {                                                           \
62        inline PyObject* operator()(T const& x) const           \
63        {                                                       \
64            return (expr);                                      \
65        }                                                       \
66    };
67
68# define BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T, expr)   \
69    namespace converter                                 \
70    {                                                   \
71      template <> struct arg_to_python< T >             \
72        : handle<>                                      \
73      {                                                 \
74          arg_to_python(T const& x)                     \
75            : python::handle<>(expr) {}                 \
76      };                                                \
77    }
78
79// Specialize argument and return value converters for T using expr
80# define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr)       \
81        BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(T,expr)  \
82        BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE(T,expr)
83
84// Specialize converters for signed and unsigned T to Python Int
85# define BOOST_PYTHON_TO_INT(T)                                         \
86    BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, ::PyInt_FromLong(x))      \
87    BOOST_PYTHON_TO_PYTHON_BY_VALUE(                                    \
88        unsigned T                                                      \
89        , static_cast<unsigned long>(x) > static_cast<unsigned long>(   \
90                (std::numeric_limits<long>::max)())                     \
91        ? ::PyLong_FromUnsignedLong(x)                                  \
92        : ::PyInt_FromLong(x))
93
94// Bool is not signed.
95#if PY_VERSION_HEX >= 0x02030000
96BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyBool_FromLong(x))
97#else
98BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyInt_FromLong(x))
99#endif
100 
101// note: handles signed char and unsigned char, but not char (see below)
102BOOST_PYTHON_TO_INT(char)
103
104BOOST_PYTHON_TO_INT(short)
105BOOST_PYTHON_TO_INT(int)
106BOOST_PYTHON_TO_INT(long)
107
108// using Python's macro instead of Boost's - we don't seem to get the
109// config right all the time.
110# ifdef HAVE_LONG_LONG
111BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed BOOST_PYTHON_LONG_LONG, ::PyLong_FromLongLong(x))
112BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned BOOST_PYTHON_LONG_LONG, ::PyLong_FromUnsignedLongLong(x))
113# endif
114   
115# undef BOOST_TO_PYTHON_INT
116
117BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x))
118BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x))
119BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<ssize_t>(x.size())))
120#if defined(Py_USING_UNICODE) && !defined(BOOST_NO_STD_WSTRING)
121BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<ssize_t>(x.size())))
122# endif
123BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, ::PyFloat_FromDouble(x))
124BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, ::PyFloat_FromDouble(x))
125BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, ::PyFloat_FromDouble(x))
126BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE(PyObject*, converter::do_return_to_python(x))
127BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<float>, ::PyComplex_FromDoubles(x.real(), x.imag()))
128BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<double>, ::PyComplex_FromDoubles(x.real(), x.imag()))
129BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<long double>, ::PyComplex_FromDoubles(x.real(), x.imag()))
130
131# undef BOOST_PYTHON_RETURN_TO_PYTHON_BY_VALUE
132# undef BOOST_PYTHON_ARG_TO_PYTHON_BY_VALUE
133# undef BOOST_PYTHON_TO_PYTHON_BY_VALUE
134# undef BOOST_PYTHON_TO_INT
135   
136namespace converter
137{ 
138
139  void initialize_builtin_converters();
140
141}
142
143}} // namespace boost::python::converter
144
145#endif // BUILTIN_CONVERTERS_DWA2002124_HPP
Note: See TracBrowser for help on using the repository browser.