2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
20 #ifndef MESSAGEQUEUE_H
21 #define MESSAGEQUEUE_H
26 #include <condition_variable>
28 #include "message.h" // Allow user classes just to include messagequeue.h
32 typedef std::deque<Message*> MQueue;
33 typedef std::deque<Message*>::iterator MQueueI;
35 // Implement this in any class wanting to receive Messages
39 virtual void processMessage(Message* m)=0;
40 // The implementing class must not call MessageQueue::removeReceiver() from within processMessage()
41 // Also, implementing class must be aware of not causing itself to be deleted during processMessage
42 // such as generating an input event which causes BoxStack to remove/delete the object
43 // This will cause the object's destructor to call removeReceiver()
44 // If in doubt, generate an input that goes in the MessageQueue, e.g. Input::sendInputKey().
47 typedef std::list<MessageReceiver*> Receivers;
48 typedef std::list<MessageReceiver*>::iterator ReceiversI;
54 virtual ~MessageQueue();
55 static MessageQueue* getInstance();
57 void addReceiver(MessageReceiver* newReceiver);
58 void removeReceiver(MessageReceiver* toRemove);
60 virtual void postMessage(Message* m);
64 bool messageLoopRun{};
65 virtual void flushMessageQueue();
66 virtual void dispatchMessage(Message* m)=0; // User class must implement and handle the message
68 bool receiverExists(MessageReceiver*);
74 std::mutex messageQueueMutex;
76 static MessageQueue* instance;
79 Message* messageBeingProcessed{};
80 std::condition_variable messageQueueCond;