Skip to content

Commit e79a668

Browse files
committed
Beginning of callback implementation
[SVN r13135]
1 parent 97825fb commit e79a668

19 files changed

+380
-177
lines changed

Jamfile

Lines changed: 16 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,21 @@ subproject libs/python ;
44
SEARCH on <module@>python.jam = $(BOOST_BUILD_PATH) ;
55
include <module@>python.jam ;
66

7-
8-
{
9-
local BOOST_PYTHON_V2_PROPERTIES
10-
= $(PYTHON_PROPERTIES)
11-
<metrowerks><*><cxxflags>"-inline deferred"
12-
<cxx><*><include>$(BOOST_ROOT)/boost/compatibility/cpp_c_headers
13-
<define>BOOST_PYTHON_DYNAMIC_LIB
14-
<define>BOOST_PYTHON_V2
15-
;
16-
17-
local gcc-release-properties
18-
= <optimization>speed <cxxflags>-fomit-frame-pointer
19-
<inlining>on <cxxflags>-foptimize-sibling-calls
20-
;
21-
22-
local PYTHON_PROPERTIES = $(BOOST_PYTHON_V2_PROPERTIES) <gcc><release>$(gcc-release-properties)
23-
;
24-
25-
dll bpl
26-
:
27-
src/converter/from_python.cpp
28-
src/converter/registry.cpp
29-
src/converter/type_id.cpp
30-
src/object/class.cpp
31-
src/object/function.cpp
32-
src/object/inheritance.cpp
33-
src/object/life_support.cpp
34-
src/errors.cpp
35-
src/module.cpp
36-
src/objects.cpp
37-
src/converter/builtin_converters.cpp
38-
:
39-
$(BOOST_PYTHON_V2_PROPERTIES)
40-
<define>BOOST_PYTHON_SOURCE
41-
;
42-
43-
# -------- general test -------
44-
extension m1 : test/m1.cpp <dll>bpl
45-
:
46-
:
47-
;
48-
49-
extension m2 : test/m2.cpp <dll>bpl
50-
:
51-
: ;
52-
53-
boost-python-runtest try : test/newtest.py <pyd>m1 <pyd>m2 : : ;
54-
55-
# ----------- builtin converters -----------
56-
57-
extension builtin_converters_ext : test/test_builtin_converters.cpp <dll>bpl
58-
:
59-
:
60-
;
61-
62-
boost-python-runtest test_builtin_converters : test/test_builtin_converters.py
63-
<pyd>builtin_converters_ext
64-
:
65-
:
66-
;
67-
68-
# ----------- pointer adoption -----------
69-
70-
extension test_pointer_adoption_ext : test/test_pointer_adoption.cpp <dll>bpl
71-
:
72-
:
73-
;
74-
75-
boost-python-runtest test_pointer_adoption : test/test_pointer_adoption.py
76-
<pyd>test_pointer_adoption_ext
77-
:
78-
:
79-
;
80-
81-
}
82-
83-
unit-test indirect_traits_test
84-
: test/indirect_traits_test.cpp : <include>$(BOOST_ROOT) ;
85-
unit-test destroy_test
86-
: test/destroy_test.cpp : <include>$(BOOST_ROOT) ;
87-
unit-test pointer_type_id_test
88-
: test/pointer_type_id_test.cpp : <include>$(BOOST_ROOT) ;
89-
90-
unit-test select_from_python_test
91-
: test/select_from_python_test.cpp
7+
dll bpl
8+
:
9+
src/converter/from_python.cpp
10+
src/converter/registry.cpp
9211
src/converter/type_id.cpp
93-
src/converter/registry.cpp # MWerks needs this for some reason
94-
: $(PYTHON_PROPERTIES)
12+
src/object/class.cpp
13+
src/object/function.cpp
14+
src/object/inheritance.cpp
15+
src/object/life_support.cpp
16+
src/errors.cpp
17+
src/module.cpp
18+
src/objects.cpp
19+
src/converter/builtin_converters.cpp
20+
src/converter/callback.cpp
21+
:
22+
$(BOOST_PYTHON_V2_PROPERTIES)
23+
<define>BOOST_PYTHON_SOURCE
9524
;
96-

include/boost/python/converter/builtin_converters.hpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# define BUILTIN_CONVERTERS_DWA2002124_HPP
88
# include <boost/python/detail/wrap_python.hpp>
99
# include <boost/python/detail/none.hpp>
10+
# include <boost/python/reference.hpp>
1011
# include <string>
1112
# include <complex>
1213

@@ -23,6 +24,11 @@ namespace detail
2324
};
2425
}
2526

