Chris@16
|
1 // Copyright David Abrahams 2002.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 #ifndef AS_TO_PYTHON_FUNCTION_DWA2002121_HPP
|
Chris@16
|
6 # define AS_TO_PYTHON_FUNCTION_DWA2002121_HPP
|
Chris@16
|
7 # include <boost/python/converter/to_python_function_type.hpp>
|
Chris@16
|
8
|
Chris@16
|
9 namespace boost { namespace python { namespace converter {
|
Chris@16
|
10
|
Chris@16
|
11 // Given a typesafe to_python conversion function, produces a
|
Chris@16
|
12 // to_python_function_t which can be registered in the usual way.
|
Chris@16
|
13 template <class T, class ToPython>
|
Chris@16
|
14 struct as_to_python_function
|
Chris@16
|
15 {
|
Chris@16
|
16 // Assertion functions used to prevent wrapping of converters
|
Chris@16
|
17 // which take non-const reference parameters. The T* argument in
|
Chris@16
|
18 // the first overload ensures it isn't used in case T is a
|
Chris@16
|
19 // reference.
|
Chris@16
|
20 template <class U>
|
Chris@16
|
21 static void convert_function_must_take_value_or_const_reference(U(*)(T), int, T* = 0) {}
|
Chris@16
|
22 template <class U>
|
Chris@16
|
23 static void convert_function_must_take_value_or_const_reference(U(*)(T const&), long ...) {}
|
Chris@16
|
24
|
Chris@16
|
25 static PyObject* convert(void const* x)
|
Chris@16
|
26 {
|
Chris@16
|
27 convert_function_must_take_value_or_const_reference(&ToPython::convert, 1L);
|
Chris@16
|
28
|
Chris@16
|
29 // Yes, the const_cast below opens a hole in const-correctness,
|
Chris@16
|
30 // but it's needed to convert auto_ptr<U> to python.
|
Chris@16
|
31 //
|
Chris@16
|
32 // How big a hole is it? It allows ToPython::convert() to be
|
Chris@16
|
33 // a function which modifies its argument. The upshot is that
|
Chris@16
|
34 // client converters applied to const objects may invoke
|
Chris@16
|
35 // undefined behavior. The damage, however, is limited by the
|
Chris@16
|
36 // use of the assertion function. Thus, the only way this can
|
Chris@16
|
37 // modify its argument is if T is an auto_ptr-like type. There
|
Chris@16
|
38 // is still a const-correctness hole w.r.t. auto_ptr<U> const,
|
Chris@16
|
39 // but c'est la vie.
|
Chris@16
|
40 return ToPython::convert(*const_cast<T*>(static_cast<T const*>(x)));
|
Chris@16
|
41 }
|
Chris@16
|
42 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
|
Chris@16
|
43 static PyTypeObject const * get_pytype() { return ToPython::get_pytype(); }
|
Chris@16
|
44 #endif
|
Chris@16
|
45 };
|
Chris@16
|
46
|
Chris@16
|
47 }}} // namespace boost::python::converter
|
Chris@16
|
48
|
Chris@16
|
49 #endif // AS_TO_PYTHON_FUNCTION_DWA2002121_HPP
|