annotate DEPENDENCIES/generic/include/boost/python/detail/defaults_gen.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // Copyright David Abrahams 2002, Joel de Guzman, 2002.
Chris@16 4 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 5 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7 //
Chris@16 8 ///////////////////////////////////////////////////////////////////////////////
Chris@16 9 #ifndef DEFAULTS_GEN_JDG20020807_HPP
Chris@16 10 #define DEFAULTS_GEN_JDG20020807_HPP
Chris@16 11
Chris@16 12 #include <boost/python/detail/preprocessor.hpp>
Chris@16 13 #include <boost/preprocessor/repeat.hpp>
Chris@16 14 #include <boost/preprocessor/repeat_from_to.hpp>
Chris@16 15 #include <boost/preprocessor/enum.hpp>
Chris@16 16 #include <boost/preprocessor/enum_params.hpp>
Chris@16 17 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
Chris@16 18 #include <boost/preprocessor/tuple.hpp>
Chris@16 19 #include <boost/preprocessor/cat.hpp>
Chris@16 20 #include <boost/preprocessor/arithmetic/sub.hpp>
Chris@16 21 #include <boost/preprocessor/stringize.hpp>
Chris@16 22 #include <boost/preprocessor/inc.hpp>
Chris@16 23 #include <boost/preprocessor/empty.hpp>
Chris@16 24 #include <boost/preprocessor/comma_if.hpp>
Chris@16 25 #include <boost/config.hpp>
Chris@16 26 #include <boost/mpl/begin_end.hpp>
Chris@16 27 #include <boost/mpl/next.hpp>
Chris@16 28 #include <boost/mpl/deref.hpp>
Chris@16 29 #include <cstddef>
Chris@16 30
Chris@16 31 namespace boost { namespace python {
Chris@16 32
Chris@16 33 namespace detail
Chris@16 34 {
Chris@16 35 // overloads_base is used as a base class for all function
Chris@16 36 // stubs. This class holds the doc_string of the stubs.
Chris@16 37 struct overloads_base
Chris@16 38 {
Chris@16 39 overloads_base(char const* doc_)
Chris@16 40 : m_doc(doc_) {}
Chris@16 41
Chris@16 42 overloads_base(char const* doc_, detail::keyword_range const& kw)
Chris@16 43 : m_doc(doc_), m_keywords(kw) {}
Chris@16 44
Chris@16 45 char const* doc_string() const
Chris@16 46 {
Chris@16 47 return m_doc;
Chris@16 48 }
Chris@16 49
Chris@16 50 detail::keyword_range const& keywords() const
Chris@16 51 {
Chris@16 52 return m_keywords;
Chris@16 53 }
Chris@16 54
Chris@16 55 private:
Chris@16 56 char const* m_doc;
Chris@16 57 detail::keyword_range m_keywords;
Chris@16 58 };
Chris@16 59
Chris@16 60 // overloads_proxy is generated by the overloads_common operator[] (see
Chris@16 61 // below). This class holds a user defined call policies of the stubs.
Chris@16 62 template <class CallPoliciesT, class OverloadsT>
Chris@16 63 struct overloads_proxy
Chris@16 64 : public overloads_base
Chris@16 65 {
Chris@16 66 typedef typename OverloadsT::non_void_return_type non_void_return_type;
Chris@16 67 typedef typename OverloadsT::void_return_type void_return_type;
Chris@16 68
Chris@16 69 overloads_proxy(
Chris@16 70 CallPoliciesT const& policies_
Chris@16 71 , char const* doc
Chris@16 72 , keyword_range const& kw
Chris@16 73 )
Chris@16 74 : overloads_base(doc, kw)
Chris@16 75 , policies(policies_)
Chris@16 76 {}
Chris@16 77
Chris@16 78 CallPoliciesT
Chris@16 79 call_policies() const
Chris@16 80 {
Chris@16 81 return policies;
Chris@16 82 }
Chris@16 83
Chris@16 84 CallPoliciesT policies;
Chris@16 85 };
Chris@16 86
Chris@16 87 // overloads_common is our default function stubs base class. This
Chris@16 88 // class returns the default_call_policies in its call_policies()
Chris@16 89 // member function. It can generate a overloads_proxy however through
Chris@16 90 // its operator[]
Chris@16 91 template <class DerivedT>
Chris@16 92 struct overloads_common
Chris@16 93 : public overloads_base
Chris@16 94 {
Chris@16 95 overloads_common(char const* doc)
Chris@16 96 : overloads_base(doc) {}
Chris@16 97
Chris@16 98 overloads_common(char const* doc, keyword_range const& kw)
Chris@16 99 : overloads_base(doc, kw) {}
Chris@16 100
Chris@16 101 default_call_policies
Chris@16 102 call_policies() const
Chris@16 103 {
Chris@16 104 return default_call_policies();
Chris@16 105 }
Chris@16 106
Chris@16 107 template <class CallPoliciesT>
Chris@16 108 overloads_proxy<CallPoliciesT, DerivedT>
Chris@16 109 operator[](CallPoliciesT const& policies) const
Chris@16 110 {
Chris@16 111 return overloads_proxy<CallPoliciesT, DerivedT>(
Chris@16 112 policies, this->doc_string(), this->keywords());
Chris@16 113 }
Chris@16 114 };
Chris@16 115
Chris@16 116 }}} // namespace boost::python::detail
Chris@16 117
Chris@16 118
Chris@16 119 #define BOOST_PYTHON_TYPEDEF_GEN(z, index, data) \
Chris@16 120 typedef typename ::boost::mpl::next<BOOST_PP_CAT(iter, index)>::type \
Chris@16 121 BOOST_PP_CAT(iter, BOOST_PP_INC(index)); \
Chris@16 122 typedef typename ::boost::mpl::deref<BOOST_PP_CAT(iter, index)>::type \
Chris@16 123 BOOST_PP_CAT(T, index);
Chris@16 124
Chris@16 125 #define BOOST_PYTHON_FUNC_WRAPPER_GEN(z, index, data) \
Chris@16 126 static RT BOOST_PP_CAT(func_, \
Chris@16 127 BOOST_PP_SUB_D(1, index, BOOST_PP_TUPLE_ELEM(3, 1, data))) ( \
Chris@16 128 BOOST_PP_ENUM_BINARY_PARAMS_Z( \
Chris@16 129 1, index, T, arg)) \
Chris@16 130 { \
Chris@16 131 BOOST_PP_TUPLE_ELEM(3, 2, data) \
Chris@16 132 BOOST_PP_TUPLE_ELEM(3, 0, data)( \
Chris@16 133 BOOST_PP_ENUM_PARAMS( \
Chris@16 134 index, \
Chris@16 135 arg)); \
Chris@16 136 }
Chris@16 137
Chris@16 138 #define BOOST_PYTHON_GEN_FUNCTION(fname, fstubs_name, n_args, n_dflts, ret) \
Chris@16 139 struct fstubs_name \
Chris@16 140 { \
Chris@16 141 BOOST_STATIC_CONSTANT(int, n_funcs = BOOST_PP_INC(n_dflts)); \
Chris@16 142 BOOST_STATIC_CONSTANT(int, max_args = n_funcs); \
Chris@16 143 \
Chris@16 144 template <typename SigT> \
Chris@16 145 struct gen \
Chris@16 146 { \
Chris@16 147 typedef typename ::boost::mpl::begin<SigT>::type rt_iter; \
Chris@16 148 typedef typename ::boost::mpl::deref<rt_iter>::type RT; \
Chris@16 149 typedef typename ::boost::mpl::next<rt_iter>::type iter0; \
Chris@16 150 \
Chris@16 151 BOOST_PP_REPEAT_2ND( \
Chris@16 152 n_args, \
Chris@16 153 BOOST_PYTHON_TYPEDEF_GEN, \
Chris@16 154 0) \
Chris@16 155 \
Chris@16 156 BOOST_PP_REPEAT_FROM_TO_2( \
Chris@16 157 BOOST_PP_SUB_D(1, n_args, n_dflts), \
Chris@16 158 BOOST_PP_INC(n_args), \
Chris@16 159 BOOST_PYTHON_FUNC_WRAPPER_GEN, \
Chris@16 160 (fname, BOOST_PP_SUB_D(1, n_args, n_dflts), ret)) \
Chris@16 161 }; \
Chris@16 162 }; \
Chris@16 163
Chris@16 164 ///////////////////////////////////////////////////////////////////////////////
Chris@16 165 #define BOOST_PYTHON_MEM_FUNC_WRAPPER_GEN(z, index, data) \
Chris@16 166 static RT BOOST_PP_CAT(func_, \
Chris@16 167 BOOST_PP_SUB_D(1, index, BOOST_PP_TUPLE_ELEM(3, 1, data))) ( \
Chris@16 168 ClassT obj BOOST_PP_COMMA_IF(index) \
Chris@16 169 BOOST_PP_ENUM_BINARY_PARAMS_Z(1, index, T, arg) \
Chris@16 170 ) \
Chris@16 171 { \
Chris@16 172 BOOST_PP_TUPLE_ELEM(3, 2, data) obj.BOOST_PP_TUPLE_ELEM(3, 0, data)( \
Chris@16 173 BOOST_PP_ENUM_PARAMS(index, arg) \
Chris@16 174 ); \
Chris@16 175 }
Chris@16 176
Chris@16 177 #define BOOST_PYTHON_GEN_MEM_FUNCTION(fname, fstubs_name, n_args, n_dflts, ret) \
Chris@16 178 struct fstubs_name \
Chris@16 179 { \
Chris@16 180 BOOST_STATIC_CONSTANT(int, n_funcs = BOOST_PP_INC(n_dflts)); \
Chris@16 181 BOOST_STATIC_CONSTANT(int, max_args = n_funcs + 1); \
Chris@16 182 \
Chris@16 183 template <typename SigT> \
Chris@16 184 struct gen \
Chris@16 185 { \
Chris@16 186 typedef typename ::boost::mpl::begin<SigT>::type rt_iter; \
Chris@16 187 typedef typename ::boost::mpl::deref<rt_iter>::type RT; \
Chris@16 188 \
Chris@16 189 typedef typename ::boost::mpl::next<rt_iter>::type class_iter; \
Chris@16 190 typedef typename ::boost::mpl::deref<class_iter>::type ClassT; \
Chris@16 191 typedef typename ::boost::mpl::next<class_iter>::type iter0; \
Chris@16 192 \
Chris@16 193 BOOST_PP_REPEAT_2ND( \
Chris@16 194 n_args, \
Chris@16 195 BOOST_PYTHON_TYPEDEF_GEN, \
Chris@16 196 0) \
Chris@16 197 \
Chris@16 198 BOOST_PP_REPEAT_FROM_TO_2( \
Chris@16 199 BOOST_PP_SUB_D(1, n_args, n_dflts), \
Chris@16 200 BOOST_PP_INC(n_args), \
Chris@16 201 BOOST_PYTHON_MEM_FUNC_WRAPPER_GEN, \
Chris@16 202 (fname, BOOST_PP_SUB_D(1, n_args, n_dflts), ret)) \
Chris@16 203 }; \
Chris@16 204 };
Chris@16 205
Chris@16 206 #define BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args, n_dflts) \
Chris@16 207 fstubs_name(char const* doc = 0) \
Chris@16 208 : ::boost::python::detail::overloads_common<fstubs_name>(doc) {} \
Chris@16 209 template <std::size_t N> \
Chris@16 210 fstubs_name(char const* doc, ::boost::python::detail::keywords<N> const& keywords) \
Chris@16 211 : ::boost::python::detail::overloads_common<fstubs_name>( \
Chris@16 212 doc, keywords.range()) \
Chris@16 213 { \
Chris@16 214 typedef typename ::boost::python::detail:: \
Chris@16 215 error::more_keywords_than_function_arguments< \
Chris@16 216 N,n_args>::too_many_keywords assertion; \
Chris@16 217 } \
Chris@16 218 template <std::size_t N> \
Chris@16 219 fstubs_name(::boost::python::detail::keywords<N> const& keywords, char const* doc = 0) \
Chris@16 220 : ::boost::python::detail::overloads_common<fstubs_name>( \
Chris@16 221 doc, keywords.range()) \
Chris@16 222 { \
Chris@16 223 typedef typename ::boost::python::detail:: \
Chris@16 224 error::more_keywords_than_function_arguments< \
Chris@16 225 N,n_args>::too_many_keywords assertion; \
Chris@16 226 }
Chris@16 227
Chris@16 228 # if defined(BOOST_NO_VOID_RETURNS)
Chris@16 229
Chris@16 230 # define BOOST_PYTHON_GEN_FUNCTION_STUB(fname, fstubs_name, n_args, n_dflts) \
Chris@16 231 struct fstubs_name \
Chris@16 232 : public ::boost::python::detail::overloads_common<fstubs_name> \
Chris@16 233 { \
Chris@16 234 BOOST_PYTHON_GEN_FUNCTION( \
Chris@16 235 fname, non_void_return_type, n_args, n_dflts, return) \
Chris@16 236 BOOST_PYTHON_GEN_FUNCTION( \
Chris@16 237 fname, void_return_type, n_args, n_dflts, ;) \
Chris@16 238 \
Chris@16 239 BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args, n_dflts) \
Chris@16 240 };
Chris@16 241
Chris@16 242 # define BOOST_PYTHON_GEN_MEM_FUNCTION_STUB(fname, fstubs_name, n_args, n_dflts) \
Chris@16 243 struct fstubs_name \
Chris@16 244 : public ::boost::python::detail::overloads_common<fstubs_name> \
Chris@16 245 { \
Chris@16 246 BOOST_PYTHON_GEN_MEM_FUNCTION( \
Chris@16 247 fname, non_void_return_type, n_args, n_dflts, return) \
Chris@16 248 BOOST_PYTHON_GEN_MEM_FUNCTION( \
Chris@16 249 fname, void_return_type, n_args, n_dflts, ;) \
Chris@16 250 \
Chris@16 251 BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args + 1, n_dflts) \
Chris@16 252 };
Chris@16 253
Chris@16 254 # else // !defined(BOOST_NO_VOID_RETURNS)
Chris@16 255
Chris@16 256 # define BOOST_PYTHON_GEN_FUNCTION_STUB(fname, fstubs_name, n_args, n_dflts) \
Chris@16 257 struct fstubs_name \
Chris@16 258 : public ::boost::python::detail::overloads_common<fstubs_name> \
Chris@16 259 { \
Chris@16 260 BOOST_PYTHON_GEN_FUNCTION( \
Chris@16 261 fname, non_void_return_type, n_args, n_dflts, return) \
Chris@16 262 \
Chris@16 263 typedef non_void_return_type void_return_type; \
Chris@16 264 BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args, n_dflts) \
Chris@16 265 };
Chris@16 266
Chris@16 267
Chris@16 268 # define BOOST_PYTHON_GEN_MEM_FUNCTION_STUB(fname, fstubs_name, n_args, n_dflts) \
Chris@16 269 struct fstubs_name \
Chris@16 270 : public ::boost::python::detail::overloads_common<fstubs_name> \
Chris@16 271 { \
Chris@16 272 BOOST_PYTHON_GEN_MEM_FUNCTION( \
Chris@16 273 fname, non_void_return_type, n_args, n_dflts, return) \
Chris@16 274 \
Chris@16 275 typedef non_void_return_type void_return_type; \
Chris@16 276 BOOST_PYTHON_OVERLOAD_CONSTRUCTORS(fstubs_name, n_args + 1, n_dflts) \
Chris@16 277 };
Chris@16 278
Chris@16 279 # endif // !defined(BOOST_NO_VOID_RETURNS)
Chris@16 280
Chris@16 281 ///////////////////////////////////////////////////////////////////////////////
Chris@16 282 //
Chris@16 283 // MAIN MACROS
Chris@16 284 //
Chris@16 285 // Given generator_name, fname, min_args and max_args, These macros
Chris@16 286 // generate function stubs that forward to a function or member function
Chris@16 287 // named fname. max_args is the arity of the function or member function
Chris@16 288 // fname. fname can have default arguments. min_args is the minimum
Chris@16 289 // arity that fname can accept.
Chris@16 290 //
Chris@16 291 // There are two versions:
Chris@16 292 //
Chris@16 293 // 1. BOOST_PYTHON_FUNCTION_OVERLOADS for free functions
Chris@16 294 // 2. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS for member functions.
Chris@16 295 //
Chris@16 296 // For instance, given a function:
Chris@16 297 //
Chris@16 298 // int
Chris@16 299 // foo(int a, char b = 1, unsigned c = 2, double d = 3)
Chris@16 300 // {
Chris@16 301 // return a + b + c + int(d);
Chris@16 302 // }
Chris@16 303 //
Chris@16 304 // The macro invocation:
Chris@16 305 //
Chris@16 306 // BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4)
Chris@16 307 //
Chris@16 308 // Generates this code:
Chris@16 309 //
Chris@16 310 // struct foo_stubsNonVoid
Chris@16 311 // {
Chris@16 312 // static const int n_funcs = 4;
Chris@16 313 // static const int max_args = n_funcs;
Chris@16 314 //
Chris@16 315 // template <typename SigT>
Chris@16 316 // struct gen
Chris@16 317 // {
Chris@16 318 // typedef typename ::boost::mpl::begin<SigT>::type rt_iter;
Chris@16 319 // typedef typename rt_iter::type RT;
Chris@16 320 // typedef typename rt_iter::next iter0;
Chris@16 321 // typedef typename iter0::type T0;
Chris@16 322 // typedef typename iter0::next iter1;
Chris@16 323 // typedef typename iter1::type T1;
Chris@16 324 // typedef typename iter1::next iter2;
Chris@16 325 // typedef typename iter2::type T2;
Chris@16 326 // typedef typename iter2::next iter3;
Chris@16 327 // typedef typename iter3::type T3;
Chris@16 328 // typedef typename iter3::next iter4;
Chris@16 329 //
Chris@16 330 // static RT func_0(T0 arg0)
Chris@16 331 // { return foo(arg0); }
Chris@16 332 //
Chris@16 333 // static RT func_1(T0 arg0, T1 arg1)
Chris@16 334 // { return foo(arg0, arg1); }
Chris@16 335 //
Chris@16 336 // static RT func_2(T0 arg0, T1 arg1, T2 arg2)
Chris@16 337 // { return foo(arg0, arg1, arg2); }
Chris@16 338 //
Chris@16 339 // static RT func_3(T0 arg0, T1 arg1, T2 arg2, T3 arg3)
Chris@16 340 // { return foo(arg0, arg1, arg2, arg3); }
Chris@16 341 // };
Chris@16 342 // };
Chris@16 343 //
Chris@16 344 // struct foo_overloads
Chris@16 345 // : public boost::python::detail::overloads_common<foo_overloads>
Chris@16 346 // {
Chris@16 347 // typedef foo_overloadsNonVoid non_void_return_type;
Chris@16 348 // typedef foo_overloadsNonVoid void_return_type;
Chris@16 349 //
Chris@16 350 // foo_overloads(char const* doc = 0)
Chris@16 351 // : boost::python::detail::overloads_common<foo_overloads>(doc) {}
Chris@16 352 // };
Chris@16 353 //
Chris@16 354 // The typedefs non_void_return_type and void_return_type are
Chris@16 355 // used to handle compilers that do not support void returns. The
Chris@16 356 // example above typedefs non_void_return_type and
Chris@16 357 // void_return_type to foo_overloadsNonVoid. On compilers that do
Chris@16 358 // not support void returns, there are two versions:
Chris@16 359 // foo_overloadsNonVoid and foo_overloadsVoid. The "Void"
Chris@16 360 // version is almost identical to the "NonVoid" version except
Chris@16 361 // for the return type (void) and the lack of the return keyword.
Chris@16 362 //
Chris@16 363 // See the overloads_common above for a description of the
Chris@16 364 // foo_overloads' base class.
Chris@16 365 //
Chris@16 366 ///////////////////////////////////////////////////////////////////////////////
Chris@16 367 #define BOOST_PYTHON_FUNCTION_OVERLOADS(generator_name, fname, min_args, max_args) \
Chris@16 368 BOOST_PYTHON_GEN_FUNCTION_STUB( \
Chris@16 369 fname, \
Chris@16 370 generator_name, \
Chris@16 371 max_args, \
Chris@16 372 BOOST_PP_SUB_D(1, max_args, min_args))
Chris@16 373
Chris@16 374 #define BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(generator_name, fname, min_args, max_args) \
Chris@16 375 BOOST_PYTHON_GEN_MEM_FUNCTION_STUB( \
Chris@16 376 fname, \
Chris@16 377 generator_name, \
Chris@16 378 max_args, \
Chris@16 379 BOOST_PP_SUB_D(1, max_args, min_args))
Chris@16 380
Chris@16 381 // deprecated macro names (to be removed)
Chris@16 382 #define BOOST_PYTHON_FUNCTION_GENERATOR BOOST_PYTHON_FUNCTION_OVERLOADS
Chris@16 383 #define BOOST_PYTHON_MEM_FUN_GENERATOR BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS
Chris@16 384
Chris@16 385 ///////////////////////////////////////////////////////////////////////////////
Chris@16 386 #endif // DEFAULTS_GEN_JDG20020807_HPP
Chris@16 387
Chris@16 388