From 7609a1c7c66031b1edd5c8aa2a3718c3b1481829 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Mon, 25 Nov 2002 22:03:42 +0000 Subject: [PATCH] Refactored; added static assertions against the specification of a default implementation [SVN r16414] --- include/boost/python/def.hpp | 112 ++++++++++++++++------------------- 1 file changed, 52 insertions(+), 60 deletions(-) diff --git a/include/boost/python/def.hpp b/include/boost/python/def.hpp index 5631fb56..8914818b 100644 --- a/include/boost/python/def.hpp +++ b/include/boost/python/def.hpp @@ -18,15 +18,25 @@ namespace boost { namespace python { namespace detail { - template - void - dispatch_def( - void const*, - char const* name, - Fn fn, - A1 const& a1) + namespace error { - def_helper helper(a1); + // Compile-time error messages + template struct multiple_functions_passed_to_def; + template <> struct multiple_functions_passed_to_def { typedef char type; }; + } + + // + // def_from_helper -- + // + // Use a def_helper to define a regular wrapped function in the current scope. + template + void def_from_helper( + char const* name, F const& fn, Helper const& helper) + { + // Must not try to use default implementations except with method definitions. + typedef typename error::multiple_functions_passed_to_def< + Helper::has_default_implementation + >::type assertion; detail::scope_setattr_doc( name, boost::python::make_function( @@ -35,41 +45,37 @@ namespace detail , helper.keywords()) , helper.doc() ); - } + } - template - void dispatch_def( - void const*, - char const* name, - Fn fn, - A1 const& a1, - A2 const& a2) - { - def_helper helper(a1,a2); + // + // These two overloads discriminate between def() as applied to + // regular functions and def() as applied to the result of + // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to + // discriminate. + // + template + void + def_maybe_overloads( + char const* name + , Fn fn + , A1 const& a1 + , ...) + { + detail::def_from_helper(name, fn, def_helper(a1)); + } - detail::scope_setattr_doc( - name, python::make_function( - fn - , helper.policies() - , helper.keywords()) - , helper.doc() - ); - } - - template - void dispatch_def( - detail::overloads_base const*, - char const* name, - SigT sig, - StubsT const& stubs) - { - // convert sig to a type_list (see detail::get_signature in signature.hpp) - // before calling detail::define_with_defaults. - - scope current; - detail::define_with_defaults( - name, stubs, current, detail::get_signature(sig)); - } + template + void def_maybe_overloads( + char const* name + , SigT sig + , StubsT const& stubs + , detail::overloads_base const*) + { + scope current; + + detail::define_with_defaults( + name, stubs, current, detail::get_signature(sig)); + } } template @@ -81,33 +87,19 @@ void def(char const* name, Fn fn) template void def(char const* name, Arg1T arg1, Arg2T const& arg2) { - // The arguments may be: - // def(name, function) - // def(name, function, policy) - // def(name, function, doc_string) - // def(name, signature, stubs) - - detail::dispatch_def(&arg2, name, arg1, arg2); + detail::def_maybe_overloads(name, arg1, arg2, &arg2); } -template -void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3) +template +void def(char const* name, F f, A1 const& a1, A2 const& a2) { - detail::dispatch_def(&arg2, name, arg1, arg2, arg3); + detail::def_from_helper(name, f, detail::def_helper(a1,a2)); } template void def(char const* name, F f, A1 const& a1, A2 const& a2, A3 const& a3) { - detail::def_helper helper(a1,a2,a3); - - detail::scope_setattr_doc( - name, python::make_function( - f - , helper.policies() - , helper.keywords()) - , helper.doc() - ); + detail::def_from_helper(name, f, detail::def_helper(a1,a2,a3)); } }} // namespace boost::python