Skip to content

Commit 7f98265

Browse files
committed
Merge Joel's changes to trunk!
[SVN r15430]
1 parent 0b75a8e commit 7f98265

29 files changed

+526
-369
lines changed

include/boost/python/class.hpp

Lines changed: 46 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace detail
4646
struct write_type_id
4747
{
4848
write_type_id(type_info**p) : p(p) {}
49-
49+
5050
// Here's the runtime behavior
5151
template <class T>
5252
void operator()(T*) const
@@ -85,15 +85,7 @@ namespace detail
8585
SelectHolder::register_();
8686
}
8787

88-
template <class T>
89-
struct assert_default_constructible
90-
{
91-
static int check2(T const&);
92-
static int check()
93-
{
94-
return sizeof(check2(T()));
95-
}
96-
};
88+
template <class T> int assert_default_constructible(T const&);
9789
}
9890

9991
//
@@ -153,8 +145,8 @@ class class_ : public objects::class_base
153145
public:
154146
// Automatically derive the class name - only works on some
155147
// compilers because type_info::name is sometimes mangled (gcc)
156-
class_(); // With default-constructor init function
157-
class_(no_init_t); // With no init function
148+
// class_(); // With default-constructor init function
149+
// class_(no_init_t); // With no init function
158150

159151
// Construct with the class name, with or without docstring, and default init() function
160152
class_(char const* name, char const* doc = 0);
@@ -165,22 +157,21 @@ class class_ : public objects::class_base
165157
// Construct with class name, docstring, and no init() function
166158
class_(char const* name, char const* doc, no_init_t);
167159

168-
template <class InitArgs>
169-
inline class_(char const* name, detail::args_base<InitArgs> const&)
160+
template <class DerivedT>
161+
inline class_(char const* name, init_base<DerivedT> const& i)
170162
: base(name, id_vector::size, id_vector().ids)
171163
{
172164
this->register_();
173-
this->def_init(InitArgs());
165+
define_init(*this, i.derived());
174166
this->set_instance_size(holder_selector::additional_size());
175167
}
176168

177-
178-
template <class InitArgs>
179-
inline class_(char const* name, char const* doc, detail::args_base<InitArgs> const&, char const* initdoc = 0)
169+
template <class DerivedT>
170+
inline class_(char const* name, char const* doc, init_base<DerivedT> const& i)
180171
: base(name, id_vector::size, id_vector().ids, doc)
181172
{
182173
this->register_();
183-
this->def_init(InitArgs(), initdoc);
174+
define_init(*this, i.derived());
184175
this->set_instance_size(holder_selector::additional_size());
185176
}
186177

@@ -194,58 +185,34 @@ class class_ : public objects::class_base
194185
return *this;
195186
}
196187

197-
template <BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, class T)>
198-
self& def(init<BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, T)> const& i)
199-
{
200-
define_init(*this, i, default_call_policies(), 0);
201-
return *this;
202-
}
203-
204-
template <class CallPolicyOrDoc, BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, class T)>
205-
self& def(
206-
init<BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, T)> const& i,
207-
CallPolicyOrDoc const& policy_or_doc,
208-
char const* doc = 0)
188+
template <class DerivedT>
189+
self& def(init_base<DerivedT> const& i)
209190
{
210-
typedef detail::def_helper<CallPolicyOrDoc> helper;
211-
define_init(*this, i,
212-
helper::get_policy(policy_or_doc),
213-
helper::get_doc(policy_or_doc, doc));
191+
define_init(*this, i.derived());
214192
return *this;
215193
}
216194

217195
template <class Arg1T, class Arg2T>
218196
self& def(char const* name, Arg1T arg1, Arg2T const& arg2)
219197
{
220198
// The arguments may be:
221-
// arg1: function or signature
222-
// arg2: policy or docstring or stubs
199+
// def(name, function)
200+
// def(name, function, policy)
201+
// def(name, function, doc_string)
202+
// def(name, signature, stubs)
223203

224-
dispatch_def(&arg2, name, arg1, arg2, (char*)0);
204+
dispatch_def(&arg2, name, arg1, arg2);
225205
return *this;
226206
}
227207

228208
template <class Arg1T, class Arg2T, class Arg3T>
229209
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3)
230-
{
231-
// The arguments may be:
232-
// arg1: function or signature
233-
// arg2: policy or docstring or stubs
234-
// arg3: policy or docstring
235-
236-
dispatch_def(&arg2, name, arg1, arg2, arg3);
237-
return *this;
238-
}
239-
240-
template <class Arg1T, class Arg2T, class Arg3T>
241-
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3, char const* doc)
242210
{
243211
// The arguments are definitely:
244-
// arg1: signature
245-
// arg2: stubs
246-
// arg3: policy
212+
// def(name, function, policy, doc_string)
213+
// def(name, function, doc_string, policy)
247214

248-
dispatch_def(&arg2, name, arg1, arg2, arg3, doc);
215+
dispatch_def(&arg2, name, arg1, arg2, arg3);
249216
return *this;
250217
}
251218