27+
namespace converter
28+
{
29+
template <class T> struct callback_to_python;
30+
}
31+
2632
# define BOOST_PYTHON_TO_PYTHON_BY_VALUE(T, expr) \
2733
template <> struct to_python_value<T&> \
2834
: detail::builtin_to_python \
@@ -39,7 +45,19 @@ namespace detail
3945
{ \
4046
return (expr); \
4147
} \
42-
};
48+
}; \
49+
namespace converter \
50+
{ \
51+
template <> struct callback_to_python< T > \
52+
{ \
53+
callback_to_python(T const& x) \
54+
: m_held(expr) {} \
55+
PyObject* get() const \
56+
{ return m_held.get(); } \
57+
private: \
58+
ref m_held; \
59+
}; \
60+
}
4361

4462

4563
# define BOOST_PYTHON_TO_INT(T) \
@@ -53,12 +71,12 @@ BOOST_PYTHON_TO_INT(int)
5371
BOOST_PYTHON_TO_INT(long)
5472
# undef BOOST_TO_PYTHON_INT
5573

56-
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, x ? PyString_FromString(x) : detail::none())
74+
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, x ? PyString_FromString(x) : boost::python::detail::none())
5775
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, PyString_FromString(x.c_str()))
5876
BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, PyFloat_FromDouble(x))
5977
BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, PyFloat_FromDouble(x))
6078
BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, PyFloat_FromDouble(x))
61-
BOOST_PYTHON_TO_PYTHON_BY_VALUE(PyObject*, x ? x : detail::none())
79+
BOOST_PYTHON_TO_PYTHON_BY_VALUE(PyObject*, (x ? x : boost::python::detail::none()))
6280
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<float>, PyComplex_FromDoubles(x.real(), x.imag()))
6381
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<double>, PyComplex_FromDoubles(x.real(), x.imag()))
6482
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::complex<long double>, PyComplex_FromDoubles(x.real(), x.imag()))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 CALLBACK_TO_PYTHON_BASE_DWA200237_HPP
7+
# define CALLBACK_TO_PYTHON_BASE_DWA200237_HPP
8+
# include <boost/python/converter/to_python_function_type.hpp>
9+
# include <boost/python/detail/wrap_python.hpp>
10+
# include <boost/python/reference.hpp>
11+
12+
namespace boost { namespace python { namespace converter {
13+
14+
namespace detail
15+
{
16+
struct callback_to_python_holder
17+
{
18+
callback_to_python_holder(PyObject* obj);
19+
inline PyObject* get() const;
20+
private:
21+
ref m_held;
22+
};
23+
24+
struct BOOST_PYTHON_DECL callback_to_python_base : callback_to_python_holder
25+
{
26+
callback_to_python_base(void const volatile* source, to_python_function_t);
27+
};
28+
29+
//
30+
// implmentation
31+
//
32+
inline callback_to_python_holder::callback_to_python_holder(PyObject* obj)
33+
: m_held(obj)
34+
{
35+
}
36+
37+
inline PyObject* callback_to_python_holder::get() const
38+
{
39+
return m_held.get();
40+
}
41+
}
42+
43+
}}} // namespace boost::python::converter
44+
45+
#endif // CALLBACK_TO_PYTHON_BASE_DWA200237_HPP

include/boost/python/converter/find_from_python.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@
88

99
# include <boost/python/detail/config.hpp>
1010
# include <boost/python/detail/wrap_python.hpp>
11+
# include <boost/python/converter/from_python_stage1_data.hpp>
1112

1213
namespace boost { namespace python { namespace converter {
1314

1415
struct lvalue_from_python_registration;
1516
struct rvalue_from_python_registration;
16-
struct rvalue_stage1_data;
1717

1818
BOOST_PYTHON_DECL void* find(
1919
PyObject* source, lvalue_from_python_registration const*);
20-
BOOST_PYTHON_DECL void* find(
21-
PyObject* source, rvalue_from_python_registration const*, rvalue_stage1_data&);
20+
21+
BOOST_PYTHON_DECL rvalue_stage1_data find(
22+
PyObject* source, rvalue_from_python_registration const*);
2223

2324
}}} // namespace boost::python::converter
2425

include/boost/python/converter/from_python.hpp

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
# include <boost/python/converter/find_from_python.hpp>
1010
# include <boost/python/detail/wrap_python.hpp>
11-
# include <boost/python/detail/destroy.hpp>
1211
# include <boost/type_traits/transform_traits.hpp>
1312
# include <boost/type_traits/cv_traits.hpp>
1413
# include <boost/python/converter/pointer_type_id.hpp>
1514
# include <boost/python/converter/from_python_data.hpp>
1615
# include <boost/mpl/select_type.hpp>
1716
# 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>
1819

