Salome HOME
Merge branch 'Dev_0.6' of newgeom:newgeom into Dev_0.6
[modules/shaper.git] / src / Events / Events_Loop.h
1 // File:        Events_Loop.hxx
2 // Created:     Thu Mar 13 2014
3 // Author:      Mikhail PONIKAROV
4
5 #ifndef Events_Loop_H_
6 #define Events_Loop_H_
7
8 #include <Events_Message.h>
9 #include <Events_Listener.h>
10
11 #include <map>
12 #include <set>
13 #include <list>
14
15 class Events_MessageGroup;
16
17 /**\class Events_Lopp
18  * \ingroup EventsLoop
19  * \brief Base class that manages the receiving and sending of all
20  * not Qt-events in the application.
21  *
22  * One per application, initialized on start. Listeners must register in this loop
23  * to get events, called by senders. Sending of events is very fast (just adding to container).
24  * Performing of events is processed in separated thread, so, sender takes 
25  * control back immideately.
26  */
27 class Events_Loop
28 {
29   /// map from event ID to sender pointer to listeners that must be called for this
30   std::map<char*, std::map<void*, std::list<Events_Listener*> > > myListeners;
31
32   /// map from event ID to listeners which must process message without waiting for flush
33   std::map<char*, Events_Listener*> myImmediateListeners;
34
35   /// map from event ID to groupped messages (accumulated on flush)
36   std::map<char*, std::shared_ptr<Events_Message> > myGroups;
37
38   ///< set of messages that are flushed right now, so they are not grouped
39   std::set<char*> myFlushed;
40
41   /// to process flushes or not
42   bool myFlushActive;
43
44   //! The empty constructor, will be called at startup of the application, only once
45   Events_Loop() : myFlushActive(true) {}
46
47  public:
48   ///! Returns the main object of the loop, one per application.
49   EVENTS_EXPORT static Events_Loop* loop();
50   //! Returns the unique event by the given name. Call this method only on initialization of object
51   //! to speedup the events processing without parsing of the string.
52   EVENTS_EXPORT static Events_ID eventByName(const char* theName);
53
54   //! Allows to send an event
55   //! \param isGroup is true for grouping messages if possible
56   EVENTS_EXPORT void send(const std::shared_ptr<Events_Message>& theMessage, bool isGroup = true);
57
58   //! Registers (or adds if such listener is already registered) a listener 
59   //! that will be called on the event and from the defined sender
60   EVENTS_EXPORT void registerListener(Events_Listener* theListener, const Events_ID theID,
61                                       void* theSender = 0, bool theImmediate = false);
62
63   //! Initializes sending of a group-message by the given ID
64   EVENTS_EXPORT void flush(const Events_ID& theID);
65
66   //! Allows to disable flushes: needed in synchronization of document mechanism 
67   //! (to synchronize all and only then flush create, update, etc in correct order)
68   EVENTS_EXPORT void activateFlushes(const bool theActivate);
69
70   //! Clears all collected messages
71   EVENTS_EXPORT void clear(const Events_ID& theID);
72
73   //! Enables flush without grouping for the given message
74   EVENTS_EXPORT void autoFlush(const Events_ID& theID, const bool theAuto = true);
75
76   //! Returns true if the evement is flushed right now
77   EVENTS_EXPORT bool isFlushed(const Events_ID& theID);
78   //! Sets the flag that the event is flished right now
79   EVENTS_EXPORT void setFlushed(const Events_ID& theID, const bool theValue);
80 };
81
82 #endif