defaults_gen.hpp 20 KB

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