source: NonGTP/Boost/boost/python/converter/shared_ptr_from_python.hpp @ 857

Revision 857, 1.7 KB checked in by igarcia, 19 years ago (diff)
Line 
1// Copyright David Abrahams 2002.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5#ifndef SHARED_PTR_FROM_PYTHON_DWA20021130_HPP
6# define SHARED_PTR_FROM_PYTHON_DWA20021130_HPP
7
8# include <boost/python/handle.hpp>
9# include <boost/python/converter/shared_ptr_deleter.hpp>
10# include <boost/python/converter/from_python.hpp>
11# include <boost/python/converter/rvalue_from_python_data.hpp>
12# include <boost/python/converter/registered.hpp>
13# include <boost/shared_ptr.hpp>
14
15namespace boost { namespace python { namespace converter {
16
17template <class T>
18struct shared_ptr_from_python
19{
20    shared_ptr_from_python()
21    {
22        converter::registry::insert(&convertible, &construct, type_id<shared_ptr<T> >());
23    }
24
25 private:
26    static void* convertible(PyObject* p)
27    {
28        if (p == Py_None)
29            return p;
30       
31        return converter::get_lvalue_from_python(p, registered<T>::converters);
32    }
33   
34    static void construct(PyObject* source, rvalue_from_python_stage1_data* data)
35    {
36        void* const storage = ((converter::rvalue_from_python_storage<shared_ptr<T> >*)data)->storage.bytes;
37        // Deal with the "None" case.
38        if (data->convertible == source)
39            new (storage) shared_ptr<T>();
40        else
41            new (storage) shared_ptr<T>(
42                static_cast<T*>(data->convertible),
43                shared_ptr_deleter(handle<>(borrowed(source)))
44                );
45       
46        data->convertible = storage;
47    }
48};
49
50}}} // namespace boost::python::converter
51
52#endif // SHARED_PTR_FROM_PYTHON_DWA20021130_HPP
Note: See TracBrowser for help on using the repository browser.