Skip to content

Commit 3caa91c

Browse files
committed
More fixes
[SVN r13182]
1 parent 0bdf354 commit 3caa91c

File tree

5 files changed

+150
-34
lines changed

5 files changed

+150
-34
lines changed

include/boost/python/class.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# include <boost/python/object/value_holder_fwd.hpp>
1313
# include <boost/python/converter/type_id.hpp>
1414
# include <boost/python/detail/wrap_function.hpp>
15+
# include <boost/python/detail/member_function_cast.hpp>
1516
# include <boost/mpl/type_list.hpp>
1617
# include <boost/python/object/class_converters.hpp>
1718
# include <boost/mpl/size.hpp>
@@ -103,14 +104,24 @@ class class_ : private objects::class_base
103104
// Use function::add_to_namespace to achieve overloading if
104105
// appropriate.
105106
objects::function::add_to_namespace(
106-
this->object(), name, ref(detail::wrap_function(f)));
107+
this->object(), name,
108+
ref(detail::wrap_function(
109+
// This bit of nastiness casts F to a member function of T if possible.
110+
detail::member_function_cast<T,F>::stage1(f).stage2((T*)0).stage3(f)
111+
)));
107112
return *this;
108113
}
109114

110115
template <class Fn, class CallPolicy>
111116
self& def(char const* name, Fn fn, CallPolicy policy)
112117
{
113-
this->def(name, boost::python::make_function(fn, policy));
118+
this->def(name
119+
, boost::python::make_function(
120+
// This bit of nastiness casts F to a member function of T if possible.
121+
detail::member_function_cast<T,Fn>::stage1(fn).stage2((T*)0).stage3(fn)
122+
, policy)
123+
);
124+
114125
return *this;
115126
}
116127

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 FIND_INSTANCE_DWA2002312_HPP
7+
# define FIND_INSTANCE_DWA2002312_HPP
8+
9+
namespace boost { namespace python { namespace objects {
10+
11+
// Given an undecorated type_id, find the instance data which
12+
// corresponds to it, or return 0 in case no such type is held.
13+
BOOST_PYTHON_DECL void* find_instance_impl(PyObject*, converter::undecorated_type_id_t);
14+
15+
// This produces a function with the right signature for use in from_python conversions
16+
template <class T>
17+
struct instance_finder
18+
{
19+
instance_finder()
20+
{
21+
converter::registry::insert(&execute, converter::undecorated_type_id<T>());
22+
}
23+
24+
static instance_finder const registration;
25+
private:
26+
static inline void* execute(PyObject* p)
27+
{
28+
return find_instance_impl(p, converter::undecorated_type_id<T>());
29+
}
30+
};
31+
32+
template <class T>
33+
instance_finder<T> const instance_finder<T>::registration;
34+
35+
}}} // namespace boost::python::objects
36+
37+
#endif // FIND_INSTANCE_DWA2002312_HPP

include/boost/python/object/pointer_holder.hpp

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# include <boost/python/object/class.hpp>
1010
# include <boost/python/converter/type_id.hpp>
1111
# include <boost/python/object/inheritance.hpp>
12+
# include <boost/python/object/find_instance.hpp>
1213
# include <boost/ref.hpp>
1314
# include <boost/type.hpp>
1415
# include <boost/mpl/select_type.hpp>
@@ -153,67 +154,88 @@ struct pointer_holder_back_reference : instance_holder
153154

154155
// Forward construction to the held object
155156
pointer_holder_back_reference(PyObject* p)
156-
: m_p(new Value(p)) {}
157+
: m_p(new BackReferenceType(p)) {
158+
(void)instance_finder<BackReferenceType>::registration;
159+
}
160+
157161

158162

