auto_ptr.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "test_class.hpp"
  7. #include <boost/python/class.hpp>
  8. #include <boost/python/extract.hpp>
  9. #include <boost/python/def.hpp>
  10. #include <boost/python/implicit.hpp>
  11. #include <boost/detail/workaround.hpp>
  12. #include <memory>
  13. using namespace boost::python;
  14. typedef test_class<> X;
  15. struct Y : X
  16. {
  17. Y(int n) : X(n) {};
  18. };
  19. int look(std::auto_ptr<X> const& x)
  20. {
  21. return (x.get()) ? x->value() : -1;
  22. }
  23. int steal(std::auto_ptr<X> x)
  24. {
  25. return x->value();
  26. }
  27. int maybe_steal(std::auto_ptr<X>& x, bool doit)
  28. {
  29. int n = x->value();
  30. if (doit)
  31. x.release();
  32. return n;
  33. }
  34. std::auto_ptr<X> make()
  35. {
  36. return std::auto_ptr<X>(new X(77));
  37. }
  38. std::auto_ptr<X> callback(object f)
  39. {
  40. std::auto_ptr<X> x(new X(77));
  41. return call<std::auto_ptr<X> >(f.ptr(), x);
  42. }
  43. std::auto_ptr<X> extract_(object o)
  44. {
  45. return extract<std::auto_ptr<X>&>(o)
  46. #if BOOST_MSVC <= 1300
  47. ()
  48. #endif
  49. ;
  50. }
  51. BOOST_PYTHON_MODULE(auto_ptr_ext)
  52. {
  53. class_<X, std::auto_ptr<X>, boost::noncopyable>("X", init<int>())
  54. .def("value", &X::value)
  55. ;
  56. class_<Y, std::auto_ptr<Y>, bases<X>, boost::noncopyable>("Y", init<int>())
  57. ;
  58. // VC6 auto_ptrs do not have converting constructors
  59. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, < 306)
  60. scope().attr("broken_auto_ptr") = 1;
  61. #else
  62. scope().attr("broken_auto_ptr") = 0;
  63. implicitly_convertible<std::auto_ptr<Y>, std::auto_ptr<X> >();
  64. #endif
  65. def("look", look);
  66. def("steal", steal);
  67. def("maybe_steal", maybe_steal);
  68. def("make", make);
  69. def("callback", callback);
  70. def("extract", extract_);
  71. }
  72. #include "module_tail.cpp"