invoke.hpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 INVOKE_DWA20021122_HPP
  7. # define INVOKE_DWA20021122_HPP
  8. # include <boost/python/detail/prefix.hpp>
  9. # include <boost/python/detail/preprocessor.hpp>
  10. # include <boost/python/detail/none.hpp>
  11. # include <boost/preprocessor/iterate.hpp>
  12. # include <boost/preprocessor/facilities/intercept.hpp>
  13. # include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  14. # include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
  15. # include <boost/preprocessor/repetition/enum_binary_params.hpp>
  16. # include <boost/python/to_python_value.hpp>
  17. // This file declares a series of overloaded invoke(...) functions,
  18. // used to invoke wrapped C++ function (object)s from Python. Each one
  19. // accepts:
  20. //
  21. // - a tag which identifies the invocation syntax (e.g. member
  22. // functions must be invoked with a different syntax from regular
  23. // functions)
  24. //
  25. // - a pointer to a result converter type, used solely as a way of
  26. // transmitting the type of the result converter to the function (or
  27. // an int, if the return type is void).
  28. //
  29. // - the "function", which may be a function object, a function or
  30. // member function pointer, or a defaulted_virtual_fn.
  31. //
  32. // - The arg_from_python converters for each of the arguments to be
  33. // passed to the function being invoked.
  34. namespace boost { namespace python { namespace detail {
  35. // This "result converter" is really just used as a dispatch tag to
  36. // invoke(...), selecting the appropriate implementation
  37. typedef int void_result_to_python;
  38. template <bool void_return, bool member>
  39. struct invoke_tag_ {};
  40. // A metafunction returning the appropriate tag type for invoking an
  41. // object of type F with return type R.
  42. template <class R, class F>
  43. struct invoke_tag
  44. : invoke_tag_<
  45. is_same<R,void>::value
  46. , is_member_function_pointer<F>::value
  47. >
  48. {
  49. };
  50. # define BOOST_PP_ITERATION_PARAMS_1 \
  51. (3, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/detail/invoke.hpp>))
  52. # include BOOST_PP_ITERATE()
  53. }}} // namespace boost::python::detail
  54. # endif // INVOKE_DWA20021122_HPP
  55. #else
  56. # define N BOOST_PP_ITERATION()
  57. template <class RC, class F BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class AC)>
  58. inline PyObject* invoke(invoke_tag_<false,false>, RC const& rc, F& f BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(1, N, AC, & ac) )
  59. {
  60. return rc(f( BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, ac, () BOOST_PP_INTERCEPT) ));
  61. }
  62. template <class RC, class F BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class AC)>
  63. inline PyObject* invoke(invoke_tag_<true,false>, RC const&, F& f BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(1, N, AC, & ac) )
  64. {
  65. f( BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, ac, () BOOST_PP_INTERCEPT) );
  66. return none();
  67. }
  68. template <class RC, class F, class TC BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class AC)>
  69. inline PyObject* invoke(invoke_tag_<false,true>, RC const& rc, F& f, TC& tc BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(1, N, AC, & ac) )
  70. {
  71. return rc( (tc().*f)(BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, ac, () BOOST_PP_INTERCEPT)) );
  72. }
  73. template <class RC, class F, class TC BOOST_PP_ENUM_TRAILING_PARAMS_Z(1, N, class AC)>
  74. inline PyObject* invoke(invoke_tag_<true,true>, RC const&, F& f, TC& tc BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(1, N, AC, & ac) )
  75. {
  76. (tc().*f)(BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, ac, () BOOST_PP_INTERCEPT));
  77. return none();
  78. }
  79. # undef N
  80. #endif // BOOST_PP_IS_ITERATING