Advertisement
LeonidPodosinnikov

events.h

Mar 4th, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. class eventBase {
  2. public:
  3.   using eventHandler_t = std::function<void()>; /* для примера указал такой тип */
  4.   eventBase();
  5.   virtual ~eventBase();
  6.   void setHandler(eventHandler_t h);
  7.   void lockHandler();
  8.   void unlockHandler();
  9.   bool handlerLocked() const;
  10.   bool update() { /* do something */ return this->run(); };
  11.   int id() const;
  12. protected:
  13.   void runHandler();            /* call python function in python space */
  14. private:
  15.   eventHandler_t handler;       /* pointer to python function */
  16.   bool handlerLock = false;
  17.   virtual bool run() = 0;       /* event function */
  18.   int _id = 0;
  19. };
  20.  
  21. class eventChanged : public eventBase {
  22. public:
  23.   eventChanged(qc::Channel* ch);
  24.   virtual ~eventChanged();
  25. private:
  26.   qc::Channel* _channel;
  27.   virtual bool run() override;  /* true if value of channel has changed */
  28. };
  29.  
  30. class eventPython : public eventBase {
  31. public:
  32.   using eventPythonFunc_t = std::function<bool()>; /* тоже для примера */
  33.   eventPython();
  34.   eventPython(eventPythonFunc_t);
  35.   virtual ~eventPython();
  36.   void setFunc(eventPythonFunc_t f) { evFunc = f; }
  37. private:
  38.   eventPythonFunc_t evFunc;
  39.   virtual bool run() override;  /* run python event function, return true if event ocurred */
  40. };
  41.  
  42. /* events collection. */
  43. class eventsFld {
  44. public:
  45.   friend eventBase;
  46.   static void update() { for (auto ev : events) ev->update(); }
  47.   static auto size() { return events.size(); }
  48. private:
  49.   static std::list<eventBase*> events;
  50.   static int lastid;
  51.   static int  add(eventBase*);     /* use in eventBase::eventBase() */
  52.   static bool remove(eventBase*);  /* use in eventBase::~eventBase() */
  53. };
  54.  
  55. class scenario {
  56. public:
  57.   scenario(const std::string& );
  58.   void init() {
  59.     try {
  60.       /* run pyInit(): */
  61.       /*   make new events and bind handlers */
  62.     } catch (...) {
  63.  
  64.     }
  65.   }
  66. private:
  67.   std::string pyScript;
  68. };
  69.  
  70. /* далее создаем сценарий  */
  71. /* нинциализируем его */
  72. /* а в рабочем цикле вызываем eventsFld::update() - обновляем все события */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement