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 TO_PYTHON_CONVERTER_DWA200221_HPP
6# define TO_PYTHON_CONVERTER_DWA200221_HPP
7
8# include <boost/python/detail/prefix.hpp>
9
10# include <boost/python/converter/registry.hpp>
11# include <boost/python/converter/as_to_python_function.hpp>
12#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
13# include <boost/python/converter/pytype_function.hpp>
14#endif
15# include <boost/python/type_id.hpp>
16
17namespace boost { namespace python {
18
19#if 0 //get_pytype member detection
20namespace detail
21{
22 typedef char yes_type;
23 typedef struct {char a[2]; } no_type;
24 template<PyTypeObject const * (*f)()> struct test_get_pytype1 { };
25 template<PyTypeObject * (*f)()> struct test_get_pytype2 { };
26
27 template<class T> yes_type tester(test_get_pytype1<&T::get_pytype>*);
28
29 template<class T> yes_type tester(test_get_pytype2<&T::get_pytype>*);
30
31 template<class T> no_type tester(...);
32
33 template<class T>
34 struct test_get_pytype_base
35 {
36 BOOST_STATIC_CONSTANT(bool, value= (sizeof(detail::tester<T>(0)) == sizeof(yes_type)));
37 };
38
39 template<class T>
40 struct test_get_pytype : boost::mpl::bool_<test_get_pytype_base<T>::value>
41 {
42 };
43
44}
45#endif
46
47template < class T, class Conversion, bool has_get_pytype=false >
48struct to_python_converter
49{
50#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
51 typedef boost::mpl::bool_<has_get_pytype> HasGetPytype;
52
53 static PyTypeObject const* get_pytype_1(boost::mpl::true_ *)
54 {
55 return Conversion::get_pytype();
56 }
57
58 static PyTypeObject const* get_pytype_1(boost::mpl::false_ *)
59 {
60 return 0;
61 }
62 static PyTypeObject const* get_pytype_impl()
63 {
64 return get_pytype_1((HasGetPytype*)0);
65 }
66#endif
67
68 to_python_converter();
69};
70
71//
72// implementation
73//
74
75template <class T, class Conversion ,bool has_get_pytype>
76to_python_converter<T,Conversion, has_get_pytype>::to_python_converter()
77{
78 typedef converter::as_to_python_function<
79 T, Conversion
80 > normalized;
81
82 converter::registry::insert(
83 &normalized::convert
84 , type_id<T>()
85#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
86 , &get_pytype_impl
87#endif
88 );
89}
90
91}} // namespace boost::python
92
93#endif // TO_PYTHON_CONVERTER_DWA200221_HPP
94
95

source code of boost/boost/python/to_python_converter.hpp