Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/test/test_builtin_converters.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 5.4 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#include <boost/python/module.hpp>
6#include <boost/python/def.hpp>
7#include <complex>
8#include <boost/python/handle.hpp>
9#include <boost/python/cast.hpp>
10#include <boost/python/object.hpp>
11#include <boost/python/detail/wrap_python.hpp>
12
13template <class T>
14struct by_value
15{
16    static T rewrap(T x)
17    {
18        return x;
19    }
20};
21
22template <class T>
23struct by_const_reference
24{
25    static T rewrap(T const& x)
26    {
27        return x;
28    }
29};
30
31template <class T>
32struct by_reference
33{
34    static T rewrap(T& x)
35    {
36        return x;
37    }
38};
39
40using boost::python::def;
41using boost::python::handle;
42using boost::python::object;
43using boost::python::borrowed;
44
45// Used to test that arbitrary handle<>s can be returned
46handle<PyTypeObject> get_type(handle<> x)
47{
48    return handle<PyTypeObject>(borrowed(x->ob_type));
49}
50
51handle<> return_null_handle()
52{
53    return handle<>();
54}
55
56char const* rewrap_value_mutable_cstring(char* x) { return x; }
57
58object identity_(object x) { return x; }
59
60BOOST_PYTHON_MODULE(builtin_converters)
61{   
62    def("get_type", get_type);
63    def("return_null_handle", return_null_handle);
64       
65    def("rewrap_value_bool", by_value<bool>::rewrap);
66    def("rewrap_value_char", by_value<char>::rewrap);
67    def("rewrap_value_signed_char", by_value<signed char>::rewrap);
68    def("rewrap_value_unsigned_char", by_value<unsigned char>::rewrap);
69    def("rewrap_value_int", by_value<int>::rewrap);
70    def("rewrap_value_unsigned_int", by_value<unsigned int>::rewrap);
71    def("rewrap_value_short", by_value<short>::rewrap);
72    def("rewrap_value_unsigned_short", by_value<unsigned short>::rewrap);
73    def("rewrap_value_long", by_value<long>::rewrap);
74    def("rewrap_value_unsigned_long", by_value<unsigned long>::rewrap);
75// using Python's macro instead of Boost's - we don't seem to get the
76// config right all the time.
77#ifdef HAVE_LONG_LONG
78    def("rewrap_value_long_long", by_value<BOOST_PYTHON_LONG_LONG>::rewrap);
79    def("rewrap_value_unsigned_long_long", by_value<unsigned BOOST_PYTHON_LONG_LONG>::rewrap);
80# endif
81    def("rewrap_value_float", by_value<float>::rewrap);
82    def("rewrap_value_double", by_value<double>::rewrap);
83    def("rewrap_value_long_double", by_value<long double>::rewrap);
84    def("rewrap_value_complex_float", by_value<std::complex<float> >::rewrap);
85    def("rewrap_value_complex_double", by_value<std::complex<double> >::rewrap);
86    def("rewrap_value_complex_long_double", by_value<std::complex<long double> >::rewrap);
87    def("rewrap_value_wstring",
88# if defined(BOOST_NO_STD_WSTRING) || !defined(Py_USING_UNICODE)
89        identity_
90# else
91        by_value<std::wstring>::rewrap
92# endif
93    );
94    def("rewrap_value_string",
95# if defined(BOOST_NO_STD_WSTRING) || !defined(Py_USING_UNICODE)
96        identity_
97# else
98        by_value<std::wstring>::rewrap
99# endif
100    );
101    def("rewrap_value_string", by_value<std::string>::rewrap);
102    def("rewrap_value_cstring", by_value<char const*>::rewrap);
103    def("rewrap_value_handle", by_value<handle<> >::rewrap);
104    def("rewrap_value_object", by_value<object>::rewrap);
105
106        // Expose this to illustrate our failings ;-). See test_builtin_converters.py
107    def("rewrap_value_mutable_cstring", rewrap_value_mutable_cstring);
108
109
110    def("rewrap_const_reference_bool", by_const_reference<bool>::rewrap);
111    def("rewrap_const_reference_char", by_const_reference<char>::rewrap);
112    def("rewrap_const_reference_signed_char", by_const_reference<signed char>::rewrap);
113    def("rewrap_const_reference_unsigned_char", by_const_reference<unsigned char>::rewrap);
114    def("rewrap_const_reference_int", by_const_reference<int>::rewrap);
115    def("rewrap_const_reference_unsigned_int", by_const_reference<unsigned int>::rewrap);
116    def("rewrap_const_reference_short", by_const_reference<short>::rewrap);
117    def("rewrap_const_reference_unsigned_short", by_const_reference<unsigned short>::rewrap);
118    def("rewrap_const_reference_long", by_const_reference<long>::rewrap);
119    def("rewrap_const_reference_unsigned_long", by_const_reference<unsigned long>::rewrap);
120// using Python's macro instead of Boost's - we don't seem to get the
121// config right all the time.
122# ifdef HAVE_LONG_LONG
123    def("rewrap_const_reference_long_long", by_const_reference<BOOST_PYTHON_LONG_LONG>::rewrap);
124    def("rewrap_const_reference_unsigned_long_long", by_const_reference<unsigned BOOST_PYTHON_LONG_LONG>::rewrap);
125# endif
126    def("rewrap_const_reference_float", by_const_reference<float>::rewrap);
127    def("rewrap_const_reference_double", by_const_reference<double>::rewrap);
128    def("rewrap_const_reference_long_double", by_const_reference<long double>::rewrap);
129    def("rewrap_const_reference_complex_float", by_const_reference<std::complex<float> >::rewrap);
130    def("rewrap_const_reference_complex_double", by_const_reference<std::complex<double> >::rewrap);
131    def("rewrap_const_reference_complex_long_double", by_const_reference<std::complex<long double> >::rewrap);
132    def("rewrap_const_reference_string", by_const_reference<std::string>::rewrap);
133    def("rewrap_const_reference_cstring", by_const_reference<char const*>::rewrap);
134    def("rewrap_const_reference_handle", by_const_reference<handle<> >::rewrap);
135    def("rewrap_const_reference_object", by_const_reference<object>::rewrap);
136    def("rewrap_reference_object", by_reference<object>::rewrap);
137}
138
Note: See TracBrowser for help on using the repository browser.