159163
template <class A1>
160164
pointer_holder_back_reference(PyObject* p, A1 a1)
161-
: m_p(new Value(p
165+
: m_p(new BackReferenceType(p
162166
, (typename unwrap_reference<A1>::type&)(a1)
163167
))
164-
{}
168+
{
169+
(void)instance_finder<BackReferenceType>::registration;
170+
}
171+
165172

166173
template <class A1, class A2>
167174
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2)
168-
: m_p(new Value(p
175+
: m_p(new BackReferenceType(p
169176
, (typename unwrap_reference<A1>::type&)(a1)
170177
, (typename unwrap_reference<A2>::type&)(a2)
171178
))
172-
{}
179+
{
180+
(void)instance_finder<BackReferenceType>::registration;
181+
}
182+
173183

174184
template <class A1, class A2, class A3>
175185
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3)
176-
: m_p(new Value(p
186+
: m_p(new BackReferenceType(p
177187
, (typename unwrap_reference<A1>::type&)(a1)
178188
, (typename unwrap_reference<A2>::type&)(a2)
179189
, (typename unwrap_reference<A3>::type&)(a3)
180190
))
181-
{}
191+
{
192+
(void)instance_finder<BackReferenceType>::registration;
193+
}
194+
182195

183196
template <class A1, class A2, class A3, class A4>
184197
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4)
185-
: m_p(new Value(p
198+
: m_p(new BackReferenceType(p
186199
, (typename unwrap_reference<A1>::type&)(a1)
187200
, (typename unwrap_reference<A2>::type&)(a2)
188201
, (typename unwrap_reference<A3>::type&)(a3)
189202
, (typename unwrap_reference<A4>::type&)(a4)
190203
))
191-
{}
204+
{
205+
(void)instance_finder<BackReferenceType>::registration;
206+
}
207+
192208

193209
template <class A1, class A2, class A3, class A4, class A5>
194210
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
195-
: m_p(new Value(p
211+
: m_p(new BackReferenceType(p
196212
, (typename unwrap_reference<A1>::type&)(a1)
197213
, (typename unwrap_reference<A2>::type&)(a2)
198214
, (typename unwrap_reference<A3>::type&)(a3)
199215
, (typename unwrap_reference<A4>::type&)(a4)
200216
, (typename unwrap_reference<A5>::type&)(a5)
201-
)) {}
217+
)) {
218+
(void)instance_finder<BackReferenceType>::registration;
219+
}
220+
202221

203222
template <class A1, class A2, class A3, class A4, class A5, class A6>
204223
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6)
205-
: m_p(new Value(p
224+
: m_p(new BackReferenceType(p
206225
, (typename unwrap_reference<A1>::type&)(a1)
207226
, (typename unwrap_reference<A2>::type&)(a2)
208227
, (typename unwrap_reference<A3>::type&)(a3)
209228
, (typename unwrap_reference<A4>::type&)(a4)
210229
, (typename unwrap_reference<A5>::type&)(a5)
211230
, (typename unwrap_reference<A6>::type&)(a6)
212-
)) {}
231+
)) {
232+
(void)instance_finder<BackReferenceType>::registration;
233+
}
234+
213235

214236
template <class A1, class A2, class A3, class A4, class A5, class A6, class A7>
215237
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7)
216-
: m_p(new Value(p
238+
: m_p(new BackReferenceType(p
217239
, (typename unwrap_reference<A1>::type&)(a1)
218240
, (typename unwrap_reference<A2>::type&)(a2)
219241
, (typename unwrap_reference<A3>::type&)(a3)
@@ -222,11 +244,14 @@ struct pointer_holder_back_reference : instance_holder
222244
, (typename unwrap_reference<A6>::type&)(a6)
223245
, (typename unwrap_reference<A7>::type&)(a7)
224246
))
225-
{}
247+
{
248+
(void)instance_finder<BackReferenceType>::registration;
249+
}
250+
226251

227252
template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8>
228253
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8)
229-
: m_p(new Value(p
254+
: m_p(new BackReferenceType(p
230255
, (typename unwrap_reference<A1>::type&)(a1)
231256
, (typename unwrap_reference<A2>::type&)(a2)
232257
, (typename unwrap_reference<A3>::type&)(a3)
@@ -236,11 +261,14 @@ struct pointer_holder_back_reference : instance_holder
236261
, (typename unwrap_reference<A7>::type&)(a7)
237262
, (typename unwrap_reference<A8>::type&)(a8)
238263
))
239-
{}
264+
{
265+
(void)instance_finder<BackReferenceType>::registration;
266+
}
267+
240268

241269
template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9>
242270
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9)
243-
: m_p(new Value(p
271+
: m_p(new BackReferenceType(p
244272
, (typename unwrap_reference<A1>::type&)(a1)
245273
, (typename unwrap_reference<A2>::type&)(a2)
246274
, (typename unwrap_reference<A3>::type&)(a3)
@@ -251,11 +279,14 @@ struct pointer_holder_back_reference : instance_holder
251279
, (typename unwrap_reference<A8>::type&)(a8)
252280
, (typename unwrap_reference<A9>::type&)(a9)
253281
))
254-
{}
282+
{
283+
(void)instance_finder<BackReferenceType>::registration;
284+
}
285+
255286

256287
template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9, class A10>
257288
pointer_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10)
258-
: m_p(new Value(p
289+
: m_p(new BackReferenceType(p
259290
, (typename unwrap_reference<A1>::type&)(a1)
260291
, (typename unwrap_reference<A2>::type&)(a2)
261292
, (typename unwrap_reference<A3>::type&)(a3)
@@ -267,7 +298,10 @@ struct pointer_holder_back_reference : instance_holder
267298
, (typename unwrap_reference<A9>::type&)(a9)
268299
, (typename unwrap_reference<A10>::type&)(a10)
269300
))
270-
{}
301+
{
302+
(void)instance_finder<BackReferenceType>::registration;
303+
}
304+
271305

272306
private: // required holder implementation
273307
void* holds(converter::undecorated_type_id_t);

include/boost/python/object/value_holder.hpp

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# include <boost/python/object/class.hpp>
1111
# include <boost/python/converter/type_id.hpp>
1212
# include <boost/python/object/inheritance.hpp>
13+
# include <boost/python/object/find_instance.hpp>
1314
# include <boost/ref.hpp>
1415

1516
namespace boost { namespace python { namespace objects {
@@ -146,22 +147,31 @@ struct value_holder_back_reference : instance_holder
146147
{
147148
// Forward construction to the held object
148149
value_holder_back_reference(PyObject* p)
149-
: m_held() {}
150+
: m_held() {
151+
(void)instance_finder<BackReferenceType>::registration;
152+
}
153+
150154

151155
template <class A1>
152156
value_holder_back_reference(PyObject* p, A1 a1)
153157
: m_held(p
154158
, (typename unwrap_reference<A1>::type&)(a1)
155159
)
156-
{}
160+
{
161+
(void)instance_finder<BackReferenceType>::registration;
162+
}
163+
157164

158165
template <class A1, class A2>
159166
value_holder_back_reference(PyObject* p, A1 a1, A2 a2)
160167
: m_held(p
161168
, (typename unwrap_reference<A1>::type&)(a1)
162169
, (typename unwrap_reference<A2>::type&)(a2)
163170
)
164-
{}
171+
{
172+
(void)instance_finder<BackReferenceType>::registration;
173+
}
174+
165175

166176
template <class A1, class A2, class A3>
167177
value_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3)
@@ -170,7 +180,10 @@ struct value_holder_back_reference : instance_holder
170180
, (typename unwrap_reference<A2>::type&)(a2)
171181
, (typename unwrap_reference<A3>::type&)(a3)
172182
)
173-
{}
183+
{
184+
(void)instance_finder<BackReferenceType>::registration;
185+
}
186+
174187

175188
template <class A1, class A2, class A3, class A4>
176189
value_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4)
@@ -180,7 +193,10 @@ struct value_holder_back_reference : instance_holder
180193
, (typename unwrap_reference<A3>::type&)(a3)
181194
, (typename unwrap_reference<A4>::type&)(a4)
182195
)
183-
{}
196+
{
197+
(void)instance_finder<BackReferenceType>::registration;
198+
}
199+
184200

185201
template <class A1, class A2, class A3, class A4, class A5>
186202
value_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
@@ -190,7 +206,10 @@ struct value_holder_back_reference : instance_holder
190206
, (typename unwrap_reference<A3>::type&)(a3)
191207
, (typename unwrap_reference<A4>::type&)(a4)
192208
, (typename unwrap_reference<A5>::type&)(a5)
193-
) {}
209+
) {
210+
(void)instance_finder<BackReferenceType>::registration;
211+
}
212+
194213

195214
template <class A1, class A2, class A3, class A4, class A5, class A6>
196215
value_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6)
@@ -201,7 +220,10 @@ struct value_holder_back_reference : instance_holder
201220
, (typename unwrap_reference<A4>::type&)(a4)
202221
, (typename unwrap_reference<A5>::type&)(a5)
203222
, (typename unwrap_reference<A6>::type&)(a6)
204-
) {}
223+
) {
224+
(void)instance_finder<BackReferenceType>::registration;
225+
}
226+
205227

206228
template <class A1, class A2, class A3, class A4, class A5, class A6, class A7>
207229
value_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7)
@@ -214,7 +236,10 @@ struct value_holder_back_reference : instance_holder
214236
, (typename unwrap_reference<A6>::type&)(a6)
215237
, (typename unwrap_reference<A7>::type&)(a7)
216238
)
217-
{}
239+
{
240+
(void)instance_finder<BackReferenceType>::registration;
241+
}
242+
218243

219244
template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8>
220245
value_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8)
@@ -228,7 +253,10 @@ struct value_holder_back_reference : instance_holder
228253
, (typename unwrap_reference<A7>::type&)(a7)
229254
, (typename unwrap_reference<A8>::type&)(a8)
230255
)
231-
{}
256+
{
257+
(void)instance_finder<BackReferenceType>::registration;
258+
}
259+
232260

