Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:07:40

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 OBJ_MGR_ARG_FROM_PYTHON_DWA2002628_HPP
0006 # define OBJ_MGR_ARG_FROM_PYTHON_DWA2002628_HPP
0007 
0008 # include <boost/python/detail/prefix.hpp>
0009 # include <boost/python/detail/referent_storage.hpp>
0010 # include <boost/python/detail/destroy.hpp>
0011 # include <boost/python/detail/construct.hpp>
0012 # include <boost/python/converter/object_manager.hpp>
0013 # include <boost/python/detail/raw_pyobject.hpp>
0014 # include <boost/python/tag.hpp>
0015 
0016 //
0017 // arg_from_python converters for Python type wrappers, to be used as
0018 // base classes for specializations.
0019 //
0020 namespace boost { namespace python { namespace converter { 
0021 
0022 template <class T>
0023 struct object_manager_value_arg_from_python
0024 {
0025     typedef T result_type;
0026     
0027     object_manager_value_arg_from_python(PyObject*);
0028     bool convertible() const;
0029     T operator()() const;
0030  private:
0031     PyObject* m_source;
0032 };
0033 
0034 // Used for converting reference-to-object-manager arguments from
0035 // python. The process used here is a little bit odd. Upon
0036 // construction, we build the object manager object in the m_result
0037 // object, *forcing* it to accept the source Python object by casting
0038 // its pointer to detail::borrowed_reference. This is supposed to
0039 // bypass any type checking of the source object. The convertible
0040 // check then extracts the owned object and checks it. If the check
0041 // fails, nothing else in the program ever gets to touch this strange
0042 // "forced" object.
0043 template <class Ref>
0044 struct object_manager_ref_arg_from_python
0045 {
0046     typedef Ref result_type;
0047     
0048     object_manager_ref_arg_from_python(PyObject*);
0049     bool convertible() const;
0050     Ref operator()() const;
0051     ~object_manager_ref_arg_from_python();
0052  private:
0053     typename python::detail::referent_storage<Ref>::type m_result;
0054 };
0055 
0056 //
0057 // implementations
0058 //
0059 
0060 template <class T>
0061 inline object_manager_value_arg_from_python<T>::object_manager_value_arg_from_python(PyObject* x)
0062     : m_source(x)
0063 {
0064 }
0065     
0066 template <class T>
0067 inline bool object_manager_value_arg_from_python<T>::convertible() const
0068 {
0069     return object_manager_traits<T>::check(m_source);
0070 }
0071 
0072 template <class T>
0073 inline T object_manager_value_arg_from_python<T>::operator()() const
0074 {
0075     return T(python::detail::borrowed_reference(m_source));
0076 }
0077 
0078 template <class Ref>
0079 inline object_manager_ref_arg_from_python<Ref>::object_manager_ref_arg_from_python(PyObject* x)
0080 {
0081 # if defined(__EDG_VERSION__) && __EDG_VERSION__ <= 243
0082     // needed for warning suppression
0083     python::detail::borrowed_reference x_ = python::detail::borrowed_reference(x);
0084     python::detail::construct_referent<Ref>(m_result.bytes, x_);
0085 # else 
0086     python::detail::construct_referent<Ref>(m_result.bytes, (python::detail::borrowed_reference)x);
0087 # endif 
0088 }
0089 
0090 template <class Ref>
0091 inline object_manager_ref_arg_from_python<Ref>::~object_manager_ref_arg_from_python()
0092 {
0093     python::detail::destroy_referent<Ref>(this->m_result.bytes);
0094 }
0095 
0096 namespace detail
0097 {
0098   template <class T>
0099   inline bool object_manager_ref_check(T const& x)
0100   {
0101       return object_manager_traits<T>::check(get_managed_object(x, tag));
0102   }
0103 }
0104 
0105 template <class Ref>
0106 inline bool object_manager_ref_arg_from_python<Ref>::convertible() const
0107 {
0108     return detail::object_manager_ref_check(
0109         python::detail::void_ptr_to_reference(this->m_result.bytes, (Ref(*)())0));
0110 }
0111 
0112 template <class Ref>
0113 inline Ref object_manager_ref_arg_from_python<Ref>::operator()() const
0114 {
0115     return python::detail::void_ptr_to_reference(
0116         this->m_result.bytes, (Ref(*)())0);
0117 }
0118 
0119 }}} // namespace boost::python::converter
0120 
0121 #endif // OBJ_MGR_ARG_FROM_PYTHON_DWA2002628_HPP