Advertisement
Guest User

PythonExperiments.cpp

a guest
Feb 4th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. // PythonWeakPtrProblems.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "PythonEmbedding.hpp"
  6. #include <boost/python.hpp>
  7. #include <boost/shared_ptr.hpp>
  8. #include <iostream>
  9.  
  10. using namespace boost;
  11. using namespace boost::python;
  12. using namespace std;
  13.  
  14.  
  15. CImplementation::CImplementation(int message)
  16. {
  17.     this->message = message;
  18. }
  19.  
  20. int CImplementation::GetMessage()
  21. {
  22.     return message;
  23. }
  24.  
  25. void CImplementation::Communicate(boost::shared_ptr<Communicatable> destination)
  26. {
  27.     destination->Listen(shared_from_this());
  28. }
  29.  
  30. void CImplementation::Listen(boost::shared_ptr<Communicatable> originator)
  31. {
  32.     cout << "CImplementation: Got " << originator->GetMessage() << endl;
  33. }
  34.  
  35. int CommunicatableWrapper::GetMessage()
  36. {
  37.     return this->get_override("GetMessage")();
  38. }
  39.  
  40. void CommunicatableWrapper::Communicate(boost::shared_ptr<Communicatable> destination)
  41. {
  42.     this->get_override("Communicate")(destination);
  43. }
  44.  
  45. void CommunicatableWrapper::Listen(boost::shared_ptr<Communicatable> originator)
  46. {
  47.     this->get_override("Listen")(originator);
  48. }
  49.  
  50. BOOST_PYTHON_MODULE(experiments)
  51. {
  52.     class_<CommunicatableWrapper, boost::shared_ptr<CommunicatableWrapper>, boost::noncopyable>("Communicatable")
  53.         .def("Communicate", pure_virtual(&Communicatable::Communicate))
  54.         .def("Listen", pure_virtual(&Communicatable::Listen))
  55.         .def("GetMessage", pure_virtual(&Communicatable::GetMessage))
  56.         ;
  57.  
  58.         class_<CImplementation, boost::shared_ptr< CImplementation >, bases<Communicatable> >("CImplementation", init<int>())
  59.             ;}
  60.  
  61. int _tmain(int argc, char** argv)
  62. {
  63.     Py_Initialize();
  64.     initexperiments();
  65.  
  66.     // Load up the auto execution script and then open up the command prompt
  67.     object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));
  68.     object main_namespace = main_module.attr("__dict__");
  69.     try
  70.     {
  71.     handle<> ignored(( PyRun_String( "execfile('Experiments.py')",
  72.                                         Py_file_input,
  73.                                         main_namespace.ptr(),
  74.                                         main_namespace.ptr() ) ));
  75.     }
  76.     catch(...)
  77.     {
  78.     boost::python::handle_exception();
  79.     }
  80.  
  81.     Py_Main(argc, argv);
  82.  
  83.     Py_Finalize();   // According to old Boost documentation, we don't want to do this.
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement