Skip to content

Commit 7609a1c

Browse files
committed
Refactored; added static assertions against the specification of a default implementation
[SVN r16414]
1 parent 087e2d6 commit 7609a1c

1 file changed

Lines changed: 52 additions & 60 deletions

File tree

include/boost/python/def.hpp

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,25 @@ namespace boost { namespace python {
1818

1919
namespace detail
2020
{
21-
template <class Fn, class A1>
22-
void
23-
dispatch_def(
24-
void const*,
25-
char const* name,
26-
Fn fn,
27-
A1 const& a1)
21+
namespace error
22+
{
23+
// Compile-time error messages
24+
template <bool> struct multiple_functions_passed_to_def;
25+
template <> struct multiple_functions_passed_to_def<false> { typedef char type; };
26+
}
27+
28+
//
29+
// def_from_helper --
30+
//
31+
// Use a def_helper to define a regular wrapped function in the current scope.
32+
template <class F, class Helper>
33+
void def_from_helper(
34+
char const* name, F const& fn, Helper const& helper)
2835
{
29-
def_helper<A1> helper(a1);
36+
// Must not try to use default implementations except with method definitions.
37+
typedef typename error::multiple_functions_passed_to_def<
38+
Helper::has_default_implementation
39+
>::type assertion;
3040

3141
detail::scope_setattr_doc(
3242
name, boost::python::make_function(
@@ -35,41 +45,37 @@ namespace detail
3545
, helper.keywords())
3646
, helper.doc()
3747
);
38-
}
48+
}
3949

40-
template <class Fn, class A1, class A2>
41-
void dispatch_def(
42-
void const*,
43-
char const* name,
44-
Fn fn,
45-
A1 const& a1,
46-
A2 const& a2)
47-
{
48-
def_helper<A1,A2> helper(a1,a2);
49-
50-
detail::scope_setattr_doc(
51-
name, python::make_function(
52-
fn
53-
, helper.policies()
54-
, helper.keywords())
55-
, helper.doc()
56-
);
57-
}
58-
59-
template <class StubsT, class SigT>
60-
void dispatch_def(
61-
detail::overloads_base const*,
62-
char const* name,
63-
SigT sig,
64-
StubsT const& stubs)
65-
{
66-
// convert sig to a type_list (see detail::get_signature in signature.hpp)
67-
// before calling detail::define_with_defaults.
50+
//
51+
// These two overloads discriminate between def() as applied to
52+
// regular functions and def() as applied to the result of
53+
// BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to
54+
// discriminate.
55+
//
56+
template <class Fn, class A1>
57+
void
58+
def_maybe_overloads(
59+
char const* name
60+
, Fn fn
61+
, A1 const& a1
62+
, ...)
63+
{
64+
detail::def_from_helper(name, fn, def_helper<A1>(a1));
65+
}
6866

69-
scope current;
70-
detail::define_with_defaults(
71-
name, stubs, current, detail::get_signature(sig));
72-
}
67+
template <class StubsT, class SigT>
68+
void def_maybe_overloads(
69+
char const* name
70+
, SigT sig
71+
, StubsT const& stubs
72+
, detail::overloads_base const*)
73+
{
74+
scope current;
75+
76+
detail::define_with_defaults(
77+
name, stubs, current, detail::get_signature(sig));
78+
}
7379
}
7480

7581
template <class Fn>
@@ -81,33 +87,19 @@ void def(char const* name, Fn fn)
8187
template <class Arg1T, class Arg2T>
8288
void def(char const* name, Arg1T arg1, Arg2T const& arg2)
8389
{
84-
// The arguments may be:
85-
// def(name, function)
86-
// def(name, function, policy)
87-
// def(name, function, doc_string)
88-
// def(name, signature, stubs)
89-
90-
detail::dispatch_def(&arg2, name, arg1, arg2);
90+
detail::def_maybe_overloads(name, arg1, arg2, &arg2);
9191
}
9292

93-
template <class Arg1T, class Arg2T, class Arg3T>
94-
void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3)
93+
template <class F, class A1, class A2>
94+
void def(char const* name, F f, A1 const& a1, A2 const& a2)
9595
{
96-
detail::dispatch_def(&arg2, name, arg1, arg2, arg3);
96+
detail::def_from_helper(name, f, detail::def_helper<A1,A2>(a1,a2));
9797
}
9898

9999
template <class F, class A1, class A2, class A3>
100100
void def(char const* name, F f, A1 const& a1, A2 const& a2, A3 const& a3)
101101
{
102-
detail::def_helper<A1,A2,A3> helper(a1,a2,a3);
103-
104-
detail::scope_setattr_doc(
105-
name, python::make_function(
106-
f
107-
, helper.policies()
108-
, helper.keywords())
109-
, helper.doc()
110-
);
102+
detail::def_from_helper(name, f, detail::def_helper<A1,A2,A3>(a1,a2,a3));
111103
}
112104

113105
}} // namespace boost::python

0 commit comments

Comments
 (0)