Skip to content

Commit 97e4b34

Browse files
committed
Add support for std::shared_ptr.
1 parent 5029273 commit 97e4b34

File tree

13 files changed

+568
-351
lines changed

13 files changed

+568
-351
lines changed

include/boost/python/converter/registered.hpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
// Copyright David Abrahams 2002.
2+
// Copyright Stefan Seefeld 2016.
23
// Distributed under the Boost Software License, Version 1.0. (See
34
// accompanying file LICENSE_1_0.txt or copy at
45
// http://www.boost.org/LICENSE_1_0.txt)
5-
#ifndef REGISTERED_DWA2002710_HPP
6-
# define REGISTERED_DWA2002710_HPP
7-
# include <boost/python/type_id.hpp>
8-
# include <boost/python/converter/registry.hpp>
9-
# include <boost/python/converter/registrations.hpp>
10-
# include <boost/type_traits/transform_traits.hpp>
11-
# include <boost/type_traits/cv_traits.hpp>
12-
# include <boost/type_traits/is_void.hpp>
13-
# include <boost/detail/workaround.hpp>
14-
# include <boost/python/type_id.hpp>
15-
# include <boost/type.hpp>
166

7+
#ifndef boost_python_converter_registered_hpp_
8+
#define boost_python_converter_registered_hpp_
9+
10+
#include <boost/python/type_id.hpp>
11+
#include <boost/python/converter/registry.hpp>
12+
#include <boost/python/converter/registrations.hpp>
13+
#include <boost/type_traits/transform_traits.hpp>
14+
#include <boost/type_traits/cv_traits.hpp>
15+
#include <boost/type_traits/is_void.hpp>
16+
#include <boost/detail/workaround.hpp>
17+
#include <boost/type.hpp>
18+
#include <memory>
1719
#if defined(BOOST_PYTHON_TRACE_REGISTRY) \
1820
|| defined(BOOST_PYTHON_CONVERTER_REGISTRY_APPLE_MACH_WORKAROUND)
1921
# include <iostream>
@@ -75,7 +77,16 @@ namespace detail
7577
{
7678
registry::lookup_shared_ptr(type_id<shared_ptr<T> >());
7779
}
78-
80+
81+
#if __cplusplus >= 201103L
82+
template <class T>
83+
inline void
84+
register_shared_ptr0(std::shared_ptr<T>*)
85+
{
86+
registry::lookup_shared_ptr(type_id<std::shared_ptr<T> >());
87+
}
88+
#endif
89+
7990
template <class T>
8091
inline void
8192
register_shared_ptr1(T const volatile*)
@@ -112,4 +123,4 @@ namespace detail
112123

113124
}}} // namespace boost::python::converter
114125

115-
#endif // REGISTERED_DWA2002710_HPP
126+
#endif
Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,65 @@
11
// Copyright David Abrahams 2002.
2+
// Copyright Stefan Seefeld 2016.
23
// Distributed under the Boost Software License, Version 1.0. (See
34
// accompanying file LICENSE_1_0.txt or copy at
45
// 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>
6+
7+
#ifndef boost_python_converter_shared_ptr_from_python_hpp_
8+
#define boost_python_converter_shared_ptr_from_python_hpp_
9+
10+
#include <boost/python/handle.hpp>
11+
#include <boost/python/converter/shared_ptr_deleter.hpp>
12+
#include <boost/python/converter/from_python.hpp>
13+
#include <boost/python/converter/rvalue_from_python_data.hpp>
14+
#include <boost/python/converter/registered.hpp>
1315
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
1416
# include <boost/python/converter/pytype_function.hpp>
1517
#endif
16-
# include <boost/shared_ptr.hpp>
18+
#include <boost/shared_ptr.hpp>
19+
#include <memory>
1720

1821
namespace boost { namespace python { namespace converter {
1922

20-
template <class T>
23+
template <class T, template <typename> class SP>
2124
struct shared_ptr_from_python
2225
{
23-
shared_ptr_from_python()
24-
{
25-
converter::registry::insert(&convertible, &construct, type_id<shared_ptr<T> >()
26+
shared_ptr_from_python()
27+
{
28+
converter::registry::insert(&convertible, &construct, type_id<SP<T> >()
2629
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
27-
, &converter::expected_from_python_type_direct<T>::get_pytype
30+
, &converter::expected_from_python_type_direct<T>::get_pytype
2831
#endif
29-
);
30-
}
32+
);
33+
}
3134

3235
private:
33-
static void* convertible(PyObject* p)
34-
{
35-
if (p == Py_None)
36-
return p;
36+
static void* convertible(PyObject* p)
37+
{
38+
if (p == Py_None)
39+
return p;
3740

38-
return converter::get_lvalue_from_python(p, registered<T>::converters);
39-
}
41+
return converter::get_lvalue_from_python(p, registered<T>::converters);
42+
}
4043

41-
static void construct(PyObject* source, rvalue_from_python_stage1_data* data)
44+
static void construct(PyObject* source, rvalue_from_python_stage1_data* data)
45+
{
46+
void* const storage = ((converter::rvalue_from_python_storage<SP<T> >*)data)->storage.bytes;
47+
// Deal with the "None" case.
48+
if (data->convertible == source)
49+
new (storage) SP<T>();
50+
else
4251
{
43-
void* const storage = ((converter::rvalue_from_python_storage<shared_ptr<T> >*)data)->storage.bytes;
44-
// Deal with the "None" case.
45-
if (data->convertible == source)
46-
new (storage) shared_ptr<T>();
47-
else
48-
{
49-
boost::shared_ptr<void> hold_convertible_ref_count(
50-
(void*)0, shared_ptr_deleter(handle<>(borrowed(source))) );
51-
// use aliasing constructor
52-
new (storage) shared_ptr<T>(
53-
hold_convertible_ref_count,
54-
static_cast<T*>(data->convertible));
55-
}
56-
57-
data->convertible = storage;
52+
SP<void> hold_convertible_ref_count(
53+
(void*)0, shared_ptr_deleter(handle<>(borrowed(source))) );
54+
// use aliasing constructor
55+
new (storage) SP<T>(hold_convertible_ref_count,
56+
static_cast<T*>(data->convertible));
5857
}
58+
59+
data->convertible = storage;
60+
}
5961
};
6062

6163
}}} // namespace boost::python::converter
6264

