Skip to content

Commit 522a292

Browse files
committed
added call policies to the default stubs.
[SVN r15190]
1 parent b8d3c84 commit 522a292

File tree

6 files changed

+141
-60
lines changed

6 files changed

+141
-60
lines changed

include/boost/python/class.hpp

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,37 @@ class class_ : public objects::class_base
175175
}
176176

177177
template <class Arg1T, class Arg2T>
178-
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0)
178+
self& def(char const* name, Arg1T arg1, Arg2T const& arg2)
179179
{
180-
dispatch_def(name, arg1, arg2, doc, &arg2);
180+
// The arguments may be:
181+
// arg1: function or signature
182+
// arg2: policy or docstring or stubs
183+
184+
dispatch_def(&arg2, name, arg1, arg2, (char*)0);
185+
return *this;
186+
}
187+
188+
template <class Arg1T, class Arg2T, class Arg3T>
189+
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3)
190+
{
191+
// The arguments may be:
192+
// arg1: function or signature
193+
// arg2: policy or docstring or stubs
194+
// arg3: policy or docstring
195+
196+
dispatch_def(&arg2, name, arg1, arg2, arg3);
197+
return *this;
198+
}
199+
200+
template <class Arg1T, class Arg2T, class Arg3T>
201+
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3, char const* doc)
202+
{
203+
// The arguments are definitely:
204+
// arg1: signature
205+
// arg2: stubs
206+
// arg3: policy
207+
208+
dispatch_def(&arg2, name, arg1, arg2, arg3, doc);
181209
return *this;
182210
}
183211

@@ -302,30 +330,37 @@ class class_ : public objects::class_base
302330

303331
template <class Fn, class CallPolicyOrDoc>
304332
void dispatch_def(
333+
void const*,
305334
char const* name,
306335
Fn fn,
307336
CallPolicyOrDoc const& policy_or_doc,
308-
char const* doc,
309-
void const*)
337+
char const* doc)
310338
{
311339
typedef detail::def_helper<CallPolicyOrDoc> helper;
312340

313341
this->def_impl(
314-
name, fn, helper::get_policy(policy_or_doc), helper::get_doc(policy_or_doc, doc), &fn);
342+
name, fn, helper::get_policy(policy_or_doc),
343+
helper::get_doc(policy_or_doc, doc), &fn);
315344

316345
}
317346

318-
template <typename StubsT, typename SigT>
347+
template <class StubsT, class SigT, class CallPolicyOrDoc>
319348
void dispatch_def(
349+
detail::func_stubs_base const*,
320350
char const* name,
321351
SigT sig,
322352
StubsT const& stubs,
323-
char const* doc,
324-
detail::func_stubs_base const*)
353+
CallPolicyOrDoc const& policy_or_doc,
354+
char const* doc = 0)
325355
{
356+
typedef detail::def_helper<CallPolicyOrDoc> helper;
357+
326358
// convert sig to a type_list (see detail::get_signature in signature.hpp)
327359
// before calling detail::define_with_defaults.
328-
detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc);
360+
detail::define_with_defaults(
361+
name, stubs, helper::get_policy(policy_or_doc),
362+
*this, detail::get_signature(sig),
363+
helper::get_doc(policy_or_doc, doc));
329364
}
330365
};
331366

include/boost/python/def.hpp

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# include <boost/python/scope.hpp>
1414
# include <boost/python/signature.hpp>
1515

16-
namespace boost { namespace python {
16+
namespace boost { namespace python {
1717

1818
namespace detail
1919
{
@@ -22,11 +22,11 @@ namespace detail
2222
template <class Fn, class CallPolicyOrDoc>
2323
void
2424
dispatch_def(
25+
void const*,
2526
char const* name,
2627
Fn fn,
2728
CallPolicyOrDoc const& policy_or_doc,
28-
char const* doc,
29-
void const*)
29+
char const* doc)
3030
{
3131
typedef detail::def_helper<CallPolicyOrDoc> helper;
3232

@@ -35,20 +35,26 @@ namespace detail
3535
helper::get_doc(policy_or_doc, doc));
3636
}
3737

38-
template <typename StubsT, typename SigT>
39-
void
40-
dispatch_def(
41-
char const* name,
42-
SigT sig,
43-
StubsT const& stubs,
44-
char const* doc,
45-
detail::func_stubs_base const*)
46-
{
47-
// convert sig to a type_list (see detail::get_signature in signature.hpp)
48-
// before calling detail::define_with_defaults.
49-
scope current;
50-
detail::define_with_defaults(name, stubs, current, detail::get_signature(sig), doc);
51-
}
38+
template <class StubsT, class SigT, class CallPolicyOrDoc>
39+
void dispatch_def(
40+
detail::func_stubs_base const*,
41+
char const* name,
42+
SigT sig,
43+
StubsT const& stubs,
44+
CallPolicyOrDoc const& policy_or_doc,
45+
char const* doc = 0)
46+
{
47+
typedef detail::def_helper<CallPolicyOrDoc> helper;
48+
49+
// convert sig to a type_list (see detail::get_signature in signature.hpp)
50+
// before calling detail::define_with_defaults.
51+
52+
scope current;
53+
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));
57+
}
5258
}
5359

5460
template <class Fn>
@@ -58,11 +64,36 @@ void def(char const* name, Fn fn)
5864
}
5965

6066
template <class Arg1T, class Arg2T>
61-
void def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0)
67+
void def(char const* name, Arg1T arg1, Arg2T const& arg2)
6268
{
63-
detail::dispatch_def(name, arg1, arg2, doc, &arg2);
69+
// The arguments may be:
70+
// arg1: function or signature
71+
// arg2: policy or docstring or stubs
72+
73+
detail::dispatch_def(&arg2, name, arg1, arg2, (char*)0);
6474
}
6575

76+
template <class Arg1T, class Arg2T, class Arg3T>
77+
void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3)
78+
{
79+
// The arguments may be:
80+
// arg1: function or signature
81+
// arg2: policy or docstring or stubs
82+
// arg3: policy or docstring
83+
84+
detail::dispatch_def(&arg2, name, arg1, arg2, arg3);
85+
}
86+
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+
}
6697

6798
}} // namespace boost::python
6899

include/boost/python/detail/defaults_def.hpp

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace objects
3434

