(original) (raw)

testboost/0000755000175000017500000000000011140664116012446 5ustar renatorenatotestboost/test.py0000644000175000017500000000013011140644175013774 0ustar renatorenatofrom test import * from test2 import * d = Derived() print "" d.runTest() testboost/base.hpp0000644000175000017500000000045511140636327014100 0ustar renatorenato#ifndef __BASE_HPP__ #define __BASE_HPP__ #include // Base Class struct Base { void runTestBase() { testVirtual(); } protected: void testProtected() { printf("protected\n"); } virtual void testVirtual() { printf("virtual\n"); } }; #endif testboost/Makefile0000644000175000017500000000032011140646335014104 0ustar renatorenatoall: test.cpp test2.cpp g++ test.cpp -I/usr/include/python2.5 -lpython2.5 -lboost_python-py25 -o test.so -shared g++ test2.cpp -I/usr/include/python2.5 -lpython2.5 -lboost_python-py25 -o test2.so -shared testboost/proxy.hpp0000644000175000017500000000037611140646401014343 0ustar renatorenato#ifndef __PROXY_HPP__ #define __PROXY_HPP__ // Base proxy this class is used to access protected // and virtual methods from base classe struct BaseProxy { virtual void testProtected_proxy() = 0; virtual void testVirtual_proxy() = 0; }; #endif testboost/test2.cpp0000644000175000017500000000204611140646307014217 0ustar renatorenato#include #include #include "base.hpp" #include "proxy.hpp" using namespace boost::python; //Derived class struct Derived: public Base { Derived(){} void runTest() { testVirtual(); } void testVirtual() { printf("Derived::testVirtual\n"); } }; // Wrapper class of a derived class struct DerivedWrap : Derived, wrapper, BaseProxy { void testVirtual() { if (override func__ = this->get_override("testVirtual")) { func__(); } else Derived::testVirtual(); } //reimplement BaseProxy methods to enable access from other class void testVirtual_proxy() { this->Derived::testVirtual(); } void testProtected_proxy() { this->testProtected(); } }; // Module test2 BOOST_PYTHON_MODULE(test2) { class_<derivedwrap, bases<base="">, boost::noncopyable>("Derived") .def("runTest", &Derived::runTest) ; } testboost/test.cpp0000644000175000017500000000300611140662622014130 0ustar renatorenato#include #include #include "base.hpp" #include "proxy.hpp" using namespace boost::python; //Wrap from my Base class struct BaseWrap : Base, wrapper, BaseProxy { void testVirtual() { if (override func__ = this->get_override("testVirtual")) func__(); else Base::testVirtual(); } void testVirtual_proxy() { this->Base::testVirtual(); } void testProtected_proxy() { this->testProtected(); } }; //this function will be exported to accept all derived classes static void testVirtual_default(Base *self) { //this cast not work when called from another python module (test2) //BaseProxy *proxy = dynamic_cast<baseproxy*>(self); BaseProxy *proxy = (BaseProxy*)self; assert(proxy); proxy->testVirtual_proxy(); } //this function will be exported to accept all derived classes static void testProtected_default(Base *self) { //this cast not work when called from another python module (test2) BaseProxy *proxy = dynamic_cast<baseproxy*>(self); assert(proxy); proxy->testProtected_proxy(); } // Module BOOST_PYTHON_MODULE(test) { class_<basewrap, boost::noncopyable="">("Base") //export default function witch accept all derived class and //avoid export the same function in all derived class .def("testProtected", testProtected_default) .def("testVirtual", testVirtual_default) ; } </basewrap,></baseproxy*></baseproxy*></derivedwrap,>