// Copyright David Abrahams 2002. // Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include "test_class.hpp" using namespace boost::python; 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 shared_ptr &get1() { return storage; } static int look_store() { return look(get()); } template static void expose(C const& c) { def("look", &look); def("store", &store); 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("get1", &get1, return_internal_reference<>()) .staticmethod("get1") .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; } static shared_ptr null(T const&) { return shared_ptr(); } 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) {} }; struct YYY : Y { YYY(int n) : Y(n) {} }; shared_ptr factory(int n) { return shared_ptr(n < 42 ? new Y(n) : new YY(n)); } // regressions from Nicodemus struct A { virtual ~A() {}; // silence compiler warnings virtual int f() = 0; static int call_f(shared_ptr& a) { return a->f(); } }; struct B: A { int f() { return 1; } }; shared_ptr New(bool make) { return 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; }; // ------ // from Neal Becker struct Test { shared_ptr x; }; // ------ BOOST_PYTHON_MODULE(MODULE) { 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); def("factory", factory); functions::expose( class_("X", init()) .def("value", &X::value) ); functions::expose( class_ >("Y", init()) .def("value", &Y::value) ); class_, boost::noncopyable>("YY", init()) ; class_, bases >("YYY", init()) ; functions::expose( class_("Z", init()) .def("value", &Z::value) .def("v", &Z::v, &ZWrap::default_v) ); // from Neal Becker class_ ("Test") .def_readonly ("x", &Test::x, "x") ; // ------ }