233261
template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9>
234262
value_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9)
@@ -243,7 +271,10 @@ struct value_holder_back_reference : instance_holder
243271
, (typename unwrap_reference<A8>::type&)(a8)
244272
, (typename unwrap_reference<A9>::type&)(a9)
245273
)
246-
{}
274+
{
275+
(void)instance_finder<BackReferenceType>::registration;
276+
}
277+
247278

248279
template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9, class A10>
249280
value_holder_back_reference(PyObject* p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10)
@@ -259,7 +290,9 @@ struct value_holder_back_reference : instance_holder
259290
, (typename unwrap_reference<A9>::type&)(a9)
260291
, (typename unwrap_reference<A10>::type&)(a10)
261292
)
262-
{}
293+
{
294+
(void)instance_finder<BackReferenceType>::registration;
295+
}
263296

264297
private: // required holder implementation
265298
void* holds(converter::undecorated_type_id_t);

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ bpl-test try : newtest.py m1.cpp m2.cpp ;
5555
bpl-test builtin_converters : test_builtin_converters.py test_builtin_converters.cpp ;
5656
bpl-test test_pointer_adoption ;
5757
bpl-test callbacks ;
58+
bpl-test virtual_functions ;
5859

5960
# --- unit tests of library components ---
6061
unit-test indirect_traits_test

0 commit comments

Comments
 (0)