@@ -288,7 +255,6 @@ class class_ : public objects::class_base
288255
// Define the default constructor.
289256
self& def_init()
290257
{
291-
detail::assert_default_constructible<T>::check();
292258
this->def_init(mpl::list0<>::type());
293259
return *this;
294260
}
@@ -369,39 +335,46 @@ class class_ : public objects::class_base
369335

370336
inline void register_() const;
371337

338+
template <class StubsT, class SigT>
339+
void dispatch_def(
340+
detail::overloads_base const*,
341+
char const* name,
342+
SigT sig,
343+
StubsT const& stubs)
344+
{
345+
// convert sig to a type_list (see detail::get_signature in signature.hpp)
346+
// before calling detail::define_with_defaults.
347+
detail::define_with_defaults(
348+
name, stubs, *this, detail::get_signature(sig));
349+
}
350+
372351
template <class Fn, class CallPolicyOrDoc>
373352
void dispatch_def(
374353
void const*,
375354
char const* name,
376355
Fn fn,
377-
CallPolicyOrDoc const& policy_or_doc,
378-
char const* doc)
356+
CallPolicyOrDoc const& policy_or_doc)
379357
{
380358
typedef detail::def_helper<CallPolicyOrDoc> helper;
381-
382359
this->def_impl(
383360
name, fn, helper::get_policy(policy_or_doc),
384-
helper::get_doc(policy_or_doc, doc), &fn);
361+
helper::get_doc(policy_or_doc, 0), &fn);
385362

386363
}
387364

388-
template <class StubsT, class SigT, class CallPolicyOrDoc>
365+
template <class Fn, class CallPolicyOrDoc1, class CallPolicyOrDoc2>
389366
void dispatch_def(
390-
detail::func_stubs_base const*,
367+
void const*,
391368
char const* name,
392-
SigT sig,
393-
StubsT const& stubs,
394-
CallPolicyOrDoc const& policy_or_doc,
395-
char const* doc = 0)
369+
Fn fn,
370+
CallPolicyOrDoc1 const& policy_or_doc1,
371+
CallPolicyOrDoc2 const& policy_or_doc2)
396372
{
397-
typedef detail::def_helper<CallPolicyOrDoc> helper;
373+
typedef detail::def_helper<CallPolicyOrDoc1> helper;
398374

399-
// convert sig to a type_list (see detail::get_signature in signature.hpp)
400-
// before calling detail::define_with_defaults.
401-
detail::define_with_defaults(
402-
name, stubs, helper::get_policy(policy_or_doc),
403-
*this, detail::get_signature(sig),
404-
helper::get_doc(policy_or_doc, doc));
375+
this->def_impl(
376+
name, fn, helper::get_policy(policy_or_doc1, policy_or_doc2),
377+
helper::get_doc(policy_or_doc1, policy_or_doc2), &fn);
405378
}
406379
};
407380

@@ -421,30 +394,12 @@ inline void class_<T,X1,X2,X3>::register_() const
421394
);
422395
}
423396

424-
425-
426-
template <class T, class X1, class X2, class X3>
427-
inline class_<T,X1,X2,X3>::class_()
428-
: base(typeid(T).name(), id_vector::size, id_vector().ids)
429-
{
430-
this->register_();
431-
this->def_init();
432-
this->set_instance_size(holder_selector::additional_size());
433-
}
434-
435-
template <class T, class X1, class X2, class X3>
436-
inline class_<T,X1,X2,X3>::class_(no_init_t)
437-
: base(typeid(T).name(), id_vector::size, id_vector().ids)
438-
{
439-
this->register_();
440-
this->def_no_init();
441-
}
442-
443397
template <class T, class X1, class X2, class X3>
444398
inline class_<T,X1,X2,X3>::class_(char const* name, char const* doc)
445399
: base(name, id_vector::size, id_vector().ids, doc)
446400
{
447401
this->register_();
402+
detail::force_instantiate(sizeof(detail::assert_default_constructible(T())));
448403
this->def_init();
449404
this->set_instance_size(holder_selector::additional_size());
450405
}

