Chris@16: // Copyright David Abrahams 2002. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: #ifndef AS_TO_PYTHON_FUNCTION_DWA2002121_HPP Chris@16: # define AS_TO_PYTHON_FUNCTION_DWA2002121_HPP Chris@16: # include Chris@16: Chris@16: namespace boost { namespace python { namespace converter { Chris@16: Chris@16: // Given a typesafe to_python conversion function, produces a Chris@16: // to_python_function_t which can be registered in the usual way. Chris@16: template Chris@16: struct as_to_python_function Chris@16: { Chris@16: // Assertion functions used to prevent wrapping of converters Chris@16: // which take non-const reference parameters. The T* argument in Chris@16: // the first overload ensures it isn't used in case T is a Chris@16: // reference. Chris@16: template Chris@16: static void convert_function_must_take_value_or_const_reference(U(*)(T), int, T* = 0) {} Chris@16: template Chris@16: static void convert_function_must_take_value_or_const_reference(U(*)(T const&), long ...) {} Chris@16: Chris@16: static PyObject* convert(void const* x) Chris@16: { Chris@16: convert_function_must_take_value_or_const_reference(&ToPython::convert, 1L); Chris@16: Chris@16: // Yes, the const_cast below opens a hole in const-correctness, Chris@16: // but it's needed to convert auto_ptr to python. Chris@16: // Chris@16: // How big a hole is it? It allows ToPython::convert() to be Chris@16: // a function which modifies its argument. The upshot is that Chris@16: // client converters applied to const objects may invoke Chris@16: // undefined behavior. The damage, however, is limited by the Chris@16: // use of the assertion function. Thus, the only way this can Chris@16: // modify its argument is if T is an auto_ptr-like type. There Chris@16: // is still a const-correctness hole w.r.t. auto_ptr const, Chris@16: // but c'est la vie. Chris@16: return ToPython::convert(*const_cast(static_cast(x))); Chris@16: } Chris@16: #ifndef BOOST_PYTHON_NO_PY_SIGNATURES Chris@16: static PyTypeObject const * get_pytype() { return ToPython::get_pytype(); } Chris@16: #endif Chris@16: }; Chris@16: Chris@16: }}} // namespace boost::python::converter Chris@16: Chris@16: #endif // AS_TO_PYTHON_FUNCTION_DWA2002121_HPP