caller.hpp 8.4 KB

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