Back to home page

EIC code displayed by LXR

 
 

    


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

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 #ifndef DEFAULTS_GEN_JDG20020807_HPP
0010 #define DEFAULTS_GEN_JDG20020807_HPP
0011 
0012 #include <boost/python/detail/preprocessor.hpp>
0013 #include <boost/preprocessor/repeat.hpp>
0014 #include <boost/preprocessor/repeat_from_to.hpp>
0015 #include <boost/preprocessor/enum.hpp>
0016 #include <boost/preprocessor/enum_params.hpp>
0017 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
0018 #include <boost/preprocessor/tuple.hpp>
0019 #include <boost/preprocessor/cat.hpp>
0020 #include <boost/preprocessor/arithmetic/sub.hpp>
0021 #include <boost/preprocessor/stringize.hpp>
0022 #include <boost/preprocessor/inc.hpp>
0023 #include <boost/preprocessor/empty.hpp>
0024 #include <boost/preprocessor/comma_if.hpp>
0025 #include <boost/config.hpp>
0026 #include <boost/mpl/begin_end.hpp>
0027 #include <boost/mpl/next.hpp>
0028 #include <boost/mpl/deref.hpp>
0029 #include <cstddef>
0030 
0031 namespace boost { namespace python {
0032 
0033 namespace detail
0034 {
0035   // overloads_base is used as a base class for all function
0036   // stubs. This class holds the doc_string of the stubs.
0037   struct overloads_base
0038   {
0039       overloads_base(char const* doc_)
0040           : m_doc(doc_) {}
0041 
0042       overloads_base(char const* doc_, detail::keyword_range const& kw)
0043           : m_doc(doc_), m_keywords(kw) {}
0044 
0045       char const* doc_string() const
0046       {
0047           return m_doc;
0048       }
0049 
0050       detail::keyword_range const& keywords() const
0051       {
0052           return m_keywords;
0053       }
0054 
0055    private:
0056       char const* m_doc;
0057       detail::keyword_range m_keywords;
0058   };
0059 
0060   // overloads_proxy is generated by the overloads_common operator[] (see
0061   // below). This class holds a user defined call policies of the stubs.
0062   template <class CallPoliciesT, class OverloadsT>
0063   struct overloads_proxy
0064       : public overloads_base
0065   {
0066       typedef typename OverloadsT::non_void_return_type   non_void_return_type;
0067       typedef typename OverloadsT::void_return_type       void_return_type;
0068 
0069       overloads_proxy(
0070           CallPoliciesT const& policies_
0071           , char const* doc
0072           , keyword_range const& kw
0073           )
0074           : overloads_base(doc, kw)
0075             , policies(policies_)
0076       {}
0077 
0078       CallPoliciesT
0079       call_policies() const
0080       {
0081           return policies;
0082       }
0083 
0084       CallPoliciesT policies;
0085   };
0086 
0087   // overloads_common is our default function stubs base class. This
0088   // class returns the default_call_policies in its call_policies()
0089   // member function.  It can generate a overloads_proxy however through
0090   // its operator[]
0091   template <class DerivedT>
0092   struct overloads_common
0093       : public overloads_base
0094   {
0095       overloads_common(char const* doc)
0096           : overloads_base(doc) {}
0097 
0098       overloads_common(char const* doc, keyword_range const& kw)
0099           : overloads_base(doc, kw) {}
0100 
0101       default_call_policies
0102       call_policies() const
0103       {
0104           return default_call_policies();
0105       }
0106 
0107       template <class CallPoliciesT>
0108       overloads_proxy<CallPoliciesT, DerivedT>
0109       operator[](CallPoliciesT const& policies) const
0110       {
0111           return overloads_proxy<CallPoliciesT, DerivedT>(
0112               policies, this->doc_string(), this->keywords());
0113       }
0114   };
0115 
0116 }}} // namespace boost::python::detail
0117 
0118 
0119 #define BOOST_PYTHON_TYPEDEF_GEN(z, index, data)                                \
0120     typedef typename ::boost::mpl::next<BOOST_PP_CAT(iter, index)>::type        \
0121         BOOST_PP_CAT(iter, BOOST_PP_INC(index));                                \
0122     typedef typename ::boost::mpl::deref<BOOST_PP_CAT(iter, index)>::type       \
0123         BOOST_PP_CAT(T, index);
0124 
0125 #define BOOST_PYTHON_FUNC_WRAPPER_GEN(z, index, data)                   \
0126     static RT BOOST_PP_CAT(func_,                                       \
0127         BOOST_PP_SUB_D(1, index, BOOST_PP_TUPLE_ELEM(3, 1, data))) (    \
0128         BOOST_PP_ENUM_BINARY_PARAMS_Z(                                  \
0129             1, index, T, arg))                                          \
0130     {                                                                   \
0131         BOOST_PP_TUPLE_ELEM(3, 2, data)                                 \
0132         BOOST_PP_TUPLE_ELEM(3, 0, data)(                                \
0133             BOOST_PP_ENUM_PARAMS(                                       \
0134                 index,                                                  \
0135                 arg));                                                  \
0136     }
0137 
0138 #define BOOST_PYTHON_GEN_FUNCTION(fname, fstubs_name, n_args, n_dflts, ret)     \
0139     struct fstubs_name                                                          \
0140     {                                                                           \
0141         BOOST_STATIC_CONSTANT(int, n_funcs = BOOST_PP_INC(n_dflts));            \
0142         BOOST_STATIC_CONSTANT(int, max_args = n_funcs);                         \
0143                                                                                 \
0144         template <typename SigT>                                                \
0145         struct gen                                                              \
0146         {                                                                       \
0147             typedef typename ::boost::mpl::begin<SigT>::type rt_iter;           \
0148             typedef typename ::boost::mpl::deref<rt_iter>::type RT;             \
0149             typedef typename ::boost::mpl::next<rt_iter>::type iter0;           \
0150                                                                                 \
0151             BOOST_PP_REPEAT_2ND(                                                \
0152                 n_args,                                                         \
0153                 BOOST_PYTHON_TYPEDEF_GEN,                                       \
0154                 0)                                                              \
0155                                                                                 \
0156             BOOST_PP_REPEAT_FROM_TO_2(                                          \
0157                 BOOST_PP_SUB_D(1, n_args, n_dflts),                             \
0158                 BOOST_PP_INC(n_args),                                           \
0159                 BOOST_PYTHON_FUNC_WRAPPER_GEN,                                  \
0160                 (fname, BOOST_PP_SUB_D(1, n_args, n_dflts), ret))               \
0161         };                                                                      \
0162     };                                                                          \
0163 
0164 ///////////////////////////////////////////////////////////////////////////////
0165 #define BOOST_PYTHON_MEM_FUNC_WRAPPER_GEN(z, index, data)                       \
0166     static RT BOOST_PP_CAT(func_,                                               \
0167         BOOST_PP_SUB_D(1, index, BOOST_PP_TUPLE_ELEM(3, 1, data))) (            \
0168             ClassT obj BOOST_PP_COMMA_IF(index)                                 \
0169             BOOST_PP_ENUM_BINARY_PARAMS_Z(1, index, T, arg)                     \
0170         )                                                                       \
0171     {                                                                           \
0172         BOOST_PP_TUPLE_ELEM(3, 2, data) obj.BOOST_PP_TUPLE_ELEM(3, 0, data)(    \
0173             BOOST_PP_ENUM_PARAMS(index, arg)                                    \
0174         );                                                                      \
0175     }
0176 
0177 #define BOOST_PYTHON_GEN_MEM_FUNCTION(fname, fstubs_name, n_args, n_dflts, ret) \
0178     struct fstubs_name                                                          \
0179     {                                                                           \
0180         BOOST_STATIC_CONSTANT(int, n_funcs = BOOST_PP_INC(n_dflts));            \
0181         BOOST_STATIC_CONSTANT(int, max_args = n_funcs + 1);                     \
0182                                                                                 \
0183         template <typename SigT>                                                \
0184         struct gen                                                              \
0185         {                                                                       \
0186             typedef typename ::boost::mpl::begin<SigT>::type rt_iter;           \
0187             typedef typename ::boost::mpl::deref<rt_iter>::type RT;             \
0188                                                                                 \
0189             typedef typename ::boost::mpl::next<rt_iter>::type class_iter;      \
0190             typedef typename ::boost::mpl::deref<class_iter>::type ClassT;      \
0191             typedef typename ::boost::mpl::next<class_iter>::type iter0;        \
0192                                                                                 \
0193             BOOST_PP_REPEAT_2ND(                                                \
0194                 n_args,                                                         \
0195                 BOOST_PYTHON_TYPEDEF_GEN,                                       \
0196                 0)                                                              \
0197                                                                                 \
0198             BOOST_PP_REPEAT_FROM_TO_2(                                          \
0199                 BOOST_PP_SUB_D(1, n_args, n_dflts),                             \
0200                 BOOST_PP_INC(n_args),                                           \
0201                 BOOST_PYTHON_MEM_FUNC_WRAPPER_GEN,                              \
0202                 (fname, BOOST_PP_SUB_D(1, n_args, n_dflts), ret))               \
0203         };                                                                      \
0204     };
0205 
0206 #define BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args, n_dflts)                    \
0207     fstubs_name(char const* doc = 0)                                                        \
0208         : ::boost::python::detail::overloads_common<fstubs_name>(doc) {}                    \
0209     template <std::size_t N>                                                                \
0210     fstubs_name(char const* doc, ::boost::python::detail::keywords<N> const& keywords)      \
0211         : ::boost::python::detail::overloads_common<fstubs_name>(                           \
0212             doc, keywords.range())                                                          \
0213     {                                                                                       \
0214         typedef typename ::boost::python::detail::                                          \
0215             error::more_keywords_than_function_arguments<                                   \
0216                 N,n_args>::too_many_keywords assertion BOOST_ATTRIBUTE_UNUSED;              \
0217     }                                                                                       \
0218     template <std::size_t N>                                                                \
0219     fstubs_name(::boost::python::detail::keywords<N> const& keywords, char const* doc = 0)  \
0220         : ::boost::python::detail::overloads_common<fstubs_name>(                           \
0221             doc, keywords.range())                                                          \
0222     {                                                                                       \
0223         typedef typename ::boost::python::detail::                                          \
0224             error::more_keywords_than_function_arguments<                                   \
0225                 N,n_args>::too_many_keywords assertion BOOST_ATTRIBUTE_UNUSED;              \
0226     }
0227 
0228 # if defined(BOOST_NO_VOID_RETURNS)
0229 
0230 #  define BOOST_PYTHON_GEN_FUNCTION_STUB(fname, fstubs_name, n_args, n_dflts)   \
0231     struct fstubs_name                                                          \
0232         : public ::boost::python::detail::overloads_common<fstubs_name>         \
0233     {                                                                           \
0234         BOOST_PYTHON_GEN_FUNCTION(                                              \
0235             fname, non_void_return_type, n_args, n_dflts, return)               \
0236         BOOST_PYTHON_GEN_FUNCTION(                                              \
0237             fname, void_return_type, n_args, n_dflts, ;)                        \
0238                                                                                 \
0239         BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args, n_dflts)        \
0240     };
0241 
0242 #  define BOOST_PYTHON_GEN_MEM_FUNCTION_STUB(fname, fstubs_name, n_args, n_dflts)       \
0243     struct fstubs_name                                                                  \
0244         : public ::boost::python::detail::overloads_common<fstubs_name>                 \
0245     {                                                                                   \
0246         BOOST_PYTHON_GEN_MEM_FUNCTION(                                                  \
0247             fname, non_void_return_type, n_args, n_dflts, return)                       \
0248         BOOST_PYTHON_GEN_MEM_FUNCTION(                                                  \
0249             fname, void_return_type, n_args, n_dflts, ;)                                \
0250                                                                                         \
0251         BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args + 1, n_dflts)            \
0252     };
0253 
0254 # else // !defined(BOOST_NO_VOID_RETURNS)
0255 
0256 #  define BOOST_PYTHON_GEN_FUNCTION_STUB(fname, fstubs_name, n_args, n_dflts)   \
0257     struct fstubs_name                                                          \
0258         : public ::boost::python::detail::overloads_common<fstubs_name>         \
0259     {                                                                           \
0260         BOOST_PYTHON_GEN_FUNCTION(                                              \
0261             fname, non_void_return_type, n_args, n_dflts, return)               \
0262                                                                                 \
0263         typedef non_void_return_type void_return_type;                          \
0264         BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args, n_dflts)        \
0265     };
0266 
0267 
0268 #  define BOOST_PYTHON_GEN_MEM_FUNCTION_STUB(fname, fstubs_name, n_args, n_dflts)       \
0269     struct fstubs_name                                                                  \
0270         : public ::boost::python::detail::overloads_common<fstubs_name>                 \
0271     {                                                                                   \
0272         BOOST_PYTHON_GEN_MEM_FUNCTION(                                                  \
0273             fname, non_void_return_type, n_args, n_dflts, return)                       \
0274                                                                                         \
0275         typedef non_void_return_type void_return_type;                                  \
0276         BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args + 1, n_dflts)            \
0277     };
0278 
0279 # endif // !defined(BOOST_NO_VOID_RETURNS)
0280 
0281 ///////////////////////////////////////////////////////////////////////////////
0282 //
0283 //  MAIN MACROS
0284 //
0285 //      Given generator_name, fname, min_args and max_args, These macros
0286 //      generate function stubs that forward to a function or member function
0287 //      named fname. max_args is the arity of the function or member function
0288 //      fname. fname can have default arguments. min_args is the minimum
0289 //      arity that fname can accept.
0290 //
0291 //      There are two versions:
0292 //
0293 //          1. BOOST_PYTHON_FUNCTION_OVERLOADS for free functions
0294 //          2. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS for member functions.
0295 //
0296 //      For instance, given a function:
0297 //
0298 //      int
0299 //      foo(int a, char b = 1, unsigned c = 2, double d = 3)
0300 //      {
0301 //          return a + b + c + int(d);
0302 //      }
0303 //
0304 //      The macro invocation:
0305 //
0306 //          BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4)
0307 //
0308 //      Generates this code:
0309 //
0310 //      struct foo_stubsNonVoid
0311 //      {
0312 //          static const int n_funcs = 4;
0313 //          static const int max_args = n_funcs;
0314 //
0315 //          template <typename SigT>
0316 //          struct gen
0317 //          {
0318 //              typedef typename ::boost::mpl::begin<SigT>::type    rt_iter;
0319 //              typedef typename rt_iter::type                      RT;
0320 //              typedef typename rt_iter::next                      iter0;
0321 //              typedef typename iter0::type                        T0;
0322 //              typedef typename iter0::next                        iter1;
0323 //              typedef typename iter1::type                        T1;
0324 //              typedef typename iter1::next                        iter2;
0325 //              typedef typename iter2::type                        T2;
0326 //              typedef typename iter2::next                        iter3;
0327 //              typedef typename iter3::type                        T3;
0328 //              typedef typename iter3::next                        iter4;
0329 //
0330 //              static RT func_0(T0 arg0)
0331 //              { return foo(arg0); }
0332 //
0333 //              static RT func_1(T0 arg0, T1 arg1)
0334 //              { return foo(arg0, arg1); }
0335 //
0336 //              static RT func_2(T0 arg0, T1 arg1, T2 arg2)
0337 //              { return foo(arg0, arg1, arg2); }
0338 //
0339 //              static RT func_3(T0 arg0, T1 arg1, T2 arg2, T3 arg3)
0340 //              { return foo(arg0, arg1, arg2, arg3); }
0341 //          };
0342 //      };
0343 //
0344 //      struct foo_overloads
0345 //          : public boost::python::detail::overloads_common<foo_overloads>
0346 //      {
0347 //          typedef foo_overloadsNonVoid    non_void_return_type;
0348 //          typedef foo_overloadsNonVoid    void_return_type;
0349 //
0350 //          foo_overloads(char const* doc = 0)
0351 //             : boost::python::detail::overloads_common<foo_overloads>(doc) {}
0352 //      };
0353 //
0354 //      The typedefs non_void_return_type and void_return_type are
0355 //      used to handle compilers that do not support void returns. The
0356 //      example above typedefs non_void_return_type and
0357 //      void_return_type to foo_overloadsNonVoid. On compilers that do
0358 //      not support void returns, there are two versions:
0359 //      foo_overloadsNonVoid and foo_overloadsVoid.  The "Void"
0360 //      version is almost identical to the "NonVoid" version except
0361 //      for the return type (void) and the lack of the return keyword.
0362 //
0363 //      See the overloads_common above for a description of the
0364 //      foo_overloads' base class.
0365 //
0366 ///////////////////////////////////////////////////////////////////////////////
0367 #define BOOST_PYTHON_FUNCTION_OVERLOADS(generator_name, fname, min_args, max_args)          \
0368     BOOST_PYTHON_GEN_FUNCTION_STUB(                                                         \
0369         fname,                                                                              \
0370         generator_name,                                                                     \
0371         max_args,                                                                           \
0372         BOOST_PP_SUB_D(1, max_args, min_args))
0373 
0374 #define BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(generator_name, fname, min_args, max_args)   \
0375     BOOST_PYTHON_GEN_MEM_FUNCTION_STUB(                                                     \
0376         fname,                                                                              \
0377         generator_name,                                                                     \
0378         max_args,                                                                           \
0379         BOOST_PP_SUB_D(1, max_args, min_args))
0380 
0381 // deprecated macro names (to be removed)
0382 #define BOOST_PYTHON_FUNCTION_GENERATOR BOOST_PYTHON_FUNCTION_OVERLOADS
0383 #define BOOST_PYTHON_MEM_FUN_GENERATOR BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS
0384 
0385 ///////////////////////////////////////////////////////////////////////////////
0386 #endif // DEFAULTS_GEN_JDG20020807_HPP
0387 
0388