1111# include < boost/python/converter/from_python_function.hpp>
1212# include < boost/python/converter/from_python_data.hpp>
1313# include < boost/python/converter/type_id.hpp>
14- # include < boost/python/converter/registration .hpp>
14+ # include < boost/python/converter/registry .hpp>
1515# include < boost/python/detail/wrap_python.hpp>
1616
1717namespace boost { namespace python { namespace converter {
@@ -29,16 +29,21 @@ template <class T> struct from_python_lookup;
2929struct BOOST_PYTHON_DECL from_python_converter_base : body
3030{
3131 from_python_converter_base (type_id_t , from_python_check); // registers
32- ~from_python_converter_base (); // unregisters
3332
3433 // Must return non-null iff the conversion will be successful. Any
3534 // non-null pointer is acceptable, and will be passed on to the
3635 // convert() function, so useful data can be stored there.
3736 inline void * convertible (PyObject*) const ;
38- // inline type_id_t key() const;
37+
38+ // Given the head of a from_python converter chain, find the
39+ // converter which can convert p, leaving its intermediate data in
40+ // data.
41+ inline static from_python_converter_base const *
42+ find (from_python_converter_base const *chain, PyObject* p, void *& data);
43+
3944 private:
40- // type_id_t m_key;
4145 from_python_check m_convertible;
46+ from_python_converter_base* m_next;
4247};
4348
4449
@@ -52,17 +57,24 @@ struct from_python_converter : from_python_converter_base
5257 from_python_converter (from_python_check, conversion_function, from_python_destructor = 0 );
5358 T convert (PyObject*, from_python_data&) const ;
5459 void destroy (from_python_data&) const ;
60+
61+ // Find a converter for converting p to a T.
62+ static from_python_converter<T> const * find (PyObject* p, void *& data);
5563
5664 private: // data members
5765 conversion_function m_convert;
5866 from_python_destructor m_destroy;
67+
68+ // Keeps the chain of converters which convert from PyObject* to T
69+ static from_python_converter_base*const & chain;
5970};
6071
61- // -------------------------------------------------------------------------
72+ // Initialized to refer to a common place in the registry.
73+ template <class T >
74+ from_python_converter_base*const &
75+ from_python_converter<T>::chain = registry::from_python_chain(type_id<T>());
6276
63- // struct from_python_base
64- // {
65- // };
77+ // -------------------------------------------------------------------------
6678
6779// A class which implements from_python with a registry lookup.
6880template <class T >
@@ -95,12 +107,21 @@ inline void* from_python_converter_base::convertible(PyObject* o) const
95107 return m_convertible (o);
96108}
97109
98- # if 0
99- inline type_id_t from_python_converter_base::key () const
110+ inline from_python_converter_base const *
111+ from_python_converter_base::find (
112+ from_python_converter_base const * chain, PyObject* p, void *& data)
100113{
101- return m_key;
114+ for (from_python_converter_base const * q = chain; q != 0 ; q = q->m_next )
115+ {
116+ void * d = q->convertible (p);
117+ if (d != 0 )
118+ {
119+ data = d;
120+ return q;
121+ }
122+ }
123+ return 0 ;
102124}
103- # endif
104125
105126template <class T >
106127inline from_python_converter<T>::from_python_converter(
@@ -115,6 +136,14 @@ inline from_python_converter<T>::from_python_converter(
115136
116137}
117138
139+ template <class T >
140+ inline from_python_converter<T> const *
141+ from_python_converter<T>::find(PyObject* p, void *& data)
142+ {
143+ return static_cast <from_python_converter<T> const *>(
144+ from_python_converter_base::find (chain, p, data));
145+ }
146+
118147template <class T >
119148inline T from_python_converter<T>::convert(PyObject* src, from_python_data& data) const
120149{
@@ -133,7 +162,7 @@ inline void from_python_converter<T>::destroy(from_python_data& data) const
133162template <class T >
134163inline from_python_lookup<T>::from_python_lookup(PyObject* src)
135164 : m_converter(
136- registration <T>::get_from_python (
165+ from_python_converter <T>::find (
137166 src, m_intermediate_data.stage1))
138167{
139168}
0 commit comments