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 // Copyright Stefan Seefeld 2016.
0003 // Distributed under the Boost Software License, Version 1.0. (See
0004 // accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef boost_python_converter_shared_ptr_from_python_hpp_
0008 #define boost_python_converter_shared_ptr_from_python_hpp_
0009 
0010 #include <boost/python/handle.hpp>
0011 #include <boost/python/converter/shared_ptr_deleter.hpp>
0012 #include <boost/python/converter/from_python.hpp>
0013 #include <boost/python/converter/rvalue_from_python_data.hpp>
0014 #include <boost/python/converter/registered.hpp>
0015 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
0016 # include <boost/python/converter/pytype_function.hpp>
0017 #endif
0018 #include <boost/shared_ptr.hpp>
0019 #include <memory>
0020 
0021 namespace boost { namespace python { namespace converter { 
0022 
0023 template <class T, template <typename> class SP>
0024 struct shared_ptr_from_python
0025 {
0026   shared_ptr_from_python()
0027   {
0028     converter::registry::insert(&convertible, &construct, type_id<SP<T> >()
0029 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
0030                 , &converter::expected_from_python_type_direct<T>::get_pytype
0031 #endif
0032                 );
0033   }
0034 
0035  private:
0036   static void* convertible(PyObject* p)
0037   {
0038     if (p == Py_None)
0039       return p;
0040         
0041     return converter::get_lvalue_from_python(p, registered<T>::converters);
0042   }
0043     
0044   static void construct(PyObject* source, rvalue_from_python_stage1_data* data)
0045   {
0046     void* const storage = ((converter::rvalue_from_python_storage<SP<T> >*)data)->storage.bytes;
0047     // Deal with the "None" case.
0048     if (data->convertible == source)
0049       new (storage) SP<T>();
0050     else
0051     {
0052       void *const storage = ((converter::rvalue_from_python_storage<SP<T> >*)data)->storage.bytes;
0053       // Deal with the "None" case.
0054       if (data->convertible == source)
0055         new (storage) SP<T>();
0056       else
0057       {
0058         SP<void> hold_convertible_ref_count((void*)0, shared_ptr_deleter(handle<>(borrowed(source))) );
0059         // use aliasing constructor
0060         new (storage) SP<T>(hold_convertible_ref_count, static_cast<T*>(data->convertible));
0061       }
0062     }
0063     data->convertible = storage;
0064   }
0065 };
0066 
0067 }}} // namespace boost::python::converter
0068 
0069 #endif