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