caller.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #if !defined(BOOST_PP_IS_ITERATING)
  2. // Copyright David Abrahams 2002.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. # ifndef CALLER_DWA20021121_HPP
  7. # define CALLER_DWA20021121_HPP
  8. # include <boost/python/type_id.hpp>
  9. # include <boost/python/handle.hpp>
  10. # include <boost/detail/indirect_traits.hpp>
  11. # include <boost/python/detail/invoke.hpp>
  12. # include <boost/python/detail/signature.hpp>
  13. # include <boost/python/detail/preprocessor.hpp>
  14. # include <boost/python/arg_from_python.hpp>
  15. # include <boost/python/converter/context_result_converter.hpp>
  16. # include <boost/python/converter/builtin_converters.hpp>
  17. # include <boost/preprocessor/iterate.hpp>
  18. # include <boost/preprocessor/cat.hpp>
  19. # include <boost/preprocessor/dec.hpp>
  20. # include <boost/preprocessor/if.hpp>
  21. # include <boost/preprocessor/iteration/local.hpp>
  22. # include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  23. # include <boost/preprocessor/repetition/repeat.hpp>
  24. # include <boost/compressed_pair.hpp>
  25. # include <boost/type_traits/is_same.hpp>
  26. # include <boost/type_traits/is_convertible.hpp>
  27. # include <boost/mpl/apply.hpp>
  28. # include <boost/mpl/eval_if.hpp>
  29. # include <boost/mpl/identity.hpp>
  30. # include <boost/mpl/size.hpp>
  31. # include <boost/mpl/at.hpp>
  32. # include <boost/mpl/int.hpp>
  33. # include <boost/mpl/next.hpp>
  34. namespace boost { namespace python { namespace detail {
  35. template <int N>
  36. inline PyObject* get(mpl::int_<N>, PyObject* const& args_)
  37. {
  38. return PyTuple_GET_ITEM(args_,N);
  39. }
  40. inline unsigned arity(PyObject* const& args_)
  41. {
  42. return PyTuple_GET_SIZE(args_);
  43. }
  44. // This "result converter" is really just used as
  45. // a dispatch tag to invoke(...), selecting the appropriate
  46. // implementation
  47. typedef int void_result_to_python;
  48. // Given a model of CallPolicies and a C++ result type, this
  49. // metafunction selects the appropriate converter to use for
  50. // converting the result to python.
  51. template <class Policies, class Result>
  52. struct select_result_converter
  53. : mpl::eval_if<
  54. is_same<Result,void>
  55. , mpl::identity<void_result_to_python>
  56. , mpl::apply1<typename Policies::result_converter,Result>
  57. >
  58. {
  59. };
  60. template <class ArgPackage, class ResultConverter>
  61. inline ResultConverter create_result_converter(
  62. ArgPackage const& args_
  63. , ResultConverter*
  64. , converter::context_result_converter*
  65. )
  66. {
  67. return ResultConverter(args_);
  68. }
  69. template <class ArgPackage, class ResultConverter>
  70. inline ResultConverter create_result_converter(
  71. ArgPackage const&
  72. , ResultConverter*
  73. , ...
  74. )
  75. {
  76. return ResultConverter();
  77. }
  78. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  79. template <class ResultConverter>
  80. struct converter_target_type
  81. {
  82. static PyTypeObject const *get_pytype()
  83. {
  84. return create_result_converter((PyObject*)0, (ResultConverter *)0, (ResultConverter *)0).get_pytype();
  85. }
  86. };
  87. template < >
  88. struct converter_target_type <void_result_to_python >
  89. {
  90. static PyTypeObject const *get_pytype()
  91. {
  92. return 0;
  93. }
  94. };
  95. #endif
  96. template <unsigned> struct caller_arity;
  97. template <class F, class CallPolicies, class Sig>
  98. struct caller;
  99. # define BOOST_PYTHON_NEXT(init,name,n) \
  100. typedef BOOST_PP_IF(n,typename mpl::next< BOOST_PP_CAT(name,BOOST_PP_DEC(n)) >::type, init) name##n;
  101. # define BOOST_PYTHON_ARG_CONVERTER(n) \
  102. BOOST_PYTHON_NEXT(typename mpl::next<first>::type, arg_iter,n) \
  103. typedef arg_from_python<BOOST_DEDUCED_TYPENAME arg_iter##n::type> c_t##n; \
  104. c_t##n c##n(get(mpl::int_<n>(), inner_args)); \
  105. if (!c##n.convertible()) \
  106. return 0;
  107. # define BOOST_PP_ITERATION_PARAMS_1 \
  108. (3, (0, BOOST_PYTHON_MAX_ARITY + 1, <boost/python/detail/caller.hpp>))
  109. # include BOOST_PP_ITERATE()
  110. # undef BOOST_PYTHON_ARG_CONVERTER
  111. # undef BOOST_PYTHON_NEXT
  112. // A metafunction returning the base class used for caller<class F,
  113. // class ConverterGenerators, class CallPolicies, class Sig>.
  114. template <class F, class CallPolicies, class Sig>
  115. struct caller_base_select
  116. {
  117. enum { arity = mpl::size<Sig>::value - 1 };
  118. typedef typename caller_arity<arity>::template impl<F,CallPolicies,Sig> type;
  119. };
  120. // A function object type which wraps C++ objects as Python callable
  121. // objects.
  122. //
  123. // Template Arguments:
  124. //
  125. // F -
  126. // the C++ `function object' that will be called. Might
  127. // actually be any data for which an appropriate invoke_tag() can
  128. // be generated. invoke(...) takes care of the actual invocation syntax.
  129. //
  130. // CallPolicies -
  131. // The precall, postcall, and what kind of resultconverter to
  132. // generate for mpl::front<Sig>::type
  133. //
  134. // Sig -
  135. // The `intended signature' of the function. An MPL sequence
  136. // beginning with a result type and continuing with a list of
  137. // argument types.
  138. template <class F, class CallPolicies, class Sig>
  139. struct caller
  140. : caller_base_select<F,CallPolicies,Sig>::type
  141. {
  142. typedef typename caller_base_select<
  143. F,CallPolicies,Sig
  144. >::type base;
  145. typedef PyObject* result_type;
  146. caller(F f, CallPolicies p) : base(f,p) {}
  147. };
  148. }}} // namespace boost::python::detail
  149. # endif // CALLER_DWA20021121_HPP
  150. #else
  151. # define N BOOST_PP_ITERATION()
  152. template <>
  153. struct caller_arity<N>
  154. {
  155. template <class F, class Policies, class Sig>
  156. struct impl
  157. {
  158. impl(F f, Policies p) : m_data(f,p) {}
  159. PyObject* operator()(PyObject* args_, PyObject*) // eliminate
  160. // this
  161. // trailing
  162. // keyword dict
  163. {
  164. typedef typename mpl::begin<Sig>::type first;
  165. typedef typename first::type result_t;
  166. typedef typename select_result_converter<Policies, result_t>::type result_converter;
  167. typedef typename Policies::argument_package argument_package;
  168. argument_package inner_args(args_);
  169. # if N
  170. # define BOOST_PP_LOCAL_MACRO(i) BOOST_PYTHON_ARG_CONVERTER(i)
  171. # define BOOST_PP_LOCAL_LIMITS (0, N-1)
  172. # include BOOST_PP_LOCAL_ITERATE()
  173. # endif
  174. // all converters have been checked. Now we can do the
  175. // precall part of the policy
  176. if (!m_data.second().precall(inner_args))
  177. return 0;
  178. PyObject* result = detail::invoke(
  179. detail::invoke_tag<result_t,F>()
  180. , create_result_converter(args_, (result_converter*)0, (result_converter*)0)
  181. , m_data.first()
  182. BOOST_PP_ENUM_TRAILING_PARAMS(N, c)
  183. );
  184. return m_data.second().postcall(inner_args, result);
  185. }
  186. static unsigned min_arity() { return N; }
  187. static py_func_sig_info signature()
  188. {
  189. const signature_element * sig = detail::signature<Sig>::elements();
  190. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  191. typedef BOOST_DEDUCED_TYPENAME Policies::template extract_return_type<Sig>::type rtype;
  192. typedef typename select_result_converter<Policies, rtype>::type result_converter;
  193. static const signature_element ret = {
  194. (boost::is_void<rtype>::value ? "void" : type_id<rtype>().name())
  195. , &detail::converter_target_type<result_converter>::get_pytype
  196. , boost::detail::indirect_traits::is_reference_to_non_const<rtype>::value
  197. };
  198. py_func_sig_info res = {sig, &ret };
  199. #else
  200. py_func_sig_info res = {sig, sig };
  201. #endif
  202. return res;
  203. }
  204. private:
  205. compressed_pair<F,Policies> m_data;
  206. };
  207. };
  208. #endif // BOOST_PP_IS_ITERATING