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

TimeBomb.hpp

By: a guest on Mar 13th, 2012  |  syntax: C++  |  size: 1.13 KB  |  views: 34  |  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. #ifndef TIMEBOMB_HPP_
  2. #define TIMEBOMB_HPP_
  3.  
  4. #include <boost/thread.hpp>
  5. #include <boost/python.hpp>
  6.  
  7. using namespace boost;
  8. using namespace boost::python;
  9.  
  10. // The interface to implement in Python.  This will get
  11. // called from a thread in the C++ runtime.
  12. class ITimeBombCallback
  13. {
  14. public:
  15.         virtual ~ITimeBombCallback() { }
  16.         virtual void ReceiveExplosion() = 0;
  17. };
  18.  
  19. // To properly call interfaces implemented in Python, we actually use a specific wrapper.
  20. // Calls to/from Python objects will actually take a trip through this.
  21. class ITimeBombCallbackWrapper : public ITimeBombCallback, public boost::python::wrapper<ITimeBombCallback>
  22. {
  23.         void ReceiveExplosion();
  24. };
  25.  
  26. // Usage:
  27. // 1. Construct with an implementation of ITimeBombCallback (from Python).
  28. // 2. Call Go() to start background thread.
  29. // 3. Wait around 5 seconds for the bomb to go off.
  30. // 4. Call Join() if you're still alive.
  31. class TimeBomb
  32. {
  33. protected:
  34.         boost::thread* backgroundThread;
  35.         ITimeBombCallback* luckyWinner;
  36.         void InnerBody();
  37.  
  38. public:
  39.  
  40.         TimeBomb(ITimeBombCallback* luckyWinner);
  41.         void Go();
  42.         void Join();
  43. };
  44.  
  45.  
  46. #endif // TIMEBOMB_HPP_