Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:37

0001 ///////////////////////////////////////////////////////////////////////////////
0002 //
0003 // Copyright David Abrahams 2002, Joel de Guzman, 2002.
0004 // Distributed under the Boost Software License, Version 1.0. (See
0005 // accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 ///////////////////////////////////////////////////////////////////////////////
0009 #if !defined(BOOST_PP_IS_ITERATING)
0010 
0011 #ifndef DEFAULTS_DEF_JDG20020811_HPP
0012 #define DEFAULTS_DEF_JDG20020811_HPP
0013 
0014 #include <boost/python/detail/defaults_gen.hpp>
0015 #include <boost/python/detail/type_traits.hpp>
0016 #include <boost/mpl/front.hpp>
0017 #include <boost/mpl/size.hpp>
0018 #include <boost/static_assert.hpp>
0019 #include <boost/preprocessor/iterate.hpp>
0020 #include <boost/python/class_fwd.hpp>
0021 #include <boost/python/scope.hpp>
0022 #include <boost/preprocessor/debug/line.hpp>
0023 #include <boost/python/detail/scope.hpp>
0024 #include <boost/python/detail/make_keyword_range_fn.hpp>
0025 #include <boost/python/object/add_to_namespace.hpp>
0026 
0027 ///////////////////////////////////////////////////////////////////////////////
0028 namespace boost { namespace python {
0029 
0030 struct module;
0031 
0032 namespace objects
0033 {
0034   struct class_base;
0035 }
0036 
0037 namespace detail
0038 {
0039   // Called as::
0040   //
0041   //    name_space_def(ns, "func", func, kw, policies, docstring, &ns)
0042   //
0043   // Dispatch to properly add f to namespace ns.
0044   //
0045   // @group define_stub_function helpers { 
0046   template <class Func, class CallPolicies, class NameSpaceT>
0047   static void name_space_def(
0048       NameSpaceT& name_space
0049       , char const* name
0050       , Func f
0051       , keyword_range const& kw
0052       , CallPolicies const& policies
0053       , char const* doc
0054       , objects::class_base*
0055       )
0056   {
0057       typedef typename NameSpaceT::wrapped_type wrapped_type;
0058       
0059       objects::add_to_namespace(
0060           name_space, name,
0061           detail::make_keyword_range_function(
0062               f, policies, kw, get_signature(f, (wrapped_type*)0))
0063         , doc
0064       );
0065   }
0066 
0067   template <class Func, class CallPolicies>
0068   static void name_space_def(
0069       object& name_space
0070       , char const* name
0071       , Func f
0072       , keyword_range const& kw
0073       , CallPolicies const& policies
0074       , char const* doc
0075       , ...
0076       )
0077   {
0078       scope within(name_space);
0079 
0080       detail::scope_setattr_doc(
0081           name
0082           , detail::make_keyword_range_function(f, policies, kw)
0083           , doc);
0084   }
0085 
0086   // For backward compatibility -- is this obsolete?
0087   template <class Func, class CallPolicies, class NameSpaceT>
0088   static void name_space_def(
0089       NameSpaceT& name_space
0090       , char const* name
0091       , Func f
0092       , keyword_range const& kw // ignored
0093       , CallPolicies const& policies
0094       , char const* doc
0095       , module*
0096       )
0097   {
0098       name_space.def(name, f, policies, doc);
0099   }
0100   // }
0101 
0102 
0103   //  Expansions of ::
0104   //
0105   //      template <typename OverloadsT, typename NameSpaceT>
0106   //      inline void
0107   //      define_stub_function(
0108   //          char const* name, OverloadsT s, NameSpaceT& name_space, mpl::int_<N>)
0109   //      {
0110   //          name_space.def(name, &OverloadsT::func_N);
0111   //      }
0112   //
0113   //  where N runs from 0 to BOOST_PYTHON_MAX_ARITY.
0114   //
0115   //  The set of overloaded functions (define_stub_function) expects:
0116   //
0117   //      1. char const* name:        function name that will be visible to python
0118   //      2. OverloadsT:              a function overloads struct (see defaults_gen.hpp)
0119   //      3. NameSpaceT& name_space:  a python::class_ or python::module instance
0120   //      4. int_t<N>:                the Nth overloaded function (OverloadsT::func_N)
0121   //                                  (see defaults_gen.hpp)
0122   //      5. char const* name:        doc string
0123   //
0124   // @group define_stub_function<N> {
0125   template <int N>
0126   struct define_stub_function {};
0127 
0128 #define BOOST_PP_ITERATION_PARAMS_1                                             \
0129     (3, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/detail/defaults_def.hpp>))
0130 
0131 #include BOOST_PP_ITERATE()
0132   
0133   // }
0134   
0135   //  This helper template struct does the actual recursive
0136   //  definition.  There's a generic version
0137   //  define_with_defaults_helper<N> and a terminal case
0138   //  define_with_defaults_helper<0>. The struct and its
0139   //  specialization has a sole static member function def that
0140   //  expects:
0141   //
0142   //    1. char const* name:        function name that will be
0143   //                                visible to python
0144   //
0145   //    2. OverloadsT:              a function overloads struct
0146   //                                (see defaults_gen.hpp)
0147   //
0148   //    3. NameSpaceT& name_space:  a python::class_ or
0149   //                                python::module instance
0150   //
0151   //    4. char const* name:        doc string
0152   //
0153   //  The def static member function calls a corresponding
0154   //  define_stub_function<N>. The general case recursively calls
0155   //  define_with_defaults_helper<N-1>::def until it reaches the
0156   //  terminal case case define_with_defaults_helper<0>.
0157   template <int N>
0158   struct define_with_defaults_helper {
0159 
0160       template <class StubsT, class CallPolicies, class NameSpaceT>
0161       static void
0162       def(
0163           char const* name,
0164           StubsT stubs,
0165           keyword_range kw,
0166           CallPolicies const& policies,
0167           NameSpaceT& name_space,
0168           char const* doc)
0169       {
0170           //  define the NTH stub function of stubs
0171           define_stub_function<N>::define(name, stubs, kw, policies, name_space, doc);
0172 
0173           if (kw.second > kw.first)
0174               --kw.second;
0175 
0176           //  call the next define_with_defaults_helper
0177           define_with_defaults_helper<N-1>::def(name, stubs, kw, policies, name_space, doc);
0178       }
0179   };
0180 
0181   template <>
0182   struct define_with_defaults_helper<0> {
0183 
0184       template <class StubsT, class CallPolicies, class NameSpaceT>
0185       static void
0186       def(
0187           char const* name,
0188           StubsT stubs,
0189           keyword_range const& kw,
0190           CallPolicies const& policies,
0191           NameSpaceT& name_space,
0192           char const* doc)
0193       {
0194           //  define the Oth stub function of stubs
0195           define_stub_function<0>::define(name, stubs, kw, policies, name_space, doc);
0196           //  return
0197       }
0198   };
0199 
0200   //  define_with_defaults
0201   //
0202   //      1. char const* name:        function name that will be
0203   //                                  visible to python
0204   //
0205   //      2. OverloadsT:              a function overloads struct
0206   //                                  (see defaults_gen.hpp)
0207   //
0208   //      3. CallPolicies& policies:  Call policies
0209   //      4. NameSpaceT& name_space:  a python::class_ or
0210   //                                  python::module instance
0211   //
0212   //      5. SigT sig:                Function signature typelist
0213   //                                  (see defaults_gen.hpp)
0214   //
0215   //      6. char const* name:        doc string
0216   //
0217   //  This is the main entry point. This function recursively
0218   //  defines all stub functions of StubT (see defaults_gen.hpp) in
0219   //  NameSpaceT name_space which can be either a python::class_ or
0220   //  a python::module. The sig argument is a typelist that
0221   //  specifies the return type, the class (for member functions,
0222   //  and the arguments. Here are some SigT examples:
0223   //
0224   //      int foo(int)        mpl::vector<int, int>
0225   //      void bar(int, int)  mpl::vector<void, int, int>
0226   //      void C::foo(int)    mpl::vector<void, C, int>
0227   //
0228   template <class OverloadsT, class NameSpaceT, class SigT>
0229   inline void
0230   define_with_defaults(
0231       char const* name,
0232       OverloadsT const& overloads,
0233       NameSpaceT& name_space,
0234       SigT const&)
0235   {
0236       typedef typename mpl::front<SigT>::type return_type;
0237       typedef typename OverloadsT::void_return_type void_return_type;
0238       typedef typename OverloadsT::non_void_return_type non_void_return_type;
0239 
0240       typedef typename mpl::if_c<
0241           is_same<void, return_type>::value
0242           , void_return_type
0243           , non_void_return_type
0244       >::type stubs_type;
0245 
0246       BOOST_STATIC_ASSERT(
0247           (stubs_type::max_args) <= mpl::size<SigT>::value);
0248 
0249       typedef typename stubs_type::template gen<SigT> gen_type;
0250       define_with_defaults_helper<stubs_type::n_funcs-1>::def(
0251           name
0252           , gen_type()
0253           , overloads.keywords()
0254           , overloads.call_policies()
0255           , name_space
0256           , overloads.doc_string());
0257   }
0258 
0259 } // namespace detail
0260 
0261 }} // namespace boost::python
0262 
0263 #endif // DEFAULTS_DEF_JDG20020811_HPP
0264 
0265 #else // defined(BOOST_PP_IS_ITERATING)
0266 // PP vertical iteration code
0267 
0268 
0269 template <>
0270 struct define_stub_function<BOOST_PP_ITERATION()> {
0271     template <class StubsT, class CallPolicies, class NameSpaceT>
0272     static void define(
0273         char const* name
0274         , StubsT const&
0275         , keyword_range const& kw
0276         , CallPolicies const& policies
0277         , NameSpaceT& name_space
0278         , char const* doc)
0279     {
0280         detail::name_space_def(
0281             name_space
0282             , name
0283             , &StubsT::BOOST_PP_CAT(func_, BOOST_PP_ITERATION())
0284             , kw
0285             , policies
0286             , doc
0287             , &name_space);
0288     }
0289 };
0290 
0291 #endif // !defined(BOOST_PP_IS_ITERATING)