include/boost/python/def.hpp

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,44 @@ namespace detail
2525
void const*,
2626
char const* name,
2727
Fn fn,
28-
CallPolicyOrDoc const& policy_or_doc,
29-
char const* doc)
28+
CallPolicyOrDoc const& policy_or_doc)
3029
{
3130
typedef detail::def_helper<CallPolicyOrDoc> helper;
3231

3332
detail::scope_setattr_doc(
3433
name, boost::python::make_function(fn, helper::get_policy(policy_or_doc)),
35-
helper::get_doc(policy_or_doc, doc));
34+
helper::get_doc(policy_or_doc, 0));
35+
}
36+
37+
template <class Fn, class CallPolicyOrDoc1, class CallPolicyOrDoc2>
38+
void dispatch_def(
39+
void const*,
40+
char const* name,
41+
Fn fn,
42+
CallPolicyOrDoc1 const& policy_or_doc1,
43+
CallPolicyOrDoc2 const& policy_or_doc2)
44+
{
45+
typedef detail::def_helper<CallPolicyOrDoc1> helper;
46+
47+
detail::scope_setattr_doc(
48+
name, boost::python::make_function(
49+
fn, helper::get_policy(policy_or_doc1, policy_or_doc2)),
50+
helper::get_doc(policy_or_doc1, policy_or_doc2));
3651
}
3752

38-
template <class StubsT, class SigT, class CallPolicyOrDoc>
53+
template <class StubsT, class SigT>
3954
void dispatch_def(
40-
detail::func_stubs_base const*,
55+
detail::overloads_base const*,
4156
char const* name,
4257
SigT sig,
43-
StubsT const& stubs,
44-
CallPolicyOrDoc const& policy_or_doc,
45-
char const* doc = 0)
58+
StubsT const& stubs)
4659
{
47-
typedef detail::def_helper<CallPolicyOrDoc> helper;
48-
4960
// convert sig to a type_list (see detail::get_signature in signature.hpp)
5061
// before calling detail::define_with_defaults.
5162

5263
scope current;
5364
detail::define_with_defaults(
54-
name, stubs, helper::get_policy(policy_or_doc),
55-
current, detail::get_signature(sig),
56-
helper::get_doc(policy_or_doc, doc));
65+
name, stubs, current, detail::get_signature(sig));
5766
}
5867
}
5968

@@ -67,33 +76,33 @@ template <class Arg1T, class Arg2T>
6776
void def(char const* name, Arg1T arg1, Arg2T const& arg2)
6877
{
6978
// The arguments may be:
70-
// arg1: function or signature
71-
// arg2: policy or docstring or stubs
79+
// def(name, function)
80+
// def(name, function, policy)
81+
// def(name, function, doc_string)
82+
// def(name, signature, stubs)
7283

73-
detail::dispatch_def(&arg2, name, arg1, arg2, (char*)0);
84+
detail::dispatch_def(&arg2, name, arg1, arg2);
7485
}
7586

7687
template <class Arg1T, class Arg2T, class Arg3T>
7788
void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3)
7889
{
79-
// The arguments may be:
80-
// arg1: function or signature
81-
// arg2: policy or docstring or stubs
82-
// arg3: policy or docstring
90+
// The arguments are definitely:
91+
// def(name, function, policy, doc_string) // TODO: exchange policy, doc_string position
8392

8493
detail::dispatch_def(&arg2, name, arg1, arg2, arg3);
8594
}
8695

87-
template <class Arg1T, class Arg2T, class Arg3T>
88-
void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3, char const* doc)
89-
{
90-
// The arguments are definitely:
91-
// arg1: signature
92-
// arg2: stubs
93-
// arg3: policy
94-
95-
detail::dispatch_def(&arg2, name, arg1, arg2, arg3, doc);
96-
}
96+
//template <class Arg1T, class Arg2T, class Arg3T>
97+
//void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3, char const* doc)
98+
//{
99+
// // The arguments are definitely:
100+
// // arg1: signature
101+
// // arg2: stubs
102+
// // arg3: policy
103+
//
104+
// detail::dispatch_def(&arg2, name, arg1, arg2, arg3, doc);
105+
//}
97106

98107
}} // namespace boost::python
99108

include/boost/python/detail/def_helper.hpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,33 @@ template <bool is_string = false>
3232
struct def_helper_impl
3333
{
3434
template <class P>
35-
static P const& get_policy(P const& x) { return x; }
35+
static P const&
36+
get_policy(P const& x) { return x; }
37+
38+
template <class P1, class P2>
39+
static P1 const&
40+
get_policy(P1 const& x, P2 const&) { return x; } // select left
3641

3742
template <class P>
38-
static char const* get_doc(P const&, char const* doc) { return doc; }
43+
static char const*
44+
get_doc(P const&, char const* doc) { return doc; } // select right
3945
};
4046

4147
template <>
4248
struct def_helper_impl<true>
4349
{
44-
static python::default_call_policies get_policy(char const*) { return default_call_policies(); }
45-
static char const* get_doc(char const* doc, char const*) { return doc; }
50+
static python::default_call_policies
51+
get_policy(char const*)
52+
{ return default_call_policies(); }
53+
54+
template <class P1, class P2>
55+
static P2 const&
56+
get_policy(P1 const&, P2 const& y) { return y; } // select right
57+
58+
template <class P>
59+
static char const*
60+
get_doc(char const* doc, P const&) // select left
61+
{ return doc; }
4662
};
4763

4864
template <class T>

0 commit comments

Comments
 (0)