call.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 CALL_DWA2002411_HPP
  7. # define CALL_DWA2002411_HPP
  8. # include <boost/python/detail/prefix.hpp>
  9. # include <boost/type.hpp>
  10. # include <boost/python/converter/arg_to_python.hpp>
  11. # include <boost/python/converter/return_from_python.hpp>
  12. # include <boost/python/detail/preprocessor.hpp>
  13. # include <boost/python/detail/void_return.hpp>
  14. # include <boost/preprocessor/comma_if.hpp>
  15. # include <boost/preprocessor/iterate.hpp>
  16. # include <boost/preprocessor/repeat.hpp>
  17. # include <boost/preprocessor/debug/line.hpp>
  18. # include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  19. # include <boost/preprocessor/repetition/enum_binary_params.hpp>
  20. namespace boost { namespace python {
  21. # define BOOST_PYTHON_FAST_ARG_TO_PYTHON_GET(z, n, _) \
  22. , converter::arg_to_python<A##n>(a##n).get()
  23. # define BOOST_PP_ITERATION_PARAMS_1 (3, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/call.hpp>))
  24. # include BOOST_PP_ITERATE()
  25. # undef BOOST_PYTHON_FAST_ARG_TO_PYTHON_GET
  26. }} // namespace boost::python
  27. # endif // CALL_DWA2002411_HPP
  28. // For gcc 4.4 compatability, we must include the
  29. // BOOST_PP_ITERATION_DEPTH test inside an #else clause.
  30. #else // BOOST_PP_IS_ITERATING
  31. #if BOOST_PP_ITERATION_DEPTH() == 1
  32. # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
  33. && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
  34. # line BOOST_PP_LINE(__LINE__, call.hpp)
  35. # endif
  36. # define N BOOST_PP_ITERATION()
  37. template <
  38. class R
  39. BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class A)
  40. >
  41. typename detail::returnable<R>::type
  42. call(PyObject* callable
  43. BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, const& a)
  44. , boost::type<R>* = 0
  45. )
  46. {
  47. PyObject* const result =
  48. PyEval_CallFunction(
  49. callable
  50. , const_cast<char*>("(" BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FIXED, "O") ")")
  51. BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FAST_ARG_TO_PYTHON_GET, nil)
  52. );
  53. // This conversion *must not* be done in the same expression as
  54. // the call, because, in the special case where the result is a
  55. // reference a Python object which was created by converting a C++
  56. // argument for passing to PyEval_CallFunction, its reference
  57. // count will be 2 until the end of the full expression containing
  58. // the conversion, and that interferes with dangling
  59. // pointer/reference detection.
  60. converter::return_from_python<R> converter;
  61. return converter(result);
  62. }
  63. # undef N
  64. #endif // BOOST_PP_ITERATION_DEPTH()
  65. #endif