From 9743ad0a69fdf5b2c1ff0152cfc1bbeeb49025f2 Mon Sep 17 00:00:00 2001 From: Joel de Guzman Date: Mon, 19 Aug 2002 18:46:44 +0000 Subject: [PATCH] module, class_ .def dispatching [SVN r14963] --- include/boost/python/class.hpp | 53 ++++++++++++-------- include/boost/python/detail/defaults_gen.hpp | 32 +++--------- include/boost/python/module.hpp | 36 +++++++++---- include/boost/python/signature.hpp | 30 +++++++++++ test/defaults.cpp | 6 +-- 5 files changed, 98 insertions(+), 59 deletions(-) diff --git a/include/boost/python/class.hpp b/include/boost/python/class.hpp index c3abd83b..22a93960 100644 --- a/include/boost/python/class.hpp +++ b/include/boost/python/class.hpp @@ -63,7 +63,6 @@ namespace detail } // Forward declaration (detail/defaults_def.hpp) - template struct func_stubs_base; } @@ -113,14 +112,10 @@ class class_ : public objects::class_base return *this; } - template - self& def(char const* name, Fn fn, CallPolicyOrDoc const& policy_or_doc, char const* doc = 0) + template + self& def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0) { - typedef detail::def_helper helper; - - this->def_impl( - name, fn, helper::get_policy(policy_or_doc), helper::get_doc(policy_or_doc, doc), &fn); - + dispatch_def(name, arg1, arg2, doc, &arg2); return *this; } @@ -131,18 +126,6 @@ class class_ : public objects::class_base return this->def(op.name(), &op_t::template apply::execute); } - template - self& def_generator( - char const* name, - detail::func_stubs_base const& stubs, - SigT sig, char const* doc = 0) - { - // 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.derived(), *this, detail::get_signature(sig), doc); - return *this; - } - // Define the constructor with the given Args, which should be an // MPL sequence of types. template @@ -242,7 +225,35 @@ class class_ : public objects::class_base objects::add_to_namespace(*this, name, f, doc); } - private: // types + template + void dispatch_def( + char const* name, + Fn fn, + CallPolicyOrDoc const& policy_or_doc, + char const* doc, + void const*) + { + typedef detail::def_helper helper; + + this->def_impl( + name, fn, helper::get_policy(policy_or_doc), helper::get_doc(policy_or_doc, doc), &fn); + + } + + 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. + detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc); + } + +private: // types typedef objects::class_id class_id; typedef typename detail::select_bases -// void foo(func_stubs_base const&); -// -// will accept only subclasses of func_stubs_base. +// func_stubs_base is used as a base class for all function stubs. // /////////////////////////////////////////////////////////////////////////////// -template -struct func_stubs_base { - - typedef DerivedT derived_t; - - DerivedT& derived() - { return *static_cast(this); } - - DerivedT const& derived() const - { return *static_cast(this); } -}; +struct func_stubs_base {}; }}} // namespace boost::python::detail @@ -182,7 +164,7 @@ struct func_stubs_base { BPL_IMPL_GEN_FUNCTION \ (FNAME, BOOST_PP_CAT(FSTUBS_NAME, _V), N_ARGS, N_DFLTS, ;) \ struct FSTUBS_NAME \ - : public boost::python::detail::func_stubs_base { \ + : public boost::python::detail::func_stubs_base { \ \ typedef BOOST_PP_CAT(FSTUBS_NAME, _NV) nv_type; \ typedef BOOST_PP_CAT(FSTUBS_NAME, _V) v_type; \ @@ -195,7 +177,7 @@ struct func_stubs_base { BPL_IMPL_GEN_MEM_FUNCTION \ (FNAME, BOOST_PP_CAT(FSTUBS_NAME, _V), N_ARGS, N_DFLTS, ;) \ struct FSTUBS_NAME \ - : public boost::python::detail::func_stubs_base { \ + : public boost::python::detail::func_stubs_base { \ \ typedef BOOST_PP_CAT(FSTUBS_NAME, _NV) nv_type; \ typedef BOOST_PP_CAT(FSTUBS_NAME, _V) v_type; \ @@ -208,7 +190,7 @@ struct func_stubs_base { BPL_IMPL_GEN_FUNCTION \ (FNAME, BOOST_PP_CAT(FSTUBS_NAME, _NV), N_ARGS, N_DFLTS, return) \ struct FSTUBS_NAME \ - : public boost::python::detail::func_stubs_base { \ + : public boost::python::detail::func_stubs_base { \ \ typedef BOOST_PP_CAT(FSTUBS_NAME, _NV) nv_type; \ typedef BOOST_PP_CAT(FSTUBS_NAME, _NV) v_type; \ @@ -219,7 +201,7 @@ struct func_stubs_base { BPL_IMPL_GEN_MEM_FUNCTION \ (FNAME, BOOST_PP_CAT(FSTUBS_NAME, _NV), N_ARGS, N_DFLTS, return) \ struct FSTUBS_NAME \ - : public boost::python::detail::func_stubs_base { \ + : public boost::python::detail::func_stubs_base { \ \ typedef BOOST_PP_CAT(FSTUBS_NAME, _NV) nv_type; \ typedef BOOST_PP_CAT(FSTUBS_NAME, _NV) v_type; \ @@ -285,7 +267,7 @@ struct func_stubs_base { // }; // // struct foo_stubs -// : public boost::python::detail::func_stubs_base { +// : public boost::python::detail::func_stubs_base { // // typedef foo_stubs_NV nv_type; // typedef foo_stubs_NV v_type; diff --git a/include/boost/python/module.hpp b/include/boost/python/module.hpp index 22a5b5ad..5f9e4a06 100644 --- a/include/boost/python/module.hpp +++ b/include/boost/python/module.hpp @@ -22,8 +22,8 @@ namespace boost { namespace python { namespace detail { // Forward declaration (detail/defaults_def.hpp) - template struct func_stubs_base; + } class module : public detail::module_base @@ -59,28 +59,44 @@ class module : public detail::module_base return *this; } + + template + module& def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0) + { + dispatch_def(name, arg1, arg2, doc, &arg2); + return *this; + } + + private: + template - module& def(char const* name, Fn fn, CallPolicyOrDoc const& policy_or_doc, char const* doc = 0) + void + dispatch_def( + char const* name, + Fn fn, + CallPolicyOrDoc const& policy_or_doc, + char const* doc, + void const*) { typedef detail::def_helper helper; this->setattr_doc( name, boost::python::make_function(fn, helper::get_policy(policy_or_doc)), helper::get_doc(policy_or_doc, doc)); - - return *this; } - template - module& def_generator( + template + void + dispatch_def( char const* name, - detail::func_stubs_base const& stubs, - SigT sig, char const* doc = 0) + 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. - detail::define_with_defaults(name, stubs.derived(), *this, detail::get_signature(sig), doc); - return *this; + detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc); } }; diff --git a/include/boost/python/signature.hpp b/include/boost/python/signature.hpp index 354f67dc..9f43ae1f 100644 --- a/include/boost/python/signature.hpp +++ b/include/boost/python/signature.hpp @@ -139,6 +139,36 @@ get_signature >(); } +/////////////////////////////////////// +#if !(defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)) + +template +< + typename RT BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) + BOOST_PP_ENUM + ( + BOOST_PP_ITERATION(), + BPL_IMPL_TEMPLATE_GEN, + BOOST_PP_EMPTY + ) +> +inline boost::mpl::type_list +< + RT BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) + BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), T) +> +get_signature + (signature) +{ + return boost::mpl::type_list + < + RT BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) + BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), T) + >(); +} + +#endif // !(defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)) + /////////////////////////////////////// template < diff --git a/test/defaults.cpp b/test/defaults.cpp index 7d617c00..9e8170b5 100644 --- a/test/defaults.cpp +++ b/test/defaults.cpp @@ -98,14 +98,14 @@ BOOST_PYTHON_MEM_FUN_GENERATOR(X_bar_stubs, bar, 1, 4) BOOST_PYTHON_MODULE_INIT(defaults_ext) { module m("defaults_ext"); - m.def_generator("foo", foo_stubs(), foo); - m.def_generator("bar", bar_stubs(), signature()); + m.def("foo", foo, foo_stubs()); + m.def("bar", signature(), bar_stubs()); class_ xc("X"); m.add(xc); xc.def_init(); - xc.def_generator("bar", X_bar_stubs(), X::bar); + xc.def("bar", X::bar, X_bar_stubs()); } #include "module_tail.cpp"