Salome HOME
Move Events messages to boost pointers: partially done
[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 <list>
13
14 class Events_MessageGroup;
15
16 /**\class Events_Lopp
17  * \ingroup EventsLoop
18  * \brief Base class that manages the receiving and sending of all
19  * not Qt-events in the application.
20  *
21  * One per application, initialized on start. Listeners must register in this loop
22  * to get events, called by senders. Sending of events is very fast (just adding to container).
23  * Performing of events is processed in separated thread, so, sender takes 
24  * control back immideately.
25  */
26 class Events_Loop
27 {
28   /// map from event ID to sender pointer to listeners that must be called for this
29   std::map<char*, std::map<void*, std::list<Events_Listener*> > > myListeners;
30
31   /// map from event ID to groupped messages (accumulated on flush)
32   std::map<char*, boost::shared_ptr<Events_Message> > myGroups;
33
34   /// to process flushes or not
35   bool myFlushActive;
36
37   //! The empty constructor, will be called at startup of the application, only once
38   Events_Loop() : myFlushActive(true) {}
39
40  public:
41   ///! Returns the main object of the loop, one per application.
42   EVENTS_EXPORT static Events_Loop* loop();
43   //! Returns the unique event by the given name. Call this method only on initialization of object
44   //! to speedup the events processing without parsing of the string.
45   EVENTS_EXPORT static Events_ID eventByName(const char* theName);
46
47   //! Allows to send an event
48   //! \param isGroup is true for grouping messages if possible
49   EVENTS_EXPORT void send(const boost::shared_ptr<Events_Message>& theMessage, bool isGroup = true);
50
51   //! Registers (or adds if such listener is already registered) a listener 
52   //! that will be called on the event and from the defined sender
53   EVENTS_EXPORT void registerListener(Events_Listener* theListener, const Events_ID theID,
54                                       void* theSender = 0);
55
56   //! Initializes sending of a group-message by the given ID
57   EVENTS_EXPORT void flush(const Events_ID& theID);
58
59   //! Allows to disable flushes: needed in synchronization of document mechanism 
60   //! (to synchronize all and only then flush create, update, etc in correct order)
61   EVENTS_EXPORT void activateFlushes(const bool theActivate);
62 };
63
64 #endif