extract.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/extract.hpp>
  6. #include <boost/python/list.hpp>
  7. #include <boost/python/module.hpp>
  8. #include <boost/python/def.hpp>
  9. #include <boost/python/class.hpp>
  10. #include <boost/python/reference_existing_object.hpp>
  11. #include <boost/python/return_value_policy.hpp>
  12. #include <boost/python/implicit.hpp>
  13. #include <string>
  14. #include <boost/lexical_cast.hpp>
  15. #define BOOST_ENABLE_ASSERT_HANDLER
  16. #include <boost/assert.hpp>
  17. #include "test_class.hpp"
  18. using namespace boost::python;
  19. typedef test_class<> X;
  20. bool extract_bool(object x) { return extract<bool>(x); }
  21. boost::python::list extract_list(object x)
  22. {
  23. extract<list> get_list((x));
  24. // Make sure we always have the right idea about whether it's a list
  25. bool is_list_1 = get_list.check();
  26. bool is_list_2 = PyObject_IsInstance(x.ptr(), (PyObject*)&PyList_Type);
  27. if (is_list_1 != is_list_2) {
  28. throw std::runtime_error("is_list_1 == is_list_2 failure.");
  29. }
  30. return get_list();
  31. }
  32. char const* extract_cstring(object x)
  33. {
  34. return extract<char const*>(x);
  35. }
  36. std::string extract_string(object x)
  37. {
  38. std::string s = extract<std::string>(x);
  39. return s;
  40. }
  41. std::string const& extract_string_cref(object x)
  42. {
  43. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  44. # pragma warning(push)
  45. # pragma warning(disable:4172) // msvc lies about returning a reference to temporary
  46. #elif defined(_MSC_VER) && defined(__ICL) && __ICL <= 900
  47. # pragma warning(push)
  48. # pragma warning(disable:473) // intel/win32 does too
  49. #endif
  50. return extract<std::string const&>(x);
  51. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(_MSC_VER) && defined(__ICL) && __ICL <= 800
  52. # pragma warning(pop)
  53. #endif
  54. }
  55. X extract_X(object x)
  56. {
  57. return extract<X>(x);
  58. }
  59. X* extract_X_ptr(object x) { return extract<X*>(x); }
  60. X& extract_X_ref(object x)
  61. {
  62. extract<X&> get_x(x);
  63. return get_x;
  64. }
  65. int double_X(object n)
  66. {
  67. extract<X> x(n);
  68. return x().value() + x().value();
  69. }
  70. bool check_bool(object x) { return extract<bool>(x).check(); }
  71. bool check_list(object x) { return extract<list>(x).check(); }
  72. bool check_cstring(object x) { return extract<char const*>(x).check(); }
  73. bool check_string(object x) { return extract<std::string>(x).check(); }
  74. bool check_string_cref(object x) { return extract<std::string const&>(x).check(); }
  75. bool check_X(object x) { return extract<X>(x).check(); }
  76. bool check_X_ptr(object x) { return extract<X*>(x).check(); }
  77. bool check_X_ref(object x) { return extract<X&>(x).check(); }
  78. std::string x_rep(X const& x)
  79. {
  80. return "X(" + boost::lexical_cast<std::string>(x.value()) + ")";
  81. }
  82. BOOST_PYTHON_MODULE(extract_ext)
  83. {
  84. implicitly_convertible<int, X>();
  85. def("extract_bool", extract_bool);
  86. def("extract_list", extract_list);
  87. def("extract_cstring", extract_cstring);
  88. def("extract_string", extract_string);
  89. def("extract_string_cref", extract_string_cref, return_value_policy<reference_existing_object>());
  90. def("extract_X", extract_X);
  91. def("extract_X_ptr", extract_X_ptr, return_value_policy<reference_existing_object>());
  92. def("extract_X_ref", extract_X_ref, return_value_policy<reference_existing_object>());
  93. def("check_bool", check_bool);
  94. def("check_list", check_list);
  95. def("check_cstring", check_cstring);
  96. def("check_string", check_string);
  97. def("check_string_cref", check_string_cref);
  98. def("check_X", check_X);
  99. def("check_X_ptr", check_X_ptr);
  100. def("check_X_ref", check_X_ref);
  101. def("double_X", double_X);
  102. def("count_Xs", &X::count);
  103. ;
  104. object x_class(
  105. class_<X>("X", init<int>())
  106. .def( "__repr__", x_rep));
  107. // Instantiate an X object through the Python interface
  108. object x_obj = x_class(3);
  109. // Get the C++ object out of the Python object
  110. X const& x = extract<X&>(x_obj);
  111. if (x.value() != 3) {
  112. throw std::runtime_error("x.value() == 3 failure.");
  113. }
  114. }
  115. #include "module_tail.cpp"