Skip to content

Commit b3117c2

Browse files
committed
Use call policies
[SVN r12618]
1 parent 6a75fa8 commit b3117c2

19 files changed

+1379
-350
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef SMART_PTR_DWA2002123_HPP
7+
# define SMART_PTR_DWA2002123_HPP
8+
9+
# include <boost/python/converter/class.hpp>
10+
# include <boost/python/object/pointer_holder.hpp>
11+
namespace boost { namespace python { namespace converter {
12+
13+
template <class Pointer, class Value>
14+
class smart_ptr_wrapper
15+
: wrapper<Pointer const&>
16+
{
17+
smart_ptr_wrapper(ref const& type_)
18+
: m_class_object(type_)
19+
{
20+
assert(type_->ob_type == (PyTypeObject*)class_metatype().get());
21+
}
22+
23+
PyObject* convert(Pointer x) const;
24+
25+
private:
26+
ref m_class_object;
27+
28+
smart_ptr_converters();
29+
}
30+
31+
32+
//
33+
// implementations
34+
//
35+
36+
template <class Pointer, class Value>
37+
PyObject* smart_ptr_wrapper<Pointer,Value>::convert(Pointer x) const
38+
{
39+
if (x.operator->() == 0)
40+
return detail::none();
41+
42+
// Don't call the type to do the construction, since that
43+
// would require the registration of an __init__ copy
44+
// constructor. Instead, just construct the object in place.
45+
PyObject* raw_result = (PyObject*)PyObject_New(
46+
instance, (PyTypeObject*)m_class_object.get());
47+
48+
if (raw_result == 0)
49+
return 0;
50+
51+
// Everything's OK; Bypass NULL checks but guard against
52+
// exceptions.
53+
ref result(raw_result, ref::allow_null());
54+
55+
// Build a value_holder to contain the object using the copy
56+
// constructor
57+
objects::pointer_holder<Pointer,Value>*
58+
p = new objects::pointer_holder<Pointer,Value>(x);
59+
60+
// Install it in the instance
61+
p->install(raw_result);
62+
63+
// Return the new result
64+
return result.release();
65+
}
66+
67+
68+
}}} // namespace boost::python::converter
69+
70+
#endif // SMART_PTR_DWA2002123_HPP

include/boost/python/converter/to_python.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ template <class T>
101101
inline bool
102102
to_python_lookup<T>::convertible() const
103103
{
104-
return m_converter != 0;
104+
return m_convert != 0;
105105
}
106106

107107
template <class T>

include/boost/python/converter/type_id.hpp

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
#ifndef TYPE_ID_DWA20011127_HPP
77
# define TYPE_ID_DWA20011127_HPP
88
# include <boost/python/detail/config.hpp>
9-
# include <boost/python/detail/config.hpp>
9+
# include <boost/python/detail/indirect_traits.hpp>
1010
# include <boost/mpl/select_type.hpp>
11-
# include <boost/type_traits/cv_traits.hpp>
12-
# include <boost/type_traits/composite_traits.hpp>
1311
# include <boost/operators.hpp>
1412
# include <typeinfo>
1513
# include <iosfwd>
@@ -79,80 +77,6 @@ struct type_id_t : totally_ordered<type_id_t>
7977
base_id_t m_base_type;
8078
};
8179

