From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- src/boost/libs/python/test/polymorphism.cpp | 162 ++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 src/boost/libs/python/test/polymorphism.cpp (limited to 'src/boost/libs/python/test/polymorphism.cpp') diff --git a/src/boost/libs/python/test/polymorphism.cpp b/src/boost/libs/python/test/polymorphism.cpp new file mode 100644 index 000000000..02713ae26 --- /dev/null +++ b/src/boost/libs/python/test/polymorphism.cpp @@ -0,0 +1,162 @@ +// Copyright David Abrahams 2002. +// 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 +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost::python; + +struct Callback +{ + Callback(PyObject* o) : mSelf(o) {} + PyObject* mSelf; +}; + +struct P +{ + virtual ~P(){} + virtual std::string f() = 0; + std::string g() { return "P::g()"; } +}; + +struct PCallback : P, Callback +{ + PCallback (PyObject* self) : Callback(self) {} + + std::string f() + { + return call_method(mSelf, "f"); + } +}; + +struct Q : virtual P +{ + std::string f() { return "Q::f()"; } +}; + +struct A +{ + virtual ~A(){} + virtual std::string f() { return "A::f()"; } +}; + +struct ACallback : A, Callback +{ + ACallback (PyObject* self) : Callback(self) {} + + + std::string f() + { + return call_method(mSelf, "f"); + } + + std::string default_f() + { + return A::f(); + } +}; + +struct B : A +{ + virtual std::string f() { return "B::f()"; } +}; + +struct C : A +{ + virtual std::string f() { return "C::f()"; } +}; + +struct D : A +{ + virtual std::string f() { return "D::f()"; } + std::string g() { return "D::g()"; } +}; + +struct DCallback : D, Callback +{ + DCallback (PyObject* self) : Callback(self) {} + + std::string f() + { + return call_method(mSelf, "f"); + } + + std::string default_f() + { + return A::f(); + } +}; + + +A& getBCppObj () +{ + static B b; + return b; +} + +std::string call_f(A& a) { return a.f(); } + +A* factory(unsigned choice) +{ + switch (choice % 3) + { + case 0: return new A; + break; + case 1: return new B; + break; + default: return new C; + break; + } +} + +C& getCCppObj () +{ + static C c; + return c; +} + +A* pass_a(A* x) { return x; } + +BOOST_PYTHON_MODULE_INIT(polymorphism_ext) +{ + class_("A") + .def("f", &A::f, &ACallback::default_f) + ; + + def("getBCppObj", getBCppObj, return_value_policy()); + + class_,boost::noncopyable>("C") + .def("f", &C::f) + ; + + class_,DCallback,boost::noncopyable>("D") + .def("f", &D::f, &DCallback::default_f) + .def("g", &D::g) + ; + + def("pass_a", &pass_a, return_internal_reference<>()); + + def("getCCppObj", getCCppObj, return_value_policy()); + + def("factory", factory, return_value_policy()); + + def("call_f", call_f); + + class_("P") + .def("f", pure_virtual(&P::f)) + ; + + class_ >("Q") + .def("g", &P::g) // make sure virtual inheritance doesn't interfere + ; +} + +//#include "module_tail.cpp" -- cgit v1.2.3