3535
namespace detail {
3636

37-
template <typename Func, class CallPolicies, class NameSpaceT>
37+
template <class Func, class CallPolicies, class NameSpaceT>
3838
static void name_space_def(
3939
NameSpaceT& name_space,
4040
char const* name,
@@ -48,7 +48,7 @@ static void name_space_def(
4848
name, f, policies, doc);
4949
}
5050

51-
template <typename Func, class CallPolicies>
51+
template <class Func, class CallPolicies>
5252
static void name_space_def(
5353
object& name_space,
5454
char const* name,
@@ -59,12 +59,12 @@ static void name_space_def(
5959
)
6060
{
6161
scope within(name_space);
62-
62+
6363
def(name, f, policies, doc);
6464
}
6565

6666
// For backward compatibility
67-
template <typename Func, class CallPolicies, class NameSpaceT>
67+
template <class Func, class CallPolicies, class NameSpaceT>
6868
static void name_space_def(
6969
NameSpaceT& name_space,
7070
char const* name,
@@ -133,27 +133,37 @@ struct define_stub_function {};
133133
template <int N>
134134
struct define_with_defaults_helper {
135135

136-
template <typename StubsT, typename NameSpaceT>
136+
template <class StubsT, class CallPolicies, class NameSpaceT>
137137
static void
138-
def(char const* name, StubsT stubs, NameSpaceT& name_space, char const* doc)
138+
def(
139+
char const* name,
140+
StubsT stubs,
141+
CallPolicies const& policies,
142+
NameSpaceT& name_space,
143+
char const* doc)
139144
{
140145
// define the NTH stub function of stubs
141-
define_stub_function<N>::define(name, stubs, name_space, doc);
146+
define_stub_function<N>::define(name, stubs, policies, name_space, doc);
142147
// call the next define_with_defaults_helper
143-
define_with_defaults_helper<N-1>::def(name, stubs, name_space, doc);
148+
define_with_defaults_helper<N-1>::def(name, stubs, policies, name_space, doc);
144149
}
145150
};
146151

147152
///////////////////////////////////////
148153
template <>
149154
struct define_with_defaults_helper<0> {
150155

151-
template <typename StubsT, typename NameSpaceT>
156+
template <class StubsT, class CallPolicies, class NameSpaceT>
152157
static void
153-
def(char const* name, StubsT stubs, NameSpaceT& name_space, char const* doc)
158+
def(
159+
char const* name,
160+
StubsT stubs,
161+
CallPolicies const& policies,
162+
NameSpaceT& name_space,
163+
char const* doc)
154164
{
155165
// define the Oth stub function of stubs
156-
define_stub_function<0>::define(name, stubs, name_space, doc);
166+
define_stub_function<0>::define(name, stubs, policies, name_space, doc);
157167
// return
158168
}
159169
};
@@ -162,11 +172,12 @@ struct define_stub_function {};
162172
//
163173
// define_with_defaults
164174
//
165-
// 1. char const* name: function name that will be visible to python
166-
// 2. StubsT: a function stubs struct (see defaults_gen.hpp)
167-
// 3. NameSpaceT& name_space: a python::class_ or python::module instance
168-
// 4. SigT sig: Function signature typelist (see defaults_gen.hpp)
169-
// 5. char const* name: doc string
175+
// 1. char const* name: function name that will be visible to python
176+
// 2. StubsT: a function stubs struct (see defaults_gen.hpp)
177+
// 3. CallPolicies& policies: Call policies
178+
// 4. NameSpaceT& name_space: a python::class_ or python::module instance
179+
// 5. SigT sig: Function signature typelist (see defaults_gen.hpp)
180+
// 6. char const* name: doc string
170181
//
171182
// This is the main entry point. This function recursively defines all
172183
// stub functions of StubT (see defaults_gen.hpp) in NameSpaceT name_space which
@@ -179,11 +190,12 @@ struct define_stub_function {};
179190
// void C::foo(int) mpl::type_list<void, C, int>
180191
//
181192
///////////////////////////////////////////////////////////////////////////////
182-
template <typename StubsT, typename NameSpaceT, typename SigT>
193+
template <class StubsT, class CallPolicies, class NameSpaceT, class SigT>
183194
inline void
184195
define_with_defaults(
185196
char const* name,
186197
StubsT,
198+
CallPolicies const& policies,
187199
NameSpaceT& name_space,
188200
SigT sig,
189201
char const* doc)
@@ -204,7 +216,7 @@ struct define_stub_function {};
204216

205217
typedef typename stubs_type::template gen<SigT> gen_type;
206218
define_with_defaults_helper<stubs_type::n_funcs-1>::def
207-
(name, gen_type(), name_space, doc);
219+
(name, gen_type(), policies, name_space, doc);
208220
}
209221

210222
} // namespace detail
@@ -220,18 +232,19 @@ struct define_stub_function {};
220232

221233
template <>
222234
struct define_stub_function<BOOST_PP_ITERATION()> {
223-
template <typename StubsT, typename NameSpaceT>
235+
template <class StubsT, class CallPolicies, class NameSpaceT>
224236
static void define(
225237
char const* name,
226238
StubsT,
239+
CallPolicies const& policies,
227240
NameSpaceT& name_space,
228241
char const* doc
229242
)
230243
{
231244
detail::name_space_def(name_space,
232245
name,
233246
&StubsT::BOOST_PP_CAT(func_, BOOST_PP_ITERATION()),
234-
default_call_policies(),
247+
policies,
235248
doc, &name_space);
236249
}
237250
};

0 commit comments

Comments
 (0)