// Copyright David Abrahams 2002. Permission to copy, use, // modify, sell and distribute this software is granted provided this // copyright notice appears in all copies. This software is provided // "as is" without express or implied warranty, and with no claim as // to its suitability for any purpose. #include #include #include #include #include #include #include "test_class.hpp" #include using namespace boost::python; using boost::shared_ptr; typedef test_class<> X; typedef test_class<1> Y; template struct functions { static int look(shared_ptr const& x) { return (x.get()) ? x->value() : -1; } static void store(shared_ptr x) { storage = x; } static void release_store() { store(shared_ptr()); } static void modify(shared_ptr& x) { x.reset(); } static shared_ptr get() { return storage; } static int look_store() { return look(get()); } static void expose() { def("look", &look); def("store", &store); def("modify", &modify); def("look_store", &look_store); } static shared_ptr storage; }; template shared_ptr functions::storage; struct Z : test_class<2> { Z(int x) : test_class<2>(x) {} virtual int v() { return this->value(); } }; struct ZWrap : Z { ZWrap(PyObject* self, int x) : Z(x), m_self(self) {} virtual int v() { return call_method(m_self, "v"); } int default_v() { return Z::v(); } PyObject* m_self; }; struct YY : Y { YY(int n) : Y(n) {} }; shared_ptr factory(int n) { return shared_ptr(n < 42 ? new Y(n) : new YY(n)); } static int stored_v() { return functions::get()->v(); } static shared_ptr stored_z() { return functions::get(); } // regressions from Nicodemus struct A { virtual int f() = 0; static int call_f(shared_ptr& a) { return a->f(); } }; struct B: A { int f() { return 1; } }; boost::shared_ptr New(bool make) { return boost::shared_ptr( make ? new B() : 0 ); } struct A_Wrapper: A { A_Wrapper(PyObject* self_): A(), self(self_) {} int f() { return call_method< int >(self, "f"); } PyObject* self; }; // ------ BOOST_PYTHON_MODULE(shared_ptr_ext) { class_, boost::noncopyable>("A") .def("call_f", &A::call_f) .staticmethod("call_f") ; // This is the ugliness required to register a to-python converter // for shared_ptr. objects::class_value_wrapper< shared_ptr , objects::make_ptr_instance,A> > >(); def("New", &New); class_("X", init()) .def("value", &X::value) ; def("factory", factory); functions::expose(); def("x_count", &X::count); def("x_release", &functions::release_store); def("x_look_store", &functions::look_store); class_ >("Y", init()) .def("value", &Y::value) ; class_, boost::noncopyable>("YY", init()) ; functions::expose(); def("y_count", &Y::count); def("y_release", &functions::release_store); def("y_look_store", &functions::look_store); class_("Z", init()) .def("value", &Z::value) .def("v", &Z::v, &ZWrap::default_v) ; functions::expose(); def("z_count", &Z::count); def("z_release", &functions::release_store); def("z_look_store", &functions::look_store); def("stored_z", &stored_z); def("stored_v", &stored_v); } #include "module_tail.cpp"