]> git.vomp.tv Git - vompclient.git/blob - messagequeue.h
Fix VRecording showing graphic at bottom left when it shouldn't
[vompclient.git] / messagequeue.h
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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/>.
18 */
19
20 #ifndef MESSAGEQUEUE_H
21 #define MESSAGEQUEUE_H
22
23 #include <deque>
24 #include <list>
25 #include <mutex>
26 #include <condition_variable>
27
28 #include "message.h" // Allow user classes just to include messagequeue.h
29
30 class LogNT;
31
32 typedef std::deque<Message*> MQueue;
33 typedef std::deque<Message*>::iterator MQueueI;
34
35 // Implement this in any class wanting to receive Messages
36 class MessageReceiver
37 {
38   public:
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().
45 };
46
47 typedef std::list<MessageReceiver*> Receivers;
48 typedef std::list<MessageReceiver*>::iterator ReceiversI;
49
50 class MessageQueue
51 {
52   public:
53     MessageQueue();
54     virtual ~MessageQueue();
55     static MessageQueue* getInstance();
56
57     void addReceiver(MessageReceiver* newReceiver);
58     void removeReceiver(MessageReceiver* toRemove);
59
60     virtual void postMessage(Message* m);
61
62   protected:
63     void messageLoop();
64     bool messageLoopRun{};
65     virtual void flushMessageQueue();
66     virtual void dispatchMessage(Message* m)=0; // User class must implement and handle the message
67
68     bool receiverExists(MessageReceiver*);
69
70     MQueue messages;
71
72     Receivers receivers;
73
74     std::mutex messageQueueMutex;
75   private:
76     static MessageQueue* instance;
77     LogNT* logger{};
78
79     Message* messageBeingProcessed{};
80     std::condition_variable messageQueueCond;
81 };
82
83 #endif