Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // Copyright David Abrahams 2002, Joel de Guzman, 2002. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: #if !defined(BOOST_PP_IS_ITERATING) Chris@16: Chris@16: #ifndef DEFAULTS_DEF_JDG20020811_HPP Chris@16: #define DEFAULTS_DEF_JDG20020811_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace boost { namespace python { Chris@16: Chris@16: struct module; Chris@16: Chris@16: namespace objects Chris@16: { Chris@16: struct class_base; Chris@16: } Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: // Called as:: Chris@16: // Chris@16: // name_space_def(ns, "func", func, kw, policies, docstring, &ns) Chris@16: // Chris@16: // Dispatch to properly add f to namespace ns. Chris@16: // Chris@16: // @group define_stub_function helpers { Chris@16: template Chris@16: static void name_space_def( Chris@16: NameSpaceT& name_space Chris@16: , char const* name Chris@16: , Func f Chris@16: , keyword_range const& kw Chris@16: , CallPolicies const& policies Chris@16: , char const* doc Chris@16: , objects::class_base* Chris@16: ) Chris@16: { Chris@16: typedef typename NameSpaceT::wrapped_type wrapped_type; Chris@16: Chris@16: objects::add_to_namespace( Chris@16: name_space, name, Chris@16: detail::make_keyword_range_function( Chris@16: f, policies, kw, get_signature(f, (wrapped_type*)0)) Chris@16: , doc Chris@16: ); Chris@16: } Chris@16: Chris@16: template Chris@16: static void name_space_def( Chris@16: object& name_space Chris@16: , char const* name Chris@16: , Func f Chris@16: , keyword_range const& kw Chris@16: , CallPolicies const& policies Chris@16: , char const* doc Chris@16: , ... Chris@16: ) Chris@16: { Chris@16: scope within(name_space); Chris@16: Chris@16: detail::scope_setattr_doc( Chris@16: name Chris@16: , detail::make_keyword_range_function(f, policies, kw) Chris@16: , doc); Chris@16: } Chris@16: Chris@16: // For backward compatibility -- is this obsolete? Chris@16: template Chris@16: static void name_space_def( Chris@16: NameSpaceT& name_space Chris@16: , char const* name Chris@16: , Func f Chris@16: , keyword_range const& kw // ignored Chris@16: , CallPolicies const& policies Chris@16: , char const* doc Chris@16: , module* Chris@16: ) Chris@16: { Chris@16: name_space.def(name, f, policies, doc); Chris@16: } Chris@16: // } Chris@16: Chris@16: Chris@16: // Expansions of :: Chris@16: // Chris@16: // template Chris@16: // inline void Chris@16: // define_stub_function( Chris@16: // char const* name, OverloadsT s, NameSpaceT& name_space, mpl::int_) Chris@16: // { Chris@16: // name_space.def(name, &OverloadsT::func_N); Chris@16: // } Chris@16: // Chris@16: // where N runs from 0 to BOOST_PYTHON_MAX_ARITY. Chris@16: // Chris@16: // The set of overloaded functions (define_stub_function) expects: Chris@16: // Chris@16: // 1. char const* name: function name that will be visible to python Chris@16: // 2. OverloadsT: a function overloads struct (see defaults_gen.hpp) Chris@16: // 3. NameSpaceT& name_space: a python::class_ or python::module instance Chris@16: // 4. int_t: the Nth overloaded function (OverloadsT::func_N) Chris@16: // (see defaults_gen.hpp) Chris@16: // 5. char const* name: doc string Chris@16: // Chris@16: // @group define_stub_function { Chris@16: template Chris@16: struct define_stub_function {}; Chris@16: Chris@16: #define BOOST_PP_ITERATION_PARAMS_1 \ Chris@16: (3, (0, BOOST_PYTHON_MAX_ARITY, )) Chris@16: Chris@16: #include BOOST_PP_ITERATE() Chris@16: Chris@16: // } Chris@16: Chris@16: // This helper template struct does the actual recursive Chris@16: // definition. There's a generic version Chris@16: // define_with_defaults_helper and a terminal case Chris@16: // define_with_defaults_helper<0>. The struct and its Chris@16: // specialization has a sole static member function def that Chris@16: // expects: Chris@16: // Chris@16: // 1. char const* name: function name that will be Chris@16: // visible to python Chris@16: // Chris@16: // 2. OverloadsT: a function overloads struct Chris@16: // (see defaults_gen.hpp) Chris@16: // Chris@16: // 3. NameSpaceT& name_space: a python::class_ or Chris@16: // python::module instance Chris@16: // Chris@16: // 4. char const* name: doc string Chris@16: // Chris@16: // The def static member function calls a corresponding Chris@16: // define_stub_function. The general case recursively calls Chris@16: // define_with_defaults_helper::def until it reaches the Chris@16: // terminal case case define_with_defaults_helper<0>. Chris@16: template Chris@16: struct define_with_defaults_helper { Chris@16: Chris@16: template Chris@16: static void Chris@16: def( Chris@16: char const* name, Chris@16: StubsT stubs, Chris@16: keyword_range kw, Chris@16: CallPolicies const& policies, Chris@16: NameSpaceT& name_space, Chris@16: char const* doc) Chris@16: { Chris@16: // define the NTH stub function of stubs Chris@16: define_stub_function::define(name, stubs, kw, policies, name_space, doc); Chris@16: Chris@16: if (kw.second > kw.first) Chris@16: --kw.second; Chris@16: Chris@16: // call the next define_with_defaults_helper Chris@16: define_with_defaults_helper::def(name, stubs, kw, policies, name_space, doc); Chris@16: } Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct define_with_defaults_helper<0> { Chris@16: Chris@16: template Chris@16: static void Chris@16: def( Chris@16: char const* name, Chris@16: StubsT stubs, Chris@16: keyword_range const& kw, Chris@16: CallPolicies const& policies, Chris@16: NameSpaceT& name_space, Chris@16: char const* doc) Chris@16: { Chris@16: // define the Oth stub function of stubs Chris@16: define_stub_function<0>::define(name, stubs, kw, policies, name_space, doc); Chris@16: // return Chris@16: } Chris@16: }; Chris@16: Chris@16: // define_with_defaults Chris@16: // Chris@16: // 1. char const* name: function name that will be Chris@16: // visible to python Chris@16: // Chris@16: // 2. OverloadsT: a function overloads struct Chris@16: // (see defaults_gen.hpp) Chris@16: // Chris@16: // 3. CallPolicies& policies: Call policies Chris@16: // 4. NameSpaceT& name_space: a python::class_ or Chris@16: // python::module instance Chris@16: // Chris@16: // 5. SigT sig: Function signature typelist Chris@16: // (see defaults_gen.hpp) Chris@16: // Chris@16: // 6. char const* name: doc string Chris@16: // Chris@16: // This is the main entry point. This function recursively Chris@16: // defines all stub functions of StubT (see defaults_gen.hpp) in Chris@16: // NameSpaceT name_space which can be either a python::class_ or Chris@16: // a python::module. The sig argument is a typelist that Chris@16: // specifies the return type, the class (for member functions, Chris@16: // and the arguments. Here are some SigT examples: Chris@16: // Chris@16: // int foo(int) mpl::vector Chris@16: // void bar(int, int) mpl::vector Chris@16: // void C::foo(int) mpl::vector Chris@16: // Chris@16: template Chris@16: inline void Chris@16: define_with_defaults( Chris@16: char const* name, Chris@16: OverloadsT const& overloads, Chris@16: NameSpaceT& name_space, Chris@16: SigT const&) Chris@16: { Chris@16: typedef typename mpl::front::type return_type; Chris@16: typedef typename OverloadsT::void_return_type void_return_type; Chris@16: typedef typename OverloadsT::non_void_return_type non_void_return_type; Chris@16: Chris@16: typedef typename mpl::if_c< Chris@16: boost::is_same::value Chris@16: , void_return_type Chris@16: , non_void_return_type Chris@16: >::type stubs_type; Chris@16: Chris@16: BOOST_STATIC_ASSERT( Chris@16: (stubs_type::max_args) <= mpl::size::value); Chris@16: Chris@16: typedef typename stubs_type::template gen gen_type; Chris@16: define_with_defaults_helper::def( Chris@16: name Chris@16: , gen_type() Chris@16: , overloads.keywords() Chris@16: , overloads.call_policies() Chris@16: , name_space Chris@16: , overloads.doc_string()); Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: }} // namespace boost::python Chris@16: Chris@16: #endif // DEFAULTS_DEF_JDG20020811_HPP Chris@16: Chris@16: #else // defined(BOOST_PP_IS_ITERATING) Chris@16: // PP vertical iteration code Chris@16: Chris@16: Chris@16: template <> Chris@16: struct define_stub_function { Chris@16: template Chris@16: static void define( Chris@16: char const* name Chris@16: , StubsT const& Chris@16: , keyword_range const& kw Chris@16: , CallPolicies const& policies Chris@16: , NameSpaceT& name_space Chris@16: , char const* doc) Chris@16: { Chris@16: detail::name_space_def( Chris@16: name_space Chris@16: , name Chris@16: , &StubsT::BOOST_PP_CAT(func_, BOOST_PP_ITERATION()) Chris@16: , kw Chris@16: , policies Chris@16: , doc Chris@16: , &name_space); Chris@16: } Chris@16: }; Chris@16: Chris@16: #endif // !defined(BOOST_PP_IS_ITERATING)