Skip to content

Commit 78ae892

Browse files
committed
Committed the defaults stuff
(integrated from v2-dev branch) [SVN r15008]
1 parent d748e37 commit 78ae892

3 files changed

Lines changed: 96 additions & 39 deletions

File tree

include/boost/python/class.hpp

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@
2929
# include <boost/python/object/add_to_namespace.hpp>
3030
# include <boost/python/detail/def_helper.hpp>
3131
# include <boost/python/detail/force_instantiate.hpp>
32+
# include <boost/python/detail/defaults_def.hpp>
33+
# include <boost/python/signature.hpp>
3234

33-
namespace boost { namespace python {
35+
namespace boost { namespace python {
3436

3537
namespace detail
3638
{
3739
struct write_type_id;
38-
40+
3941
template <class T, class Prev = detail::not_specified>
4042
struct select_held_type;
41-
43+
4244
template <class T1, class T2, class T3>
4345
struct has_noncopyable;
4446

@@ -83,21 +85,21 @@ class class_ : public objects::class_base
8385

8486
typedef class_<T,X1,X2,X3> self;
8587
BOOST_STATIC_CONSTANT(bool, is_copyable = (!detail::has_noncopyable<X1,X2,X3>::value));
86-
88+
8789
typedef typename detail::select_held_type<
8890
X1, typename detail::select_held_type<
8991
X2, typename detail::select_held_type<
9092
X3
9193
>::type>::type>::type held_type;
92-
94+
9395
typedef objects::class_id class_id;
94-
96+
9597
typedef typename detail::select_bases<X1
9698
, typename detail::select_bases<X2
9799
, typename boost::python::detail::select_bases<X3>::type
98100
>::type
99101
>::type bases;
100-
102+
101103
// A helper class which will contain an array of id objects to be
102104
// passed to the base class constructor
103105
struct id_vector
@@ -107,12 +109,12 @@ class class_ : public objects::class_base
107109
{
108110
// Stick the derived class id into the first element of the array
109111
ids[0] = type_id<T>();
110-
112+
111113
// Write the rest of the elements into succeeding positions.
112114
class_id* p = ids + 1;
113115
mpl::for_each<bases, void, detail::write_type_id>::execute(&p);
114116
}
115-
117+
116118
BOOST_STATIC_CONSTANT(
117119
std::size_t, size = mpl::size<bases>::value + 1);
118120
class_id ids[size];
@@ -124,13 +126,13 @@ class class_ : public objects::class_base
124126
// compilers because type_info::name is sometimes mangled (gcc)
125127
class_(); // With default-constructor init function
126128
class_(no_init_t); // With no init function
127-
129+
128130
// Construct with the class name, with or without docstring, and default init() function
129131
class_(char const* name, char const* doc = 0);
130132

131133
// Construct with class name, no docstring, and no init() function
132134
class_(char const* name, no_init_t);
133-
135+
134136
// Construct with class name, docstring, and no init() function
135137
class_(char const* name, char const* doc, no_init_t);
136138

@@ -141,16 +143,16 @@ class class_ : public objects::class_base
141143
this->register_();
142144
this->def_init(InitArgs());
143145
}
144-
145-
146+
147+
146148
template <class InitArgs>
147149
inline class_(char const* name, char const* doc, detail::args_base<InitArgs> const&, char const* initdoc = 0)
148150
: base(name, id_vector::size, id_vector().ids, doc)
149151
{
150152
this->register_();
151153
this->def_init(InitArgs(), initdoc);
152154
}
153-
155+
154156
// Wrap a member function or a non-member function which can take
155157
// a T, T cv&, or T cv* as its first parameter, or a callable
156158
// python object.
@@ -161,14 +163,10 @@ class class_ : public objects::class_base
161163
return *this;
162164
}
163165

164-
template <class Fn, class CallPolicyOrDoc>
165-
self& def(char const* name, Fn fn, CallPolicyOrDoc const& policy_or_doc, char const* doc = 0)
166+
template <class Arg1T, class Arg2T>
167+
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0)
166168
{
167-
typedef detail::def_helper<CallPolicyOrDoc> helper;
168-
169-
this->def_impl(
170-
name, fn, helper::get_policy(policy_or_doc), helper::get_doc(policy_or_doc, doc), &fn);
171-
169+
dispatch_def(name, arg1, arg2, doc, &arg2);
172170
return *this;
173171
}
174172

@@ -178,7 +176,7 @@ class class_ : public objects::class_base
178176
typedef detail::operator_<id,L,R> op_t;
179177
return this->def(op.name(), &op_t::template apply<T>::execute);
180178
}
181-
179+
182180
// Define the constructor with the given Args, which should be an
183181
// MPL sequence of types.
184182
template <class Args>
@@ -196,7 +194,7 @@ class class_ : public objects::class_base
196194
self& def_init(Args const&, CallPolicyOrDoc const& policy_or_doc, char const* doc = 0)
197195
{
198196
typedef detail::def_helper<CallPolicyOrDoc> helper;
199-
197+
200198
return this->def(
201199
"__init__",
202200
python::make_constructor<Args>(
@@ -265,7 +263,7 @@ class class_ : public objects::class_base
265263
objects::add_to_namespace(
266264
*this, name,
267265
make_function(
268-
// This bit of nastiness casts F to a member function of T if possible.
266+
// This bit of nastiness casts F to a member function of T if possible.
269267
detail::member_function_cast<T,Fn>::stage1(fn).stage2((T*)0).stage3(fn)
270268
, policies)
271269
, doc);
@@ -279,6 +277,34 @@ class class_ : public objects::class_base
279277
}
280278

281279
inline void register_() const;
280+
281+
template <class Fn, class CallPolicyOrDoc>
282+
void dispatch_def(
283+
char const* name,
284+
Fn fn,
285+
CallPolicyOrDoc const& policy_or_doc,
286+
char const* doc,
287+
void const*)
288+
{
289+
typedef detail::def_helper<CallPolicyOrDoc> helper;
290+
291+
this->def_impl(
292+
name, fn, helper::get_policy(policy_or_doc), helper::get_doc(policy_or_doc, doc), &fn);
293+
294+
}
295+
296+
template <typename StubsT, typename SigT>
297+
void dispatch_def(
298+
char const* name,
299+
SigT sig,
300+
StubsT const& stubs,
301+
char const* doc,
302+
detail::func_stubs_base const*)
303+
{
304+
// convert sig to a type_list (see detail::get_signature in signature.hpp)
305+
// before calling detail::define_with_defaults.
306+
detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc);
307+
}
282308
};
283309

284310

@@ -290,7 +316,7 @@ template <class T, class X1, class X2, class X3>
290316
inline void class_<T,X1,X2,X3>::register_() const
291317
{
292318
objects::register_class_from_python<T,bases>();
293-
319+
294320
detail::register_copy_constructor<T>(
295321
mpl::bool_t<is_copyable>()
296322
, objects::select_holder<T,held_type>((held_type*)0).get()

include/boost/python/module.hpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# include <boost/python/module_init.hpp>
1515
# include <boost/python/object_core.hpp>
1616
# include <boost/python/detail/def_helper.hpp>
17+
# include <boost/python/detail/defaults_def.hpp>
18+
# include <boost/python/signature.hpp>
1719

1820
namespace boost { namespace python {
1921

@@ -32,34 +34,62 @@ class module : public detail::module_base
3234
this->module_base::setattr_doc(name, python::object(x), 0);
3335
return *this;
3436
}
35-
37+
3638
module& add(type_handle x); // just use the type's name
37-
39+
3840
template <class T1, class T2 , class T3, class T4>
3941
module& add(class_<T1,T2,T3,T4> const& c)
4042
{
4143
// Soon to disappear...
4244
return *this;
4345
}
44-
46+
4547
template <class Fn>
4648
module& def(char const* name, Fn fn)
4749
{
4850
this->setattr_doc(
4951
name, boost::python::make_function(fn), 0);
50-
52+
53+
return *this;
54+
}
55+
56+
template <class Arg1T, class Arg2T>
57+
module& def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0)
58+
{
59+
dispatch_def(name, arg1, arg2, doc, &arg2);
5160
return *this;
5261
}
62+
63+
private:
64+
5365
template <class Fn, class CallPolicyOrDoc>
54-
module& def(char const* name, Fn fn, CallPolicyOrDoc const& policy_or_doc, char const* doc = 0)
66+
void
67+
dispatch_def(
68+
char const* name,
69+
Fn fn,
70+
CallPolicyOrDoc const& policy_or_doc,
71+
char const* doc,
72+
void const*)
5573
{
5674
typedef detail::def_helper<CallPolicyOrDoc> helper;
57-
75+
5876
this->setattr_doc(
5977
name, boost::python::make_function(fn, helper::get_policy(policy_or_doc)),
6078
helper::get_doc(policy_or_doc, doc));
61-
62-
return *this;
79+
}
80+
81+
template <typename StubsT, typename SigT>
82+
void
83+
dispatch_def(
84+
char const* name,
85+
SigT sig,
86+
StubsT const& stubs,
87+
char const* doc,
88+
detail::func_stubs_base const*)
89+
{
90+
// convert sig to a type_list (see detail::get_signature in signature.hpp)
91+
// before calling detail::define_with_defaults.
92+
detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc);
6393
}
6494
};
6595

test/Jamfile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local PYTHON_PROPERTIES = $(BOOST_PYTHON_V2_PROPERTIES) ;
1414
rule bpl-test ( name ? : files * )
1515
{
1616
files ?= $(name).py $(name).cpp ;
17-
17+
1818
local modules ;
1919
local py ;
2020
for local f in $(files)
@@ -28,19 +28,19 @@ rule bpl-test ( name ? : files * )
2828
py = $(f) ;
2929
}
3030
}
31-
31+
3232
name ?= $(py:S=) ;
3333

