Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

StacklessPythonCppInterop.cpp

By: a guest on Mar 13th, 2012  |  syntax: C++  |  size: 1.52 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // StacklessPythonCppInterop.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <boost/python.hpp>
  6. #include "TimeBomb.hpp"
  7.  
  8. using namespace boost;
  9. using namespace boost::python;
  10.  
  11. BOOST_PYTHON_MODULE(kaboom)
  12. {
  13.         class_<ITimeBombCallbackWrapper, boost::shared_ptr<ITimeBombCallbackWrapper>, boost::noncopyable>("ITimeBombCallback")
  14.             .def("ReceiveExplosion", pure_virtual(&ITimeBombCallback::ReceiveExplosion))
  15.             ;
  16.  
  17.         class_<TimeBomb>("TimeBomb", init<ITimeBombCallback*>())
  18.                 .def("Go", &TimeBomb::Go)
  19.                 .def("Join", &TimeBomb::Join)
  20.                 ;
  21.                
  22. }
  23.  
  24. int main(int argc, char** argv)
  25. {
  26.         Py_Initialize();
  27.  
  28.         initkaboom();           // Initializes the Boost.Python module I defined.
  29.  
  30.         // Load up the auto execution script and then open up the command prompt
  31.         object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));
  32.         object main_namespace = main_module.attr("__dict__");
  33.         try
  34.         {
  35.                 handle<> ignored(( PyRun_String( "execfile('test_script.py')",
  36.                                        Py_file_input,
  37.                                        main_namespace.ptr(),
  38.                                        main_namespace.ptr() ) ));
  39.         }
  40.         catch(...)
  41.         {
  42.                 // If an exception was thrown, translate it to Python.  Sometimes
  43.                 // this even works!
  44.                 // When there are problems with the script up front, I just get
  45.                 // flung to the Python command line.  That's on my list of things
  46.                 // to improve...
  47.                 boost::python::handle_exception();
  48.         }
  49.  
  50.         Py_Main(argc, argv);
  51.  
  52.         return 0;
  53. }
clone this paste RAW Paste Data