1 // Copyright David Abrahams 2002.
2 // Copyright Stefan Seefeld 2016.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #include "test_class.hpp"
9 using namespace boost::python;
11 typedef test_class<> X;
12 typedef test_class<1> Y;
17 static int look(shared_ptr<T> const& x)
19 return (x.get()) ? x->value() : -1;
22 static void store(shared_ptr<T> x)
27 static void release_store()
29 store(shared_ptr<T>());
32 static void modify(shared_ptr<T>& x)
37 static shared_ptr<T> get() { return storage; }
38 static shared_ptr<T> &get1() { return storage; }
40 static int look_store()
46 static void expose(C const& c)
50 def("modify", &modify);
51 def("identity", &identity);
58 .staticmethod("store")
59 .def("modify", &modify)
60 .staticmethod("modify")
61 .def("look_store", &look_store)
62 .staticmethod("look_store")
63 .def("identity", &identity)
64 .staticmethod("identity")
67 .def("get1", &get1, return_internal_reference<>())
71 .def("count", &T::count)
72 .staticmethod("count")
73 .def("release", &release_store)
74 .staticmethod("release")
78 static shared_ptr<T> identity(shared_ptr<T> x) { return x; }
79 static shared_ptr<T> null(T const&) { return shared_ptr<T>(); }
82 static shared_ptr<T> storage;
85 template <class T> shared_ptr<T> functions<T>::storage;
87 struct Z : test_class<2>
89 Z(int x) : test_class<2>(x) {}
90 virtual int v() { return this->value(); }
95 ZWrap(PyObject* self, int x)
96 : Z(x), m_self(self) {}
99 virtual int v() { return call_method<int>(m_self, "v"); }
100 int default_v() { return Z::v(); }
116 shared_ptr<Y> factory(int n)
118 return shared_ptr<Y>(n < 42 ? new Y(n) : new YY(n));
121 // regressions from Nicodemus
124 virtual ~A() {}; // silence compiler warnings
126 static int call_f(shared_ptr<A>& a) { return a->f(); }
131 int f() { return 1; }
134 shared_ptr<A> New(bool make)
136 return shared_ptr<A>( make ? new B() : 0 );
141 A_Wrapper(PyObject* self_):
145 return call_method< int >(self, "f");
161 BOOST_PYTHON_MODULE(MODULE)
163 class_<A, shared_ptr<A_Wrapper>, boost::noncopyable>("A")
164 .def("call_f", &A::call_f)
165 .staticmethod("call_f")
168 // This is the ugliness required to register a to-python converter
169 // for shared_ptr<A>.
170 objects::class_value_wrapper<
172 , objects::make_ptr_instance<A, objects::pointer_holder<shared_ptr<A>,A> >
177 def("factory", factory);
179 functions<X>::expose(
180 class_<X, boost::noncopyable>("X", init<int>())
181 .def("value", &X::value)
184 functions<Y>::expose(
185 class_<Y, shared_ptr<Y> >("Y", init<int>())
186 .def("value", &Y::value)
189 class_<YY, bases<Y>, boost::noncopyable>("YY", init<int>())
192 class_<YYY, shared_ptr<YYY>, bases<Y> >("YYY", init<int>())
195 functions<Z>::expose(
196 class_<Z, ZWrap>("Z", init<int>())
197 .def("value", &Z::value)
198 .def("v", &Z::v, &ZWrap::default_v)
202 class_<Test> ("Test")
203 .def_readonly ("x", &Test::x, "x")