m2.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright David Abrahams 2001.
  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. // This module exercises the converters exposed in m1 at a low level
  6. // by exposing raw Python extension functions that use wrap<> and
  7. // unwrap<> objects.
  8. #include <boost/python/module.hpp>
  9. #include <boost/python/def.hpp>
  10. #include <boost/python/copy_non_const_reference.hpp>
  11. #include <boost/python/copy_const_reference.hpp>
  12. #include <boost/python/return_value_policy.hpp>
  13. #include "simple_type.hpp"
  14. #if PY_VERSION_HEX >= 0x03000000
  15. # define PyString_FromString PyUnicode_FromString
  16. # define PyInt_FromLong PyLong_FromLong
  17. #endif
  18. // Get a simple (by value) from the argument, and return the
  19. // string it holds.
  20. PyObject* unwrap_simple(simple x)
  21. {
  22. return PyString_FromString(x.s);
  23. }
  24. // Likewise, but demands that its possible to get a non-const
  25. // reference to the simple.
  26. PyObject* unwrap_simple_ref(simple& x)
  27. {
  28. return PyString_FromString(x.s);
  29. }
  30. // Likewise, with a const reference to the simple object.
  31. PyObject* unwrap_simple_const_ref(simple const& x)
  32. {
  33. return PyString_FromString(x.s);
  34. }
  35. // Get an int (by value) from the argument, and convert it to a
  36. // Python Int.
  37. PyObject* unwrap_int(int x)
  38. {
  39. return PyInt_FromLong(x);
  40. }
  41. // Get a non-const reference to an int from the argument
  42. PyObject* unwrap_int_ref(int& x)
  43. {
  44. return PyInt_FromLong(x);
  45. }
  46. // Get a const reference to an int from the argument.
  47. PyObject* unwrap_int_const_ref(int const& x)
  48. {
  49. return PyInt_FromLong(x);
  50. }
  51. #if PY_VERSION_HEX >= 0x03000000
  52. # undef PyString_FromString
  53. # undef PyInt_FromLong
  54. #endif
  55. // rewrap<T> extracts a T from the argument, then converts the T back
  56. // to a PyObject* and returns it.
  57. template <class T>
  58. struct rewrap
  59. {
  60. static T f(T x) { return x; }
  61. };
  62. BOOST_PYTHON_MODULE(m2)
  63. {
  64. using boost::python::return_value_policy;
  65. using boost::python::copy_const_reference;
  66. using boost::python::copy_non_const_reference;
  67. using boost::python::def;
  68. def("unwrap_int", unwrap_int);
  69. def("unwrap_int_ref", unwrap_int_ref);
  70. def("unwrap_int_const_ref", unwrap_int_const_ref);
  71. def("unwrap_simple", unwrap_simple);
  72. def("unwrap_simple_ref", unwrap_simple_ref);
  73. def("unwrap_simple_const_ref", unwrap_simple_const_ref);
  74. def("wrap_int", &rewrap<int>::f);
  75. def("wrap_int_ref", &rewrap<int&>::f
  76. , return_value_policy<copy_non_const_reference>()
  77. );
  78. def("wrap_int_const_ref", &rewrap<int const&>::f
  79. , return_value_policy<copy_const_reference>()
  80. );
  81. def("wrap_simple", &rewrap<simple>::f);
  82. def("wrap_simple_ref", &rewrap<simple&>::f
  83. , return_value_policy<copy_non_const_reference>()
  84. );
  85. def("wrap_simple_const_ref", &rewrap<simple const&>::f
  86. , return_value_policy<copy_const_reference>()
  87. );
  88. }
  89. #include "module_tail.cpp"