3434
for local f in $(files)
3535
{
3636
if $(f:S) != .py
3737
{
3838
local m = $(f:S=) ;
39-
39+
4040
if $(m) = $(py:S=)
4141
{
4242
m = $(name) ;
43-
43+
4444
if $(m) = $(py:S=)
4545
{
4646
m = $(m)_ext ;
@@ -50,7 +50,7 @@ rule bpl-test ( name ? : files * )
5050
modules += $(m) ;
5151
}
5252
}
53-
53+
5454
boost-python-runtest $(name) : $(py) <pyd>$(modules) ;
5555
}
5656

@@ -63,6 +63,7 @@ bpl-test builtin_converters : test_builtin_converters.py test_builtin_converters
6363
bpl-test test_pointer_adoption ;
6464
bpl-test operators ;
6565
bpl-test callbacks ;
66+
bpl-test defaults_ext : defaults.py defaults.cpp ;
6667

6768
bpl-test object ;
6869
bpl-test list ;
@@ -123,7 +124,7 @@ run upcast.cpp <lib>../../test/build/test_exec_monitor
123124
run select_holder.cpp <lib>../../test/build/test_exec_monitor
124125
: # command-line args
125126
: # input files
126-
: $(UNIT_TEST_PROPERTIES)
127+
: $(UNIT_TEST_PROPERTIES)
127128
;
128129

129130

0 commit comments

Comments
 (0)