Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:37

0001 // Copyright David Abrahams 2002.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef AS_TO_PYTHON_FUNCTION_DWA2002121_HPP
0006 # define AS_TO_PYTHON_FUNCTION_DWA2002121_HPP
0007 # include <boost/python/converter/to_python_function_type.hpp>
0008 
0009 namespace boost { namespace python { namespace converter { 
0010 
0011 // Given a typesafe to_python conversion function, produces a
0012 // to_python_function_t which can be registered in the usual way.
0013 template <class T, class ToPython>
0014 struct as_to_python_function
0015 {
0016     // Assertion functions used to prevent wrapping of converters
0017     // which take non-const reference parameters. The T* argument in
0018     // the first overload ensures it isn't used in case T is a
0019     // reference.
0020     template <class U>
0021     static void convert_function_must_take_value_or_const_reference(U(*)(T), int, T* = 0) {}
0022     template <class U>
0023     static void convert_function_must_take_value_or_const_reference(U(*)(T const&), long ...) {}
0024         
0025     static PyObject* convert(void const* x)
0026     {
0027         convert_function_must_take_value_or_const_reference(&ToPython::convert, 1L);
0028         
0029         // Yes, the const_cast below opens a hole in const-correctness,
0030         // but it's needed to convert auto_ptr<U> to python.
0031         //
0032         // How big a hole is it?  It allows ToPython::convert() to be
0033         // a function which modifies its argument. The upshot is that
0034         // client converters applied to const objects may invoke
0035         // undefined behavior. The damage, however, is limited by the
0036         // use of the assertion function. Thus, the only way this can
0037         // modify its argument is if T is an auto_ptr-like type. There
0038         // is still a const-correctness hole w.r.t. auto_ptr<U> const,
0039         // but c'est la vie.
0040         return ToPython::convert(*const_cast<T*>(static_cast<T const*>(x)));
0041     }
0042 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
0043     static PyTypeObject const * get_pytype() { return ToPython::get_pytype(); }
0044 #endif
0045 };
0046 
0047 }}} // namespace boost::python::converter
0048 
0049 #endif // AS_TO_PYTHON_FUNCTION_DWA2002121_HPP