override.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #if !defined(BOOST_PP_IS_ITERATING)
  2. // Copyright David Abrahams 2004. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef OVERRIDE_DWA2004721_HPP
  6. # define OVERRIDE_DWA2004721_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/converter/return_from_python.hpp>
  9. # include <boost/python/extract.hpp>
  10. # include <boost/python/handle.hpp>
  11. # include <boost/preprocessor/iterate.hpp>
  12. # include <boost/preprocessor/repeat.hpp>
  13. # include <boost/preprocessor/debug/line.hpp>
  14. # include <boost/preprocessor/repetition/enum_params.hpp>
  15. # include <boost/preprocessor/repetition/enum_binary_params.hpp>
  16. # include <boost/type.hpp>
  17. namespace boost { namespace python {
  18. class override;
  19. namespace detail
  20. {
  21. class wrapper_base;
  22. // The result of calling a method.
  23. class method_result
  24. {
  25. private:
  26. friend class boost::python::override;
  27. explicit method_result(PyObject* x)
  28. : m_obj(x)
  29. {}
  30. public:
  31. template <class T>
  32. operator T()
  33. {
  34. converter::return_from_python<T> converter;
  35. return converter(m_obj.release());
  36. }
  37. # if BOOST_WORKAROUND(_MSC_FULL_VER, BOOST_TESTED_AT(140050215))
  38. template <class T>
  39. operator T*()
  40. {
  41. converter::return_from_python<T*> converter;
  42. return converter(m_obj.release());
  43. }
  44. # endif
  45. # if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) || BOOST_WORKAROUND(BOOST_INTEL_WIN, >= 900)
  46. // No operator T&
  47. # else
  48. template <class T>
  49. operator T&() const
  50. {
  51. converter::return_from_python<T&> converter;
  52. return converter(const_cast<handle<>&>(m_obj).release());
  53. }
  54. # endif
  55. template <class T>
  56. T as(type<T>* = 0)
  57. {
  58. converter::return_from_python<T> converter;
  59. return converter(m_obj.release());
  60. }
  61. template <class T>
  62. T unchecked(type<T>* = 0)
  63. {
  64. return extract<T>(m_obj.get())();
  65. }
  66. private:
  67. mutable handle<> m_obj;
  68. };
  69. }
  70. class override : public object
  71. {
  72. private:
  73. friend class detail::wrapper_base;
  74. override(handle<> x)
  75. : object(x)
  76. {}
  77. public:
  78. detail::method_result
  79. operator()() const
  80. {
  81. detail::method_result x(
  82. PyObject_CallFunction(
  83. this->ptr()
  84. , const_cast<char*>("()")
  85. ));
  86. return x;
  87. }
  88. # define BOOST_PYTHON_fast_arg_to_python_get(z, n, _) \
  89. , converter::arg_to_python<A##n>(a##n).get()
  90. # define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PYTHON_MAX_ARITY, <boost/python/override.hpp>))
  91. # include BOOST_PP_ITERATE()
  92. # undef BOOST_PYTHON_fast_arg_to_python_get
  93. };
  94. }} // namespace boost::python
  95. #endif // OVERRIDE_DWA2004721_HPP
  96. #else
  97. # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
  98. && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
  99. # line BOOST_PP_LINE(__LINE__, override.hpp)
  100. # endif
  101. # define N BOOST_PP_ITERATION()
  102. template <
  103. BOOST_PP_ENUM_PARAMS_Z(1, N, class A)
  104. >
  105. detail::method_result
  106. operator()( BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, const& a) ) const
  107. {
  108. detail::method_result x(
  109. PyObject_CallFunction(
  110. this->ptr()
  111. , const_cast<char*>("(" BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FIXED, "O") ")")
  112. BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_fast_arg_to_python_get, nil)
  113. ));
  114. return x;
  115. }
  116. # undef N
  117. #endif