Salome HOME
4e3c0bf2b8c7deac0c8f5ce33fbd2b7b348bb9e8
[modules/shaper.git] / src / Events / Events_Loop.h
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef Events_Loop_H_
21 #define Events_Loop_H_
22
23 #include <Events_Message.h>
24 #include <Events_Listener.h>
25
26 #include <map>
27 #include <set>
28 #include <list>
29
30 class Events_MessageGroup;
31
32 /**\class Events_Loop
33  * \ingroup EventsLoop
34  * \brief Base class that manages the receiving and sending of all
35  * not Qt-events in the application.
36  *
37  * One per application, initialized on start. Listeners must register in this loop
38  * to get events, called by senders. Sending of events is very fast (just adding to container).
39  * Performing of events is processed in separated thread, so, sender takes 
40  * control back immideately.
41  */
42 class Events_Loop
43 {
44   /// map from event ID to sender pointer to listeners that must be called for this
45   std::map<char*, std::map<void*, std::list<Events_Listener*> > > myListeners;
46
47   /// map from event ID to listeners which must process message without waiting for flush
48   std::map<char*, Events_Listener*> myImmediateListeners;
49
50   /// map from event ID to groupped messages (accumulated for flush)
51   std::map<char*, std::shared_ptr<Events_Message> > myGroups;
52
53   ///< set of messages that are flushed right now, so they are not grouped
54   std::set<char*> myFlushed;
55
56   /// to process flushes or not
57   bool myFlushActive;
58
59   //! The empty constructor, will be called at startup of the application, only once
60   Events_Loop() : myFlushActive(true) {}
61
62  public:
63   ///! Returns the main object of the loop, one per application.
64   EVENTS_EXPORT static Events_Loop* loop();
65   //! Returns the unique event by the given name. Call this method only on initialization of object
66   //! to speedup the events processing without parsing of the string.
67   EVENTS_EXPORT static Events_ID eventByName(const char* theName);
68
69   //! Allows to send an event
70   //! \param theMessage the enevt message to send
71   //! \param isGroup is true for grouping messages if possible
72   EVENTS_EXPORT void send(const std::shared_ptr<Events_Message>& theMessage, bool isGroup = true);
73
74   //! Registers (or adds if such listener is already registered) a listener
75   //! that will be called on the event and from the defined sender
76   //! \param theListener the object that will listen (process) the event
77   //! \param theID listen for messages with this ID
78   //! \param theSender listen only for this sender (NULL - listen everybody)
79   //! \param theImmediate for listeners who can not wait (no groupping mechanism is used for it)
80   EVENTS_EXPORT void registerListener(Events_Listener* theListener, const Events_ID theID,
81     void* theSender = 0, bool theImmediate = false);
82
83   //! Remove the listener from internal maps if it was registered there
84   //! \param theListener a listener
85   EVENTS_EXPORT void removeListener(Events_Listener* theListener);
86
87   //! Initializes sending of a group-message by the given ID
88   EVENTS_EXPORT void flush(const Events_ID& theID);
89
90   //! Removes messages with the given ID: they are not needed anymore (UPDATE on close)
91   EVENTS_EXPORT void eraseMessages(const Events_ID& theID);
92
93   //! Allows to disable flushes: needed in synchronization of document mechanism
94   //! (to synchronize all and only then flush create, update, etc in correct order)
95   //! \param theActivate a state about flushe is active. If false, the flush is disabled
96   //! \return the previous active flush state
97   EVENTS_EXPORT bool activateFlushes(const bool theActivate);
98
99   //! Clears all collected messages
100   EVENTS_EXPORT void clear(const Events_ID& theID);
101
102   //! Returns true if the evement is flushed right now
103   EVENTS_EXPORT bool isFlushed(const Events_ID& theID);
104   //! Sets the flag that the event is flished right now
105   EVENTS_EXPORT void setFlushed(const Events_ID& theID, const bool theValue);
106
107   //! Returns true if a loop accumulated events to be flashed
108   EVENTS_EXPORT bool hasGrouppedEvent(const Events_ID& theID);
109
110 private:
111   //! Calls "processEvent" for the given listeners.
112   //! If theFlushedNow for grouped listeners is stores message in listeners.
113   void sendProcessEvent(const std::shared_ptr<Events_Message>& theMessage,
114     std::list<Events_Listener*>& theListeners, const bool theFlushedNow);
115
116 };
117
118 #endif