82-
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
83-
template <class T>
84-
struct is_reference_to_const
85-
{
86-
BOOST_STATIC_CONSTANT(bool, value = false);
87-
};
88-
89-
template <class T>
90-
struct is_reference_to_const<T const&>
91-
{
92-
BOOST_STATIC_CONSTANT(bool, value = true);
93-
};
94-
95-
template <class T>
96-
struct is_reference_to_volatile
97-
{
98-
BOOST_STATIC_CONSTANT(bool, value = false);
99-
};
100-
101-
template <class T>
102-
struct is_reference_to_volatile<T volatile&>
103-
{
104-
BOOST_STATIC_CONSTANT(bool, value = true);
105-
};
106-
# else
107-
template <typename V>
108-
struct is_const_help
109-
{
110-
typedef typename mpl::select_type<
111-
is_const<V>::value
112-
, type_traits::yes_type
113-
, type_traits::no_type
114-
>::type type;
115-
};
116-
117-
template <typename V>
118-
struct is_volatile_help
119-
{
120-
typedef typename mpl::select_type<
121-
is_volatile<V>::value
122-
, type_traits::yes_type
123-
, type_traits::no_type
124-
>::type type;
125-
};
126-
127-
template <typename V>
128-
typename is_const_help<V>::type reference_to_const_helper(V&);
129-
130-
type_traits::no_type
131-
reference_to_const_helper(...);
132-
133-
template <class T>
134-
struct is_reference_to_const
135-
{
136-
static T t;
137-
BOOST_STATIC_CONSTANT(
138-
bool, value
139-
= sizeof(reference_to_const_helper(t)) == sizeof(type_traits::yes_type));
140-
};
141-
142-
template <typename V>
143-
typename is_volatile_help<V>::type reference_to_volatile_helper(V&);
144-
type_traits::no_type reference_to_volatile_helper(...);
145-
146-
template <class T>
147-
struct is_reference_to_volatile
148-
{
149-
static T t;
150-
BOOST_STATIC_CONSTANT(
151-
bool, value
152-
= sizeof(reference_to_volatile_helper(t)) == sizeof(type_traits::yes_type));
153-
};
154-
# endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
155-
15680
template <class T>
15781
inline undecorated_type_id_t undecorated_type_id(detail::dummy<T>* = 0)
15882
{
@@ -165,9 +89,9 @@ inline type_id_t type_id(detail::dummy<T>* = 0)
16589
return type_id_t(
16690
undecorated_type_id<T>()
16791
, type_id_t::decoration(
168-
(is_const<T>::value || is_reference_to_const<T>::value
92+
(is_const<T>::value || python::detail::is_reference_to_const<T>::value
16993
? type_id_t::const_ : 0)
170-
| (is_volatile<T>::value || is_reference_to_volatile<T>::value
94+
| (is_volatile<T>::value || python::detail::is_reference_to_volatile<T>::value
17195
? type_id_t::volatile_ : 0)
17296
| (is_reference<T>::value ? type_id_t::reference : 0)
17397
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef COPY_CONST_REFERENCE_DWA2002131_HPP
7+
# define COPY_CONST_REFERENCE_DWA2002131_HPP
8+
# include <boost/python/detail/indirect_traits.hpp>
9+
# include <boost/mpl/select_type.hpp>
10+
# include <boost/python/to_python.hpp>
11+
12+
namespace boost { namespace python {
13+
14+
namespace detail
15+
{
16+
template <class R>
17+
struct copy_const_reference_expects_a_const_reference_return_type
18+
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
19+
{}
20+
# endif
21+
;
22+
}
23+
24+
template <class T> struct to_python;
25+
26+
struct copy_const_reference
27+
{
28+
template <class T>
29+
struct apply
30+
{
31+
typedef typename mpl::select_type<
32+
detail::is_reference_to_const<T>::value
33+
, to_python<T>
34+
, detail::copy_const_reference_expects_a_const_reference_return_type<T>
35+
>::type type;
36+
};
37+
};
38+
39+
40+
}} // namespace boost::python
41+
42+
#endif // COPY_CONST_REFERENCE_DWA2002131_HPP
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef COPY_NON_CONST_REFERENCE_DWA2002131_HPP
7+
# define COPY_NON_CONST_REFERENCE_DWA2002131_HPP
8+
# include <boost/python/detail/indirect_traits.hpp>
9+
# include <boost/mpl/select_type.hpp>
10+
# include <boost/python/to_python.hpp>
11+
12+
namespace boost { namespace python {
13+
14+
namespace detail
15+
{
16+
template <class R>
17+
struct copy_non_const_reference_expects_a_non_const_reference_return_type
18+
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
19+
{}
20+
# endif
21+
;
22+
}
23+
24+
template <class T> struct to_python;
25+
26+
struct copy_non_const_reference
27+
{
28+
template <class T>
29+
struct apply
30+
{
31+
typedef typename mpl::select_type<
32+
boost::python::detail::is_reference_to_non_const<T>::value
33+
, to_python<T>
34+
, detail::copy_non_const_reference_expects_a_non_const_reference_return_type<T>
35+
>::type type;
36+
};
37+
};
38+
39+
40+
}} // namespace boost::python
41+
42+
#endif // COPY_NON_CONST_REFERENCE_DWA2002131_HPP
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef DEFAULT_CALL_POLICIES_DWA2002131_HPP
7+
# define DEFAULT_CALL_POLICIES_DWA2002131_HPP
8+
# include <boost/python/detail/wrap_python.hpp>
9+
# include <boost/mpl/select_type.hpp>
10+
11+
namespace boost { namespace python {
12+
13+
template <class T> struct to_python;
14+
15+
namespace detail
16+
{
17+
// for "readable" error messages
18+
template <class T> struct specify_a_result_policy_to_wrap_functions_returning
19+
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
20+
{}
21+
# endif
22+
;
23+
}
24+
25+
struct default_result_converter;
26+
27+
struct default_call_policies
28+
{
29+
// Nothing to do
30+
static bool precall(PyObject*)
31+
{
32+
return true;
33+
}
34+
35+
// Pass the result through
36+
static PyObject* postcall(PyObject*, PyObject* result)
37+
{
38+
return result;
39+
}
40+
41+
typedef default_result_converter result_converter;
42+
};
43+
44+
struct default_result_converter
45+
{
46+
template <class R>
47+
struct apply
48+
{
49+
typedef typename mpl::select_type<
50+
is_reference<R>::value | is_pointer<R>::value
51+
, detail::specify_a_result_policy_to_wrap_functions_returning<R>
52+
, to_python<R>
53+
>::type type;
54+
};
55+
};
56+
57+
// Exceptions for c strings an PyObject*s
58+
template <>
59+
struct default_result_converter::apply<char const*>
60+
{
61+
typedef boost::python::to_python<char const*> type;
62+
};
63+
64+
template <>
65+
struct default_result_converter::apply<PyObject*>
66+
{
67+
typedef boost::python::to_python<PyObject*> type;
68+
};
69+
70+
}} // namespace boost::python
71+
72+
#endif // DEFAULT_CALL_POLICIES_DWA2002131_HPP

0 commit comments

Comments
 (0)