Salome HOME
Fix for the unit tests due to names changing in issue #2375
[modules/shaper.git] / src / Events / Events_Loop.h
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef Events_Loop_H_
22 #define Events_Loop_H_
23
24 #include <Events_Message.h>
25 #include <Events_Listener.h>
26
27 #include <map>
28 #include <set>
29 #include <list>
30
31 class Events_MessageGroup;
32
33 /**\class Events_Loop
34  * \ingroup EventsLoop
35  * \brief Base class that manages the receiving and sending of all
36  * not Qt-events in the application.
37  *
38  * One per application, initialized on start. Listeners must register in this loop
39  * to get events, called by senders. Sending of events is very fast (just adding to container).
40  * Performing of events is processed in separated thread, so, sender takes 
41  * control back immideately.
42  */
43 class Events_Loop
44 {
45   /// map from event ID to sender pointer to listeners that must be called for this
46   std::map<char*, std::map<void*, std::list<Events_Listener*> > > myListeners;
47
48   /// map from event ID to listeners which must process message without waiting for flush
49   std::map<char*, Events_Listener*> myImmediateListeners;
50
51   /// map from event ID to groupped messages (accumulated for flush)
52   std::map<char*, std::shared_ptr<Events_Message> > myGroups;
53
54   ///< set of messages that are flushed right now, so they are not grouped
55   std::set<char*> myFlushed;
56
57   /// to process flushes or not
58   bool myFlushActive;
59
60   //! The empty constructor, will be called at startup of the application, only once
61   Events_Loop() : myFlushActive(true) {}
62
63  public:
64   ///! Returns the main object of the loop, one per application.
65   EVENTS_EXPORT static Events_Loop* loop();
66   //! Returns the unique event by the given name. Call this method only on initialization of object
67   //! to speedup the events processing without parsing of the string.
68   EVENTS_EXPORT static Events_ID eventByName(const char* theName);
69
70   //! Allows to send an event
71   //! \param theMessage the enevt message to send
72   //! \param isGroup is true for grouping messages if possible
73   EVENTS_EXPORT void send(const std::shared_ptr<Events_Message>& theMessage, bool isGroup = true);
74
75   //! Registers (or adds if such listener is already registered) a listener
76   //! that will be called on the event and from the defined sender
77   //! \param theListener the object that will listen (process) the event
78   //! \param theID listen for messages with this ID
79   //! \param theSender listen only for this sender (NULL - listen everybody)
80   //! \param theImmediate for listeners who can not wait (no groupping mechanism is used for it)
81   EVENTS_EXPORT void registerListener(Events_Listener* theListener, const Events_ID theID,
82     void* theSender = 0, bool theImmediate = false);
83
84   //! Remove the listener from internal maps if it was registered there
85   //! \param theListener a listener
86   EVENTS_EXPORT void removeListener(Events_Listener* theListener);
87
88   //! Initializes sending of a group-message by the given ID
89   EVENTS_EXPORT void flush(const Events_ID& theID);
90
91   //! Removes messages with the given ID: they are not needed anymore (UPDATE on close)
92   EVENTS_EXPORT void eraseMessages(const Events_ID& theID);
93
94   //! Allows to disable flushes: needed in synchronization of document mechanism
95   //! (to synchronize all and only then flush create, update, etc in correct order)
96   //! \param theActivate a state about flushe is active. If false, the flush is disabled
97   //! \return the previous active flush state
98   EVENTS_EXPORT bool activateFlushes(const bool theActivate);
99
100   //! Clears all collected messages
101   EVENTS_EXPORT void clear(const Events_ID& theID);
102
103   //! Returns true if the evement is flushed right now
104   EVENTS_EXPORT bool isFlushed(const Events_ID& theID);
105   //! Sets the flag that the event is flished right now
106   EVENTS_EXPORT void setFlushed(const Events_ID& theID, const bool theValue);
107
108   //! Returns true if a loop accumulated events to be flashed
109   EVENTS_EXPORT bool hasGrouppedEvent(const Events_ID& theID);
110
111 private:
112   //! Calls "processEvent" for the given listeners.
113   //! If theFlushedNow for grouped listeners is stores message in listeners.
114   void sendProcessEvent(const std::shared_ptr<Events_Message>& theMessage,
115     std::list<Events_Listener*>& theListeners, const bool theFlushedNow);
116
117 };
118
119 #endif