diff --git a/test/shared_ptr.cpp b/test/shared_ptr.cpp index d254a9d2..2d0f0ec7 100644 --- a/test/shared_ptr.cpp +++ b/test/shared_ptr.cpp @@ -49,15 +49,36 @@ struct functions { return look(get()); } - - static void expose() + + template + static void expose(C const& c) { def("look", &look); def("store", &store); - def("modify", &modify); - def("look_store", &look_store); - def("identity", &identity); - def("null", &null); + def("modify", &modify); + def("identity", &identity); + def("null", &null); + + const_cast(c) + .def("look", &look) + .staticmethod("look") + .def("store", &store) + .staticmethod("store") + .def("modify", &modify) + .staticmethod("modify") + .def("look_store", &look_store) + .staticmethod("look_store") + .def("identity", &identity) + .staticmethod("identity") + .def("null", &null) + .staticmethod("null") + .def("get", &get) + .staticmethod("get") + .def("count", &T::count) + .staticmethod("count") + .def("release", &release_store) + .staticmethod("release") + ; } static shared_ptr identity(shared_ptr x) { return x; } @@ -93,14 +114,16 @@ struct YY : Y YY(int n) : Y(n) {} }; +struct YYY : Y +{ + YYY(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 { @@ -147,41 +170,30 @@ BOOST_PYTHON_MODULE(shared_ptr_ext) >(); 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); + functions::expose( + class_("X", init()) + .def("value", &X::value) + ); - class_ >("Y", init()) - .def("value", &Y::value) - ; + functions::expose( + 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) + class_, bases >("YYY", init()) ; - - 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); + + functions::expose( + class_("Z", init()) + .def("value", &Z::value) + .def("v", &Z::v, &ZWrap::default_v) + ); } #include "module_tail.cpp" diff --git a/test/shared_ptr.py b/test/shared_ptr.py index b87b86b5..8bfd03f5 100644 --- a/test/shared_ptr.py +++ b/test/shared_ptr.py @@ -1,6 +1,10 @@ ''' >>> from shared_ptr_ext import * + Test that shared_ptr can be converted to shared_ptr + +>>> Y.store(YYY(42)) + >>> x = X(17) >>> null_x = null(x) >>> null_x # should be None @@ -36,15 +40,15 @@ -1 >>> store(p) >>> del p ->>> stored_v() +>>> Z.get().v() -12 ->>> z_count() +>>> Z.count() 1 ->>> z_look_store() +>>> Z.look_store() 12 ->>> z_release() +>>> Z.release() bye ->>> z_count() +>>> Z.count() 0 >>> z = Z(13) @@ -56,18 +60,18 @@ bye ... except TypeError: pass ... else: 'print expected a TypeError' ->>> stored_z() # should be None +>>> Z.get() # should be None >>> store(z) ->>> assert stored_z() is z # show that deleter introspection works +>>> assert Z.get() is z # show that deleter introspection works >>> del z ->>> stored_v() +>>> Z.get().value() 13 ->>> z_count() +>>> Z.count() 1 ->>> z_look_store() +>>> Z.look_store() 13 ->>> z_release() ->>> z_count() +>>> Z.release() +>>> Z.count() 0 >>> x = X(17) @@ -82,12 +86,12 @@ bye -1 >>> store(x) >>> del x ->>> x_count() +>>> X.count() 1 ->>> x_look_store() +>>> X.look_store() 17 ->>> x_release() ->>> x_count() +>>> X.release() +>>> X.count() 0 @@ -98,12 +102,12 @@ bye >>> look(y) -1 >>> store(Y(23)) ->>> y_count() +>>> Y.count() 1 ->>> y_look_store() +>>> Y.look_store() 23 ->>> y_release() ->>> y_count() +>>> Y.release() +>>> Y.count() 0 '''