Chris@16
|
1 // Copyright David Abrahams 2002.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 #ifndef SHARED_PTR_FROM_PYTHON_DWA20021130_HPP
|
Chris@16
|
6 # define SHARED_PTR_FROM_PYTHON_DWA20021130_HPP
|
Chris@16
|
7
|
Chris@16
|
8 # include <boost/python/handle.hpp>
|
Chris@16
|
9 # include <boost/python/converter/shared_ptr_deleter.hpp>
|
Chris@16
|
10 # include <boost/python/converter/from_python.hpp>
|
Chris@16
|
11 # include <boost/python/converter/rvalue_from_python_data.hpp>
|
Chris@16
|
12 # include <boost/python/converter/registered.hpp>
|
Chris@16
|
13 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
|
Chris@16
|
14 # include <boost/python/converter/pytype_function.hpp>
|
Chris@16
|
15 #endif
|
Chris@16
|
16 # include <boost/shared_ptr.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 namespace boost { namespace python { namespace converter {
|
Chris@16
|
19
|
Chris@16
|
20 template <class T>
|
Chris@16
|
21 struct shared_ptr_from_python
|
Chris@16
|
22 {
|
Chris@16
|
23 shared_ptr_from_python()
|
Chris@16
|
24 {
|
Chris@16
|
25 converter::registry::insert(&convertible, &construct, type_id<shared_ptr<T> >()
|
Chris@16
|
26 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
|
Chris@16
|
27 , &converter::expected_from_python_type_direct<T>::get_pytype
|
Chris@16
|
28 #endif
|
Chris@16
|
29 );
|
Chris@16
|
30 }
|
Chris@16
|
31
|
Chris@16
|
32 private:
|
Chris@16
|
33 static void* convertible(PyObject* p)
|
Chris@16
|
34 {
|
Chris@16
|
35 if (p == Py_None)
|
Chris@16
|
36 return p;
|
Chris@16
|
37
|
Chris@16
|
38 return converter::get_lvalue_from_python(p, registered<T>::converters);
|
Chris@16
|
39 }
|
Chris@16
|
40
|
Chris@16
|
41 static void construct(PyObject* source, rvalue_from_python_stage1_data* data)
|
Chris@16
|
42 {
|
Chris@16
|
43 void* const storage = ((converter::rvalue_from_python_storage<shared_ptr<T> >*)data)->storage.bytes;
|
Chris@16
|
44 // Deal with the "None" case.
|
Chris@16
|
45 if (data->convertible == source)
|
Chris@16
|
46 new (storage) shared_ptr<T>();
|
Chris@16
|
47 else
|
Chris@16
|
48 {
|
Chris@16
|
49 boost::shared_ptr<void> hold_convertible_ref_count(
|
Chris@16
|
50 (void*)0, shared_ptr_deleter(handle<>(borrowed(source))) );
|
Chris@16
|
51 // use aliasing constructor
|
Chris@16
|
52 new (storage) shared_ptr<T>(
|
Chris@16
|
53 hold_convertible_ref_count,
|
Chris@16
|
54 static_cast<T*>(data->convertible));
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57 data->convertible = storage;
|
Chris@16
|
58 }
|
Chris@16
|
59 };
|
Chris@16
|
60
|
Chris@16
|
61 }}} // namespace boost::python::converter
|
Chris@16
|
62
|
Chris@16
|
63 #endif // SHARED_PTR_FROM_PYTHON_DWA20021130_HPP
|