defaults_def.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #if !defined(BOOST_PP_IS_ITERATING)
  10. #ifndef DEFAULTS_DEF_JDG20020811_HPP
  11. #define DEFAULTS_DEF_JDG20020811_HPP
  12. #include <boost/python/detail/defaults_gen.hpp>
  13. #include <boost/python/detail/type_traits.hpp>
  14. #include <boost/mpl/front.hpp>
  15. #include <boost/mpl/size.hpp>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/preprocessor/iterate.hpp>
  18. #include <boost/python/class_fwd.hpp>
  19. #include <boost/python/scope.hpp>
  20. #include <boost/preprocessor/debug/line.hpp>
  21. #include <boost/python/detail/scope.hpp>
  22. #include <boost/python/detail/make_keyword_range_fn.hpp>
  23. #include <boost/python/object/add_to_namespace.hpp>
  24. ///////////////////////////////////////////////////////////////////////////////
  25. namespace boost { namespace python {
  26. struct module;
  27. namespace objects
  28. {
  29. struct class_base;
  30. }
  31. namespace detail
  32. {
  33. // Called as::
  34. //
  35. // name_space_def(ns, "func", func, kw, policies, docstring, &ns)
  36. //
  37. // Dispatch to properly add f to namespace ns.
  38. //
  39. // @group define_stub_function helpers {
  40. template <class Func, class CallPolicies, class NameSpaceT>
  41. static void name_space_def(
  42. NameSpaceT& name_space
  43. , char const* name
  44. , Func f
  45. , keyword_range const& kw
  46. , CallPolicies const& policies
  47. , char const* doc
  48. , objects::class_base*
  49. )
  50. {
  51. typedef typename NameSpaceT::wrapped_type wrapped_type;
  52. objects::add_to_namespace(
  53. name_space, name,
  54. detail::make_keyword_range_function(
  55. f, policies, kw, get_signature(f, (wrapped_type*)0))
  56. , doc
  57. );
  58. }
  59. template <class Func, class CallPolicies>
  60. static void name_space_def(
  61. object& name_space
  62. , char const* name
  63. , Func f
  64. , keyword_range const& kw
  65. , CallPolicies const& policies
  66. , char const* doc
  67. , ...
  68. )
  69. {
  70. scope within(name_space);
  71. detail::scope_setattr_doc(
  72. name
  73. , detail::make_keyword_range_function(f, policies, kw)
  74. , doc);
  75. }
  76. // For backward compatibility -- is this obsolete?
  77. template <class Func, class CallPolicies, class NameSpaceT>
  78. static void name_space_def(
  79. NameSpaceT& name_space
  80. , char const* name
  81. , Func f
  82. , keyword_range const& kw // ignored
  83. , CallPolicies const& policies
  84. , char const* doc
  85. , module*
  86. )
  87. {
  88. name_space.def(name, f, policies, doc);
  89. }
  90. // }
  91. // Expansions of ::
  92. //
  93. // template <typename OverloadsT, typename NameSpaceT>
  94. // inline void
  95. // define_stub_function(
  96. // char const* name, OverloadsT s, NameSpaceT& name_space, mpl::int_<N>)
  97. // {
  98. // name_space.def(name, &OverloadsT::func_N);
  99. // }
  100. //
  101. // where N runs from 0 to BOOST_PYTHON_MAX_ARITY.
  102. //
  103. // The set of overloaded functions (define_stub_function) expects:
  104. //
  105. // 1. char const* name: function name that will be visible to python
  106. // 2. OverloadsT: a function overloads struct (see defaults_gen.hpp)
  107. // 3. NameSpaceT& name_space: a python::class_ or python::module instance
  108. // 4. int_t<N>: the Nth overloaded function (OverloadsT::func_N)
  109. // (see defaults_gen.hpp)
  110. // 5. char const* name: doc string
  111. //
  112. // @group define_stub_function<N> {
  113. template <int N>
  114. struct define_stub_function {};
  115. #define BOOST_PP_ITERATION_PARAMS_1 \
  116. (3, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/detail/defaults_def.hpp>))
  117. #include BOOST_PP_ITERATE()
  118. // }
  119. // This helper template struct does the actual recursive
  120. // definition. There's a generic version
  121. // define_with_defaults_helper<N> and a terminal case
  122. // define_with_defaults_helper<0>. The struct and its
  123. // specialization has a sole static member function def that
  124. // expects:
  125. //
  126. // 1. char const* name: function name that will be
  127. // visible to python
  128. //
  129. // 2. OverloadsT: a function overloads struct
  130. // (see defaults_gen.hpp)
  131. //
  132. // 3. NameSpaceT& name_space: a python::class_ or
  133. // python::module instance
  134. //
  135. // 4. char const* name: doc string
  136. //
  137. // The def static member function calls a corresponding
  138. // define_stub_function<N>. The general case recursively calls
  139. // define_with_defaults_helper<N-1>::def until it reaches the
  140. // terminal case case define_with_defaults_helper<0>.
  141. template <int N>
  142. struct define_with_defaults_helper {
  143. template <class StubsT, class CallPolicies, class NameSpaceT>
  144. static void
  145. def(
  146. char const* name,
  147. StubsT stubs,
  148. keyword_range kw,
  149. CallPolicies const& policies,
  150. NameSpaceT& name_space,
  151. char const* doc)
  152. {
  153. // define the NTH stub function of stubs
  154. define_stub_function<N>::define(name, stubs, kw, policies, name_space, doc);
  155. if (kw.second > kw.first)
  156. --kw.second;
  157. // call the next define_with_defaults_helper
  158. define_with_defaults_helper<N-1>::def(name, stubs, kw, policies, name_space, doc);
  159. }
  160. };
  161. template <>
  162. struct define_with_defaults_helper<0> {
  163. template <class StubsT, class CallPolicies, class NameSpaceT>
  164. static void
  165. def(
  166. char const* name,
  167. StubsT stubs,
  168. keyword_range const& kw,
  169. CallPolicies const& policies,
  170. NameSpaceT& name_space,
  171. char const* doc)
  172. {
  173. // define the Oth stub function of stubs
  174. define_stub_function<0>::define(name, stubs, kw, policies, name_space, doc);
  175. // return
  176. }
  177. };
  178. // define_with_defaults
  179. //
  180. // 1. char const* name: function name that will be
  181. // visible to python
  182. //
  183. // 2. OverloadsT: a function overloads struct
  184. // (see defaults_gen.hpp)
  185. //
  186. // 3. CallPolicies& policies: Call policies
  187. // 4. NameSpaceT& name_space: a python::class_ or
  188. // python::module instance
  189. //
  190. // 5. SigT sig: Function signature typelist
  191. // (see defaults_gen.hpp)
  192. //
  193. // 6. char const* name: doc string
  194. //
  195. // This is the main entry point. This function recursively
  196. // defines all stub functions of StubT (see defaults_gen.hpp) in
  197. // NameSpaceT name_space which can be either a python::class_ or
  198. // a python::module. The sig argument is a typelist that
  199. // specifies the return type, the class (for member functions,
  200. // and the arguments. Here are some SigT examples:
  201. //
  202. // int foo(int) mpl::vector<int, int>
  203. // void bar(int, int) mpl::vector<void, int, int>
  204. // void C::foo(int) mpl::vector<void, C, int>
  205. //
  206. template <class OverloadsT, class NameSpaceT, class SigT>
  207. inline void
  208. define_with_defaults(
  209. char const* name,
  210. OverloadsT const& overloads,
  211. NameSpaceT& name_space,
  212. SigT const&)
  213. {
  214. typedef typename mpl::front<SigT>::type return_type;
  215. typedef typename OverloadsT::void_return_type void_return_type;
  216. typedef typename OverloadsT::non_void_return_type non_void_return_type;
  217. typedef typename mpl::if_c<
  218. is_same<void, return_type>::value
  219. , void_return_type
  220. , non_void_return_type
  221. >::type stubs_type;
  222. BOOST_STATIC_ASSERT(
  223. (stubs_type::max_args) <= mpl::size<SigT>::value);
  224. typedef typename stubs_type::template gen<SigT> gen_type;
  225. define_with_defaults_helper<stubs_type::n_funcs-1>::def(
  226. name
  227. , gen_type()
  228. , overloads.keywords()
  229. , overloads.call_policies()
  230. , name_space
  231. , overloads.doc_string());
  232. }
  233. } // namespace detail
  234. }} // namespace boost::python
  235. #endif // DEFAULTS_DEF_JDG20020811_HPP
  236. #else // defined(BOOST_PP_IS_ITERATING)
  237. // PP vertical iteration code
  238. template <>
  239. struct define_stub_function<BOOST_PP_ITERATION()> {
  240. template <class StubsT, class CallPolicies, class NameSpaceT>
  241. static void define(
  242. char const* name
  243. , StubsT const&
  244. , keyword_range const& kw
  245. , CallPolicies const& policies
  246. , NameSpaceT& name_space
  247. , char const* doc)
  248. {
  249. detail::name_space_def(
  250. name_space
  251. , name
  252. , &StubsT::BOOST_PP_CAT(func_, BOOST_PP_ITERATION())
  253. , kw
  254. , policies
  255. , doc
  256. , &name_space);
  257. }
  258. };
  259. #endif // !defined(BOOST_PP_IS_ITERATING)