Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PythonWeakPtrProblems.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "PythonEmbedding.hpp"
- #include <boost/python.hpp>
- #include <boost/shared_ptr.hpp>
- #include <iostream>
- using namespace boost;
- using namespace boost::python;
- using namespace std;
- CImplementation::CImplementation(int message)
- {
- this->message = message;
- }
- int CImplementation::GetMessage()
- {
- return message;
- }
- void CImplementation::Communicate(boost::shared_ptr<Communicatable> destination)
- {
- destination->Listen(shared_from_this());
- }
- void CImplementation::Listen(boost::shared_ptr<Communicatable> originator)
- {
- cout << "CImplementation: Got " << originator->GetMessage() << endl;
- }
- int CommunicatableWrapper::GetMessage()
- {
- return this->get_override("GetMessage")();
- }
- void CommunicatableWrapper::Communicate(boost::shared_ptr<Communicatable> destination)
- {
- this->get_override("Communicate")(destination);
- }
- void CommunicatableWrapper::Listen(boost::shared_ptr<Communicatable> originator)
- {
- this->get_override("Listen")(originator);
- }
- BOOST_PYTHON_MODULE(experiments)
- {
- class_<CommunicatableWrapper, boost::shared_ptr<CommunicatableWrapper>, boost::noncopyable>("Communicatable")
- .def("Communicate", pure_virtual(&Communicatable::Communicate))
- .def("Listen", pure_virtual(&Communicatable::Listen))
- .def("GetMessage", pure_virtual(&Communicatable::GetMessage))
- ;
- class_<CImplementation, boost::shared_ptr< CImplementation >, bases<Communicatable> >("CImplementation", init<int>())
- ;}
- int _tmain(int argc, char** argv)
- {
- Py_Initialize();
- initexperiments();
- // Load up the auto execution script and then open up the command prompt
- object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));
- object main_namespace = main_module.attr("__dict__");
- try
- {
- handle<> ignored(( PyRun_String( "execfile('Experiments.py')",
- Py_file_input,
- main_namespace.ptr(),
- main_namespace.ptr() ) ));
- }
- catch(...)
- {
- boost::python::handle_exception();
- }
- Py_Main(argc, argv);
- Py_Finalize(); // According to old Boost documentation, we don't want to do this.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement