invoke.hpp 3.6 KB

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