Skip to content

Commit b8aaf7d

Browse files
committed
Rationalized conversion registry
Better error reporting [SVN r14412]
1 parent c0ecde9 commit b8aaf7d

File tree

13 files changed

+241
-233
lines changed

13 files changed

+241
-233
lines changed

include/boost/python/converter/arg_from_python.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
# include <boost/python/converter/rvalue_from_python_data.hpp>
1515
# include <boost/mpl/select_type.hpp>
1616
# include <boost/python/converter/registry.hpp>
17-
# include <boost/python/converter/lvalue_from_python_chain.hpp>
18-
# include <boost/python/converter/rvalue_from_python_chain.hpp>
17+
# include <boost/python/converter/from_python.hpp>
18+
# include <boost/python/converter/pointee_from_python.hpp>
1919
# include <boost/python/detail/void_ptr.hpp>
2020
# include <boost/python/back_reference.hpp>
2121
# include <boost/python/detail/referent_storage.hpp>
@@ -229,7 +229,7 @@ inline pointer_cref_arg_from_python<T>::pointer_cref_arg_from_python(PyObject* p
229229
// a U object.
230230
python::detail::write_void_ptr_reference(
231231
m_result.bytes
232-
, p == Py_None ? p : find(p, lvalue_from_python_chain<T>::value)
232+
, p == Py_None ? p : converter::get_lvalue_from_python(p, pointee_from_python<T>::converters)
233233
, (T(*)())0);
234234
}
235235

@@ -252,7 +252,7 @@ inline T pointer_cref_arg_from_python<T>::operator()(PyObject* p) const
252252
template <class T>
253253
inline pointer_arg_from_python<T>::pointer_arg_from_python(PyObject* p)
254254
: arg_lvalue_from_python_base(
255-
p == Py_None ? p : find(p, lvalue_from_python_chain<T>::value))
255+
p == Py_None ? p : converter::get_lvalue_from_python(p, pointee_from_python<T>::converters))
256256
{
257257
}
258258

@@ -266,7 +266,7 @@ inline T pointer_arg_from_python<T>::operator()(PyObject* p) const
266266
//
267267
template <class T>
268268
inline reference_arg_from_python<T>::reference_arg_from_python(PyObject* p)
269-
: arg_lvalue_from_python_base(find(p,lvalue_from_python_chain<T>::value))
269+
: arg_lvalue_from_python_base(converter::get_lvalue_from_python(p,from_python<T>::converters))
270270
{
271271
}
272272

@@ -281,7 +281,7 @@ inline T reference_arg_from_python<T>::operator()(PyObject*) const
281281
//
282282
template <class T>
283283
inline arg_rvalue_from_python<T>::arg_rvalue_from_python(PyObject* obj)
284-
: m_data(find(obj, rvalue_from_python_chain<T>::value))
284+
: m_data(converter::rvalue_from_python_stage1(obj, from_python<T>::converters))
285285
{
286286
}
287287

include/boost/python/converter/callback_from_python_base.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88

99
namespace boost { namespace python { namespace converter {
1010

11+
struct rvalue_from_python_stage1_data;
12+
struct from_python_registration;
13+
1114
namespace detail
1215
{
13-
14-
// Throw an exception
15-
BOOST_PYTHON_DECL void throw_if_not_registered(rvalue_from_python_stage1_data const&);
1616
BOOST_PYTHON_DECL void* convert_rvalue(PyObject*, rvalue_from_python_stage1_data&, void* storage);
17-
18-
BOOST_PYTHON_DECL void throw_if_not_registered(lvalue_from_python_registration*const&);
19-
BOOST_PYTHON_DECL void* callback_convert_reference(PyObject*, lvalue_from_python_registration*const&);
20-
BOOST_PYTHON_DECL void* callback_convert_pointer(PyObject*, lvalue_from_python_registration*const&);
17+
BOOST_PYTHON_DECL void* callback_convert_reference(PyObject*, from_python_registration const&);
18+
BOOST_PYTHON_DECL void* callback_convert_pointer(PyObject*, from_python_registration const&);
2119
BOOST_PYTHON_DECL void absorb_result(PyObject*);
2220
}
2321

include/boost/python/converter/find_from_python.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212

1313
namespace boost { namespace python { namespace converter {
1414

15-
struct lvalue_from_python_registration;
16-
struct rvalue_from_python_registration;
15+
struct from_python_registration;
16+
struct rvalue_from_python_chain;
1717

18-
BOOST_PYTHON_DECL void* find(
19-
PyObject* source, lvalue_from_python_registration const*);
18+
BOOST_PYTHON_DECL void* get_lvalue_from_python(
19+
PyObject* source, from_python_registration const&);
2020

21-
BOOST_PYTHON_DECL rvalue_from_python_stage1_data find(
22-
PyObject* source, rvalue_from_python_registration const*);
21+
BOOST_PYTHON_DECL rvalue_from_python_stage1_data rvalue_from_python_stage1(
22+
PyObject* source, from_python_registration const&);
2323

24-
BOOST_PYTHON_DECL rvalue_from_python_registration const* find_chain(
25-
PyObject* source, rvalue_from_python_registration const*);
24+
BOOST_PYTHON_DECL rvalue_from_python_chain const* implicit_conversion_chain(
25+
PyObject* source, from_python_registration const&);
2626

2727
}}} // namespace boost::python::converter
2828

include/boost/python/converter/rvalue_from_python_chain.hpp renamed to include/boost/python/converter/from_python.hpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,51 @@
33
// copyright notice appears in all copies. This software is provided
44
// "as is" without express or implied warranty, and with no claim as
55
// to its suitability for any purpose.
6-
#ifndef RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP
7-
# define RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP
6+
#ifndef FROM_PYTHON_DWA2002710_HPP
7+
# define FROM_PYTHON_DWA2002710_HPP
88
# include <boost/python/type_id.hpp>
99
# include <boost/python/converter/registry.hpp>
1010
# include <boost/type_traits/transform_traits.hpp>
1111
# include <boost/type_traits/cv_traits.hpp>
1212

1313
namespace boost { namespace python { namespace converter {
1414

15+
struct from_python_registration;
16+
1517
namespace detail
1618
{
1719
template <class T>
18-
struct rvalue_from_python_chain_impl
20+
struct from_python_base
1921
{
20-
static rvalue_from_python_registration*const& value;
22+
static from_python_registration const& converters;
2123
};
22-
23-
template <class T>
24-
rvalue_from_python_registration*const& rvalue_from_python_chain_impl<T>::value
25-
= registry::rvalue_converters(type_id<T>());
2624
}
2725

2826
template <class T>
29-
struct rvalue_from_python_chain
30-
: detail::rvalue_from_python_chain_impl<
27+
struct from_python
28+
: detail::from_python_base<
3129
typename add_reference<
32-
typename add_cv<T>::type
30+
typename add_cv<T>::type
3331
>::type
34-
>
32+
>
3533
{
3634
};
3735

36+
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
37+
// collapses a few more types to the same static instance
38+
template <class T>
39+
struct from_python<T&> : from_python<T> {};
40+
# endif
41+
42+
//
43+
// implementations
44+
//
45+
namespace detail
46+
{
47+
template <class T>
48+
from_python_registration const& from_python_base<T>::converters
49+
= registry::from_python_converters(type_id<T>());
50+
}
3851
}}} // namespace boost::python::converter
3952

40-
#endif // RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP
53+
#endif // FROM_PYTHON_DWA2002710_HPP

include/boost/python/converter/implicit.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ struct implicit
1616
static void* convertible(PyObject* obj)
1717
{
1818
// Find a converter registration which can produce a Source
19-
// instance from obj
20-
return const_cast<rvalue_from_python_registration*>(
21-
find_chain(obj, rvalue_from_python_chain<Source>::value));
19+
// instance from obj. The user has told us that Source can be
20+
// converted to Target, and instantiating construct() below,
21+
// ensures that at compile-time.
22+
return const_cast<rvalue_from_python_chain*>(
23+
converter::implicit_conversion_chain(obj, from_python<Source>::converters));
2224
}
2325

2426
static void construct(PyObject* obj, rvalue_from_python_stage1_data* data)
2527
{
2628
// This is the registration we got from the convertible step
27-
rvalue_from_python_registration const* registration
28-
= static_cast<rvalue_from_python_registration*>(data->convertible);
29+
rvalue_from_python_chain const* registration
30+
= static_cast<rvalue_from_python_chain*>(data->convertible);
2931

3032
// Call the convertible function again
3133
rvalue_from_python_data<Source> intermediate_data(registration->convertible(obj));

include/boost/python/converter/lvalue_from_python_chain.hpp

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 POINTEE_FROM_PYTHON_DWA2002710_HPP
7+
# define POINTEE_FROM_PYTHON_DWA2002710_HPP
8+
# include <boost/python/converter/from_python.hpp>
9+
# include <boost/python/converter/pointer_type_id.hpp>
10+
# include <boost/python/converter/registry.hpp>
11+
# include <boost/type_traits/transform_traits.hpp>
12+
# include <boost/type_traits/cv_traits.hpp>
13+
14+
namespace boost { namespace python { namespace converter {
15+
16+
struct from_python_registration;
17+
18+
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
19+
template <class T>
20+
struct pointee_from_python
21+
: from_python<
22+
typename remove_pointer<
23+
typename remove_cv<
24+
typename remove_reference<T>::type
25+
>::type
26+
>::type
27+
>
28+
{
29+
};
30+
# else
31+
namespace detail
32+
{
33+
template <class T>
34+
struct pointee_from_python_base
35+
{
36+
static from_python_registration const& converters;
37+
};
38+
}
39+
40+
template <class T>
41+
struct pointee_from_python
42+
: detail::pointee_from_python_base<
43+
typename add_reference<
44+
typename add_cv<T>::type
45+
>::type
46+
>
47+
{
48+
};
49+
50+
//
51+
// implementations
52+
//
53+
namespace detail
54+
{
55+
template <class T>
56+
from_python_registration const& pointee_from_python_base<T>::converters
57+
= registry::from_python_converters(pointer_type_id<T>());
58+
}
59+
60+
# endif
61+
}}} // namespace boost::python::converter
62+
63+
#endif // POINTEE_FROM_PYTHON_DWA2002710_HPP

include/boost/python/converter/registrations.hpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,41 @@
88

99
# include <boost/python/converter/convertible_function.hpp>
1010
# include <boost/python/converter/constructor_function.hpp>
11+
# include <boost/python/type_id.hpp>
1112

1213
namespace boost { namespace python { namespace converter {
1314

14-
struct lvalue_from_python_registration
15+
struct lvalue_from_python_chain
1516
{
1617
convertible_function convert;
17-
lvalue_from_python_registration* next;
18+
lvalue_from_python_chain* next;
1819
};
1920

20-
struct rvalue_from_python_registration
21+
struct rvalue_from_python_chain
2122
{
2223
convertible_function convertible;
2324
constructor_function construct;
24-
rvalue_from_python_registration* next;
25+
rvalue_from_python_chain* next;
2526
};
2627

28+
struct from_python_registration
29+
{
30+
explicit from_python_registration(type_info);
31+
32+
const python::type_info target_type;
33+
lvalue_from_python_chain* lvalue_chain;
34+
rvalue_from_python_chain* rvalue_chain;
35+
};
36+
37+
//
38+
// implementations
39+
//
40+
inline from_python_registration::from_python_registration(type_info target_type)
41+
: target_type(target_type)
42+
, lvalue_chain(0)
43+
, rvalue_chain(0)
44+
{}
45+
2746
}}} // namespace boost::python::converter
2847

2948
#endif // REGISTRATIONS_DWA2002223_HPP

include/boost/python/converter/registry.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515

1616
namespace boost { namespace python { namespace converter {
1717

18-
struct lvalue_from_python_registration;
19-
struct rvalue_from_python_registration;
18+
struct from_python_registration;
2019

2120
// This namespace acts as a sort of singleton
2221
namespace registry
2322
{
24-
BOOST_PYTHON_DECL lvalue_from_python_registration*& lvalue_converters(type_info);
25-
BOOST_PYTHON_DECL rvalue_from_python_registration*& rvalue_converters(type_info);
23+
BOOST_PYTHON_DECL from_python_registration const& from_python_converters(type_info);
2624

2725
BOOST_PYTHON_DECL to_python_function_t const&
2826
get_to_python_function(type_info);

0 commit comments

Comments
 (0)