Advertisement
bueddl

Untitled

Nov 3rd, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1.  
  2. class base_module {
  3. public:
  4.  
  5.     // Will be invoked in dedicated thread
  6.     void main()
  7.     {
  8.         m_running = true;
  9.         while (m_running)
  10.         {
  11.             // Get message from priority queue
  12.             auto message = m_queue.pop_front();
  13.  
  14.             // Dispatch
  15.             switch (message.type()) {
  16.                 case message_type::EVENT:
  17.                     // Find event handler and invoke
  18.                     event &ev = message;
  19.                     if ((auto handler = m_event_handlers.find(ev)) == m_event_handlers.end())
  20.                         // Silently drop
  21.                         continue;
  22.  
  23.                     // Wrap call to leave number of arguments at event side! (dynamic argument lists possible)
  24.                     auto call_wrapper = ev.wrap(handler);
  25.                     call_wrapper(); // This will invoke handler() with arguments given by ev
  26.                 break;
  27.  
  28.                 case message_type::RESPONSE:
  29.                     // There should be a pending request, find it and call the completion handler
  30.                     response &res = message;
  31.                     if ((auto handler = m_pending_requests.find(response.id)) == m_pending_requests.end())
  32.                         throw system::fatal;
  33.  
  34.                     // Again, use a call wrapper
  35.                     auto call_wrapper = res.wrap(handler);
  36.                     call_wrapper(); // This will invoke handler() with arguments given by res
  37.                 break;
  38.  
  39.                 case message_type::REQUEST:
  40.                     // A request has been made to an exported method, invoke handler for request
  41.                     // ...TODO...
  42.                 break;
  43.             }
  44.         }
  45.     }
  46.  
  47.     void terminate()
  48.     {
  49.         m_running = false;
  50.     }
  51.  
  52.     // Add message
  53.     void deliver_message(message &msg)
  54.     {
  55.         m_queue.push_back(msg);
  56.     }
  57.  
  58. private:
  59.     bool m_running;
  60.     priority_queue m_queue;
  61. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement