callbacks.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/python/module.hpp>
  6. #include <boost/python/def.hpp>
  7. #include <boost/python/class.hpp>
  8. #include <boost/ref.hpp>
  9. #include <boost/python/ptr.hpp>
  10. #include <boost/python/return_value_policy.hpp>
  11. #include <boost/python/reference_existing_object.hpp>
  12. #include <boost/python/call.hpp>
  13. #include <boost/python/object.hpp>
  14. #define BOOST_ENABLE_ASSERT_HANDLER
  15. #include <boost/assert.hpp>
  16. using namespace boost::python;
  17. BOOST_STATIC_ASSERT(converter::is_object_manager<handle<> >::value);
  18. int apply_int_int(PyObject* f, int x)
  19. {
  20. return call<int>(f, x);
  21. }
  22. void apply_void_int(PyObject* f, int x)
  23. {
  24. call<void>(f, x);
  25. }
  26. struct X
  27. {
  28. explicit X(int x) : x(x), magic(7654321) { ++counter; }
  29. X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
  30. ~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; }
  31. void set(int _x) { BOOST_ASSERT(magic == 7654321); this->x = _x; }
  32. int value() const { BOOST_ASSERT(magic == 7654321); return x; }
  33. static int count() { return counter; }
  34. private:
  35. void operator=(X const&);
  36. private:
  37. int x;
  38. long magic;
  39. static int counter;
  40. };
  41. X apply_X_X(PyObject* f, X x)
  42. {
  43. return call<X>(f, x);
  44. }
  45. void apply_void_X_ref(PyObject* f, X& x)
  46. {
  47. call<void>(f, boost::ref(x));
  48. }
  49. X& apply_X_ref_handle(PyObject* f, handle<> obj)
  50. {
  51. return call<X&>(f, obj);
  52. }
  53. X* apply_X_ptr_handle_cref(PyObject* f, handle<> const& obj)
  54. {
  55. return call<X*>(f, obj);
  56. }
  57. void apply_void_X_cref(PyObject* f, X const& x)
  58. {
  59. call<void>(f, boost::cref(x));
  60. }
  61. void apply_void_X_ptr(PyObject* f, X* x)
  62. {
  63. call<void>(f, ptr(x));
  64. }
  65. void apply_void_X_deep_ptr(PyObject* f, X* x)
  66. {
  67. call<void>(f, x);
  68. }
  69. char const* apply_cstring_cstring(PyObject* f, char const* s)
  70. {
  71. return call<char const*>(f, s);
  72. }
  73. char const* apply_cstring_pyobject(PyObject* f, PyObject* s)
  74. {
  75. return call<char const*>(f, borrowed(s));
  76. }
  77. char apply_char_char(PyObject* f, char c)
  78. {
  79. return call<char>(f, c);
  80. }
  81. char const* apply_to_string_literal(PyObject* f)
  82. {
  83. return call<char const*>(f, "hello, world");
  84. }
  85. handle<> apply_to_own_type(handle<> x)
  86. {
  87. // Tests that we can return handle<> from a callback and that we
  88. // can pass arbitrary handle<T>.
  89. return call<handle<> >(x.get(), type_handle(borrowed(x->ob_type)));
  90. }
  91. object apply_object_object(PyObject* f, object x)
  92. {
  93. return call<object>(f, x);
  94. }
  95. int X::counter;
  96. BOOST_PYTHON_MODULE(callbacks_ext)
  97. {
  98. def("apply_object_object", apply_object_object);
  99. def("apply_to_own_type", apply_to_own_type);
  100. def("apply_int_int", apply_int_int);
  101. def("apply_void_int", apply_void_int);
  102. def("apply_X_X", apply_X_X);
  103. def("apply_void_X_ref", apply_void_X_ref);
  104. def("apply_void_X_cref", apply_void_X_cref);
  105. def("apply_void_X_ptr", apply_void_X_ptr);
  106. def("apply_void_X_deep_ptr", apply_void_X_deep_ptr);
  107. def("apply_X_ptr_handle_cref", apply_X_ptr_handle_cref
  108. , return_value_policy<reference_existing_object>());
  109. def("apply_X_ref_handle", apply_X_ref_handle
  110. , return_value_policy<reference_existing_object>());
  111. def("apply_cstring_cstring", apply_cstring_cstring);
  112. def("apply_cstring_pyobject", apply_cstring_pyobject);
  113. def("apply_char_char", apply_char_char);
  114. def("apply_to_string_literal", apply_to_string_literal);
  115. class_<X>("X", init<int>())
  116. .def(init<X const&>())
  117. .def("value", &X::value)
  118. .def("set", &X::set)
  119. ;
  120. def("x_count", &X::count);
  121. }
  122. #include "module_tail.cpp"