From 522a29241b3b91937eb5d33049d659c33a810372 Mon Sep 17 00:00:00 2001 From: Joel de Guzman Date: Fri, 6 Sep 2002 23:11:09 +0000 Subject: [PATCH] added call policies to the default stubs. [SVN r15190] --- include/boost/python/class.hpp | 53 ++++++++++++--- include/boost/python/def.hpp | 69 ++++++++++++++------ include/boost/python/detail/defaults_def.hpp | 53 +++++++++------ include/boost/python/init.hpp | 18 ++--- include/boost/python/module.hpp | 4 +- test/defaults.cpp | 4 +- 6 files changed, 141 insertions(+), 60 deletions(-) diff --git a/include/boost/python/class.hpp b/include/boost/python/class.hpp index 08a2c47d..4c7a1383 100644 --- a/include/boost/python/class.hpp +++ b/include/boost/python/class.hpp @@ -175,9 +175,37 @@ class class_ : public objects::class_base } template - self& def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0) + self& def(char const* name, Arg1T arg1, Arg2T const& arg2) { - dispatch_def(name, arg1, arg2, doc, &arg2); + // The arguments may be: + // arg1: function or signature + // arg2: policy or docstring or stubs + + dispatch_def(&arg2, name, arg1, arg2, (char*)0); + return *this; + } + + template + self& def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3) + { + // The arguments may be: + // arg1: function or signature + // arg2: policy or docstring or stubs + // arg3: policy or docstring + + dispatch_def(&arg2, name, arg1, arg2, arg3); + return *this; + } + + template + self& def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3, char const* doc) + { + // The arguments are definitely: + // arg1: signature + // arg2: stubs + // arg3: policy + + dispatch_def(&arg2, name, arg1, arg2, arg3, doc); return *this; } @@ -302,30 +330,37 @@ class class_ : public objects::class_base template void dispatch_def( + void const*, char const* name, Fn fn, CallPolicyOrDoc const& policy_or_doc, - char const* doc, - void const*) + char const* doc) { typedef detail::def_helper helper; this->def_impl( - name, fn, helper::get_policy(policy_or_doc), helper::get_doc(policy_or_doc, doc), &fn); + name, fn, helper::get_policy(policy_or_doc), + helper::get_doc(policy_or_doc, doc), &fn); } - template + template void dispatch_def( + detail::func_stubs_base const*, char const* name, SigT sig, StubsT const& stubs, - char const* doc, - detail::func_stubs_base const*) + CallPolicyOrDoc const& policy_or_doc, + char const* doc = 0) { + typedef detail::def_helper helper; + // convert sig to a type_list (see detail::get_signature in signature.hpp) // before calling detail::define_with_defaults. - detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc); + detail::define_with_defaults( + name, stubs, helper::get_policy(policy_or_doc), + *this, detail::get_signature(sig), + helper::get_doc(policy_or_doc, doc)); } }; diff --git a/include/boost/python/def.hpp b/include/boost/python/def.hpp index 5052644a..c6b8bb54 100644 --- a/include/boost/python/def.hpp +++ b/include/boost/python/def.hpp @@ -13,7 +13,7 @@ # include # include -namespace boost { namespace python { +namespace boost { namespace python { namespace detail { @@ -22,11 +22,11 @@ namespace detail template void dispatch_def( + void const*, char const* name, Fn fn, CallPolicyOrDoc const& policy_or_doc, - char const* doc, - void const*) + char const* doc) { typedef detail::def_helper helper; @@ -35,20 +35,26 @@ namespace detail helper::get_doc(policy_or_doc, doc)); } - template - void - dispatch_def( - char const* name, - SigT sig, - StubsT const& stubs, - char const* doc, - detail::func_stubs_base const*) - { - // 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), doc); - } + template + void dispatch_def( + detail::func_stubs_base const*, + char const* name, + SigT sig, + StubsT const& stubs, + CallPolicyOrDoc const& policy_or_doc, + char const* doc = 0) + { + typedef detail::def_helper helper; + + // 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, helper::get_policy(policy_or_doc), + current, detail::get_signature(sig), + helper::get_doc(policy_or_doc, doc)); + } } template @@ -58,11 +64,36 @@ void def(char const* name, Fn fn) } template -void def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0) +void def(char const* name, Arg1T arg1, Arg2T const& arg2) { - detail::dispatch_def(name, arg1, arg2, doc, &arg2); + // The arguments may be: + // arg1: function or signature + // arg2: policy or docstring or stubs + + detail::dispatch_def(&arg2, name, arg1, arg2, (char*)0); } +template +void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3) +{ + // The arguments may be: + // arg1: function or signature + // arg2: policy or docstring or stubs + // arg3: policy or docstring + + detail::dispatch_def(&arg2, name, arg1, arg2, arg3); +} + +template +void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3, char const* doc) +{ + // The arguments are definitely: + // arg1: signature + // arg2: stubs + // arg3: policy + + detail::dispatch_def(&arg2, name, arg1, arg2, arg3, doc); +} }} // namespace boost::python diff --git a/include/boost/python/detail/defaults_def.hpp b/include/boost/python/detail/defaults_def.hpp index bf2a4fb9..908fdbd9 100644 --- a/include/boost/python/detail/defaults_def.hpp +++ b/include/boost/python/detail/defaults_def.hpp @@ -34,7 +34,7 @@ namespace objects namespace detail { -template +template static void name_space_def( NameSpaceT& name_space, char const* name, @@ -48,7 +48,7 @@ static void name_space_def( name, f, policies, doc); } -template +template static void name_space_def( object& name_space, char const* name, @@ -59,12 +59,12 @@ static void name_space_def( ) { scope within(name_space); - + def(name, f, policies, doc); } // For backward compatibility -template +template static void name_space_def( NameSpaceT& name_space, char const* name, @@ -133,14 +133,19 @@ struct define_stub_function {}; template struct define_with_defaults_helper { - template + template static void - def(char const* name, StubsT stubs, NameSpaceT& name_space, char const* doc) + def( + char const* name, + StubsT stubs, + CallPolicies const& policies, + NameSpaceT& name_space, + char const* doc) { // define the NTH stub function of stubs - define_stub_function::define(name, stubs, name_space, doc); + define_stub_function::define(name, stubs, policies, name_space, doc); // call the next define_with_defaults_helper - define_with_defaults_helper::def(name, stubs, name_space, doc); + define_with_defaults_helper::def(name, stubs, policies, name_space, doc); } }; @@ -148,12 +153,17 @@ struct define_stub_function {}; template <> struct define_with_defaults_helper<0> { - template + template static void - def(char const* name, StubsT stubs, NameSpaceT& name_space, char const* doc) + def( + char const* name, + StubsT stubs, + CallPolicies const& policies, + NameSpaceT& name_space, + char const* doc) { // define the Oth stub function of stubs - define_stub_function<0>::define(name, stubs, name_space, doc); + define_stub_function<0>::define(name, stubs, policies, name_space, doc); // return } }; @@ -162,11 +172,12 @@ struct define_stub_function {}; // // define_with_defaults // -// 1. char const* name: function name that will be visible to python -// 2. StubsT: a function stubs struct (see defaults_gen.hpp) -// 3. NameSpaceT& name_space: a python::class_ or python::module instance -// 4. SigT sig: Function signature typelist (see defaults_gen.hpp) -// 5. char const* name: doc string +// 1. char const* name: function name that will be visible to python +// 2. StubsT: a function stubs struct (see defaults_gen.hpp) +// 3. CallPolicies& policies: Call policies +// 4. NameSpaceT& name_space: a python::class_ or python::module instance +// 5. SigT sig: Function signature typelist (see defaults_gen.hpp) +// 6. char const* name: doc string // // This is the main entry point. This function recursively defines all // stub functions of StubT (see defaults_gen.hpp) in NameSpaceT name_space which @@ -179,11 +190,12 @@ struct define_stub_function {}; // void C::foo(int) mpl::type_list // /////////////////////////////////////////////////////////////////////////////// - template + template inline void define_with_defaults( char const* name, StubsT, + CallPolicies const& policies, NameSpaceT& name_space, SigT sig, char const* doc) @@ -204,7 +216,7 @@ struct define_stub_function {}; typedef typename stubs_type::template gen gen_type; define_with_defaults_helper::def - (name, gen_type(), name_space, doc); + (name, gen_type(), policies, name_space, doc); } } // namespace detail @@ -220,10 +232,11 @@ struct define_stub_function {}; template <> struct define_stub_function { - template + template static void define( char const* name, StubsT, + CallPolicies const& policies, NameSpaceT& name_space, char const* doc ) @@ -231,7 +244,7 @@ struct define_stub_function { detail::name_space_def(name_space, name, &StubsT::BOOST_PP_CAT(func_, BOOST_PP_ITERATION()), - default_call_policies(), + policies, doc, &name_space); } }; diff --git a/include/boost/python/init.hpp b/include/boost/python/init.hpp index 5ea2fdbc..617ed30a 100644 --- a/include/boost/python/init.hpp +++ b/include/boost/python/init.hpp @@ -125,7 +125,7 @@ namespace detail { // Case 1: default case, just push T to the back of ListT - template + template struct apply { typedef typename boost::mpl::push_back::sequence sequence; @@ -134,7 +134,7 @@ namespace detail { struct append_to_init_helper2 { - template + template struct apply { // Case 2: optional case, T is an optional, append all @@ -153,14 +153,14 @@ namespace detail { // Case 3: nil case, we found a nil, do nothing - template + template struct apply { typedef ListT sequence; }; }; - template + template struct append_to_init { typedef typename boost::mpl::select_type @@ -192,7 +192,7 @@ namespace detail { template struct check_init_params_helper { - template + template struct apply { // case where size of sequence is not zero @@ -221,7 +221,7 @@ namespace detail { // case where size of sequence is zero - template + template struct apply { enum { is_ok = true }; @@ -252,20 +252,20 @@ namespace detail { // init >::value == 3 // /////////////////////////////////////////////////////////////////////////// - template + template struct count_optionals1 { BOOST_STATIC_CONSTANT(int, value = 0); }; - template + template struct count_optionals2 { BOOST_STATIC_CONSTANT( int, value = boost::mpl::size::value); }; - template + template struct count_optionals : boost::mpl::select_type < diff --git a/include/boost/python/module.hpp b/include/boost/python/module.hpp index cc8cb832..a455ca41 100644 --- a/include/boost/python/module.hpp +++ b/include/boost/python/module.hpp @@ -89,7 +89,9 @@ class module : public detail::module_base { // convert sig to a type_list (see detail::get_signature in signature.hpp) // before calling detail::define_with_defaults. - detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc); + detail::define_with_defaults( + name, stubs, default_call_policies(), + *this, detail::get_signature(sig), doc); } }; diff --git a/test/defaults.cpp b/test/defaults.cpp index 06fb2bc2..cacb6ef7 100644 --- a/test/defaults.cpp +++ b/test/defaults.cpp @@ -126,7 +126,7 @@ BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3) BOOST_PYTHON_MODULE_INIT(defaults_ext) { - def("foo", foo, foo_stubs()); + def("foo", foo, foo_stubs(), default_call_policies()); def("bar", (object(*)(int, char, std::string, double))0, bar_stubs()); // Show that this works with the old obsolete module version of def(). @@ -147,7 +147,7 @@ BOOST_PYTHON_MODULE_INIT(defaults_ext) # endif .def("get_state", &X::get_state) .def("bar", &X::bar, X_bar_stubs()) - .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs()) + .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs(), default_call_policies()) .def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs()) .def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs()) ;