Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/python/lvalue_from_pytype.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 LVALUE_FROM_PYTYPE_DWA2002130_HPP
0006 # define LVALUE_FROM_PYTYPE_DWA2002130_HPP
0007 
0008 # include <boost/python/detail/prefix.hpp>
0009 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
0010 # include <boost/python/converter/pytype_function.hpp>
0011 #endif
0012 
0013 # include <boost/python/type_id.hpp>
0014 # include <boost/python/converter/registry.hpp>
0015 # include <boost/python/detail/void_ptr.hpp>
0016 # include <boost/python/detail/type_traits.hpp>
0017 
0018 namespace boost { namespace python {
0019 
0020 namespace detail
0021 {
0022   // Given a pointer-to-function of 1 parameter returning a reference
0023   // type, return the type_id of the function's return type.
0024   template <class T, class U>
0025   inline type_info extractor_type_id(T&(*)(U))
0026   {
0027       return type_id<T>();
0028   }
0029 
0030   // A function generator whose static execute() function is an lvalue
0031   // from_python converter using the given Extractor. U is expected to
0032   // be the actual type of the PyObject instance from which the result
0033   // is being extracted.
0034   template <class Extractor, class U>
0035   struct normalized_extractor
0036   {
0037       static inline void* execute(PyObject* op)
0038       {
0039           typedef typename add_lvalue_reference<U>::type param;
0040           return &Extractor::execute(
0041               boost::python::detail::void_ptr_to_reference(
0042                   op, (param(*)())0 )
0043               );
0044       }
0045   };
0046 
0047   // Given an Extractor type and a pointer to its execute function,
0048   // return a new object whose static execute function does the same
0049   // job but is a conforming lvalue from_python conversion function.
0050   //
0051   // usage: normalize<Extractor>(&Extractor::execute)
0052   template <class Extractor, class T, class U>
0053   inline normalized_extractor<Extractor,U>
0054   normalize(T(*)(U), Extractor* = 0)
0055   {
0056       return normalized_extractor<Extractor, U>();
0057   }
0058 }
0059 
0060 // An Extractor which extracts the given member from a Python object
0061 // whose instances are stored as InstanceType.
0062 template <class InstanceType, class MemberType, MemberType (InstanceType::*member)>
0063 struct extract_member
0064 {
0065     static MemberType& execute(InstanceType& c)
0066     {
0067         (void)Py_TYPE(&c); // static assertion
0068         return c.*member;
0069     }
0070 };
0071 
0072 // An Extractor which simply extracts the entire python object
0073 // instance of InstanceType.
0074 template <class InstanceType>
0075 struct extract_identity
0076 {
0077     static InstanceType& execute(InstanceType& c)
0078     {
0079         (void)Py_TYPE(&c); // static assertion
0080         return c;
0081     }
0082 };
0083 
0084 // Registers a from_python conversion which extracts lvalues using
0085 // Extractor's static execute function from Python objects whose type
0086 // object is python_type.
0087 template <class Extractor, PyTypeObject const* python_type>
0088 struct lvalue_from_pytype 
0089 {
0090     lvalue_from_pytype()
0091     {
0092         converter::registry::insert
0093             ( &extract
0094             , detail::extractor_type_id(&Extractor::execute)
0095 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
0096             , &get_pytype
0097 #endif
0098             );
0099     }
0100  private:
0101     static void* extract(PyObject* op)
0102     {
0103         return PyObject_TypeCheck(op, const_cast<PyTypeObject*>(python_type))
0104             ? const_cast<void*>(
0105                 static_cast<void const volatile*>(
0106                     detail::normalize<Extractor>(&Extractor::execute).execute(op)))
0107             : 0
0108             ;
0109     }
0110 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
0111     static PyTypeObject const*get_pytype() { return python_type; }
0112 #endif
0113 };
0114 
0115 }} // namespace boost::python
0116 
0117 #endif // LVALUE_FROM_PYTYPE_DWA2002130_HPP