63-
#endif // SHARED_PTR_FROM_PYTHON_DWA20021130_HPP
65+
#endif
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Copyright David Abrahams 2003.
2+
// Copyright Stefan Seefeld 2016.
23
// Distributed under the Boost Software License, Version 1.0. (See
34
// accompanying file LICENSE_1_0.txt or copy at
45
// http://www.boost.org/LICENSE_1_0.txt)
5-
#ifndef SHARED_PTR_TO_PYTHON_DWA2003224_HPP
6-
# define SHARED_PTR_TO_PYTHON_DWA2003224_HPP
76

8-
# include <boost/python/refcount.hpp>
9-
# include <boost/python/converter/shared_ptr_deleter.hpp>
10-
# include <boost/shared_ptr.hpp>
11-
# include <boost/get_pointer.hpp>
7+
#ifndef boost_python_converter_shared_ptr_to_python_hpp_
8+
#define boost_python_converter_shared_ptr_to_python_hpp_
9+
10+
#include <boost/python/refcount.hpp>
11+
#include <boost/python/converter/shared_ptr_deleter.hpp>
12+
#include <boost/shared_ptr.hpp>
13+
#include <boost/get_pointer.hpp>
1214

1315
namespace boost { namespace python { namespace converter {
1416

@@ -23,6 +25,19 @@ PyObject* shared_ptr_to_python(shared_ptr<T> const& x)
2325
return converter::registered<shared_ptr<T> const&>::converters.to_python(&x);
2426
}
2527

28+
#if __cplusplus >= 201103L
29+
template <class T>
30+
PyObject* shared_ptr_to_python(std::shared_ptr<T> const& x)
31+
{
32+
if (!x)
33+
return python::detail::none();
34+
else if (shared_ptr_deleter* d = std::get_deleter<shared_ptr_deleter>(x))
35+
return incref(get_pointer(d->owner));
36+
else
37+
return converter::registered<std::shared_ptr<T> const&>::converters.to_python(&x);
38+
}
39+
#endif
40+
2641
}}} // namespace boost::python::converter
2742

28-
#endif // SHARED_PTR_TO_PYTHON_DWA2003224_HPP
43+
#endif
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
// Copyright David Abrahams 2003.
2+
// Copyright Stefan Seefeld 2016.
23
// Distributed under the Boost Software License, Version 1.0. (See
34
// accompanying file LICENSE_1_0.txt or copy at
45
// http://www.boost.org/LICENSE_1_0.txt)
5-
#ifndef IS_SHARED_PTR_DWA2003224_HPP
6-
# define IS_SHARED_PTR_DWA2003224_HPP
76

8-
# include <boost/python/detail/is_xxx.hpp>
9-
# include <boost/shared_ptr.hpp>
7+
#ifndef boost_python_detail_is_shared_ptr_hpp_
8+
#define boost_python_detail_is_shared_ptr_hpp_
9+
10+
#include <boost/python/detail/is_xxx.hpp>
11+
#include <boost/shared_ptr.hpp>
1012

1113
namespace boost { namespace python { namespace detail {
1214

1315
BOOST_PYTHON_IS_XXX_DEF(shared_ptr, shared_ptr, 1)
14-
16+
#if __cplusplus >= 201103L
17+
template <typename T>
18+
struct is_shared_ptr<std::shared_ptr<T> > : std::true_type {};
19+
#endif
20+
1521
}}} // namespace boost::python::detail
1622

17-
#endif // IS_SHARED_PTR_DWA2003224_HPP
23+
#endif
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
// Copyright David Abrahams 2003.
2+
// Copyright Stefan Seefeld 2016.
23
// Distributed under the Boost Software License, Version 1.0. (See
34
// accompanying file LICENSE_1_0.txt or copy at
45
// http://www.boost.org/LICENSE_1_0.txt)
5-
#ifndef VALUE_IS_SHARED_PTR_DWA2003224_HPP
6-
# define VALUE_IS_SHARED_PTR_DWA2003224_HPP
76

8-
# include <boost/python/detail/value_is_xxx.hpp>
9-
# include <boost/shared_ptr.hpp>
7+
#ifndef boost_python_detail_value_is_shared_ptr_hpp_
8+
#define boost_python_detail_value_is_shared_ptr_hpp_
9+
10+
#include <boost/python/detail/value_is_xxx.hpp>
11+
#include <boost/python/detail/is_shared_ptr.hpp>
1012

1113
namespace boost { namespace python { namespace detail {
1214

13-
BOOST_PYTHON_VALUE_IS_XXX_DEF(shared_ptr, shared_ptr, 1)
14-
15+
template <class X_>
16+
struct value_is_shared_ptr
17+
{
18+
static bool const value = is_shared_ptr<typename remove_cv<
19+
typename remove_reference<X_>
20+
::type>
21+
::type>
22+
::value;
23+
typedef mpl::bool_<value> type;
24+
};
25+
1526
}}} // namespace boost::python::detail
1627

1728
#endif // VALUE_IS_SHARED_PTR_DWA2003224_HPP

0 commit comments

Comments
 (0)