1920
namespace boost { namespace python { namespace converter {
2021

@@ -42,8 +43,6 @@ struct pointer_const_reference_from_python
4243

4344
private:
4445
typename detail::referent_storage<T>::type m_result;
45-
46-
static lvalue_from_python_registration*& chain;
4746
};
4847

4948
// Used when T == U*
@@ -52,8 +51,6 @@ struct pointer_from_python : from_python_base
5251
{
5352
pointer_from_python(PyObject*);
5453
T operator()(PyObject*) const;
55-
56-
static lvalue_from_python_registration*& chain;
5754
};
5855

5956
// Used when T == U& and (T != V const& or T == W volatile&)
@@ -62,8 +59,6 @@ struct reference_from_python : from_python_base
6259
{
6360
reference_from_python(PyObject*);
6461
T operator()(PyObject*) const;
65-
66-
static lvalue_from_python_registration*& chain;
6762
};
6863

6964
// ------- rvalue converters ---------
@@ -79,14 +74,12 @@ class rvalue_from_python
7974

8075
public:
8176
rvalue_from_python(PyObject*);
82-
~rvalue_from_python();
8377
bool convertible() const;
8478

8579
result_type operator()(PyObject*);
8680

8781
private:
8882
rvalue_data<result_type> m_data;
89-
static rvalue_from_python_registration*& chain;
9083
};
9184

9285
template <class T>
@@ -191,7 +184,7 @@ inline pointer_const_reference_from_python<T>::pointer_const_reference_from_pyth
191184
{
192185
detail::write_void_ptr_reference(
193186
m_result.bytes
194-
, p == Py_None ? p : find(p, chain)
187+
, p == Py_None ? p : find(p, lvalue_from_python_chain<T>::value)
195188
, (T(*)())0);
196189
}
197190

@@ -208,15 +201,11 @@ inline T pointer_const_reference_from_python<T>::operator()(PyObject* p) const
208201
: detail::void_ptr_to_reference(m_result.bytes, (T(*)())0);
209202
}
210203

211-
template <class T>
212-
lvalue_from_python_registration*& pointer_const_reference_from_python<T>::chain
213-
= registry::lvalue_converters(pointer_type_id<T>());
214-
215204
// --------
216205

217206
template <class T>
218207
inline pointer_from_python<T>::pointer_from_python(PyObject* p)
219-
: from_python_base(p == Py_None ? p : find(p, chain))
208+
: from_python_base(p == Py_None ? p : find(p, lvalue_from_python_chain<T>::value))
220209
{
221210
}
222211

@@ -226,15 +215,11 @@ inline T pointer_from_python<T>::operator()(PyObject* p) const
226215
return (p == Py_None) ? 0 : T(result());
227216
}
228217

229-
template <class T>
230-
lvalue_from_python_registration*& pointer_from_python<T>::chain
231-
= registry::lvalue_converters(pointer_type_id<T>());
232-
233218
// --------
234219

235220
template <class T>
236221
inline reference_from_python<T>::reference_from_python(PyObject* p)
237-
: from_python_base(find(p,chain))
222+
: from_python_base(find(p,lvalue_from_python_chain<T>::value))
238223
{
239224
}
240225

@@ -244,23 +229,12 @@ inline T reference_from_python<T>::operator()(PyObject*) const
244229
return detail::void_ptr_to_reference(result(), (T(*)())0);
245230
}
246231

247-
template <class T>
248-
lvalue_from_python_registration*& reference_from_python<T>::chain
249-
= registry::lvalue_converters(undecorated_type_id<T>());
250-
251232
// -------
252233

253234
template <class T>
254235
inline rvalue_from_python<T>::rvalue_from_python(PyObject* obj)
236+
: m_data(find(obj, rvalue_from_python_chain<T>::value))
255237
{
256-
find(obj, chain, m_data.stage1);
257-
}
258-
259-
template <class T>
260-
inline rvalue_from_python<T>::~rvalue_from_python()
261-
{
262-
if (m_data.stage1.convertible == m_data.storage.bytes)
263-
python::detail::destroy_reference<result_type>(m_data.storage.bytes);
264238
}
265239

266240
template <class T>
@@ -279,10 +253,6 @@ rvalue_from_python<T>::operator()(PyObject* p)
279253
return detail::void_ptr_to_reference(m_data.stage1.convertible, (result_type(*)())0);
280254
}
281255

282-
template <class T>
283-
rvalue_from_python_registration*& rvalue_from_python<T>::chain
284-
= registry::rvalue_converters(undecorated_type_id<T>());
285-
286256
}}} // namespace boost::python::converter
287257

288258
#endif // FROM_PYTHON_DWA2002127_HPP

0 commit comments

Comments
 (0)