Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / Events / Events_Loop.cpp
1 // File:        Events_Loop.hxx
2 // Created:     Thu Mar 13 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Events_Loop.h>
6 #include <Events_MessageGroup.h>
7
8 #include <string>
9 #include <cstring>
10
11 using namespace std;
12
13 Events_Loop* Events_Loop::loop()
14 {
15   // initialized on initialization of the application
16   static Events_Loop MAIN_LOOP;
17   return &MAIN_LOOP;
18 }
19
20 Events_ID Events_Loop::eventByName(const char* theName)
21 {
22   ///! All events created in this session, uniquely identified by the text and char pointer
23   static map<string, char*> CREATED_EVENTS;
24   char* aResult;
25   string aName(theName);
26   map<string, char*>::iterator aFound = CREATED_EVENTS.find(aName);
27   if (aFound == CREATED_EVENTS.end()) {  //not created yet
28 #ifdef WIN32
29     aResult = _strdup(theName);  // copy to make unique internal pointer
30 #else
31     aResult = strdup(theName);  // copy to make unique internal pointer
32 #endif
33     CREATED_EVENTS[aName] = aResult;
34   } else
35     aResult = aFound->second;
36
37   return Events_ID(aResult);
38 }
39
40 void Events_Loop::send(Events_Message& theMessage, bool isGroup)
41 {
42   // if it is grouped message, just accumulate it
43   if (isGroup) {
44     Events_MessageGroup* aGroup = dynamic_cast<Events_MessageGroup*>(&theMessage);
45     if (aGroup) {
46       std::map<char*, Events_MessageGroup*>::iterator aMyGroup = myGroups.find(
47           aGroup->eventID().eventText());
48       if (aMyGroup == myGroups.end()) {  // create a new group of messages for accumulation
49         myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
50         aMyGroup = myGroups.find(aGroup->eventID().eventText());
51       }
52       aMyGroup->second->Join(*aGroup);
53       return;
54     }
55   }
56
57   // TO DO: make it in thread and with usage of semaphores
58
59   map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
60       theMessage.eventID().eventText());
61   if (aFindID != myListeners.end()) {
62     map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
63         theMessage.sender());
64     if (aFindSender != aFindID->second.end()) {
65       list<Events_Listener*>& aListeners = aFindSender->second;
66       for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
67         (*aL)->processEvent(&theMessage);
68     }
69     if (theMessage.sender()) {  // also call for NULL senders registered
70       aFindSender = aFindID->second.find(NULL);
71       if (aFindSender != aFindID->second.end()) {
72         list<Events_Listener*>& aListeners = aFindSender->second;
73         for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
74           (*aL)->processEvent(&theMessage);
75       }
76     }
77   }
78 }
79
80 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
81                                    void* theSender)
82 {
83   map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
84       theID.eventText());
85   if (aFindID == myListeners.end()) {  // create container associated with ID
86     myListeners[theID.eventText()] = map<void*, list<Events_Listener*> >();
87     aFindID = myListeners.find(theID.eventText());
88   }
89
90   map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(theSender);
91   if (aFindSender == aFindID->second.end()) {  // create container associated with sender
92     aFindID->second[theSender] = list<Events_Listener*>();
93     aFindSender = aFindID->second.find(theSender);
94   }
95   // check that listener was not registered wit hsuch parameters before
96   list<Events_Listener*>& aListeners = aFindSender->second;
97   for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
98     if (*aL == theListener)
99       return;  // avoid duplicates
100
101   aListeners.push_back(theListener);
102 }
103
104 void Events_Loop::flush(const Events_ID& theID)
105 {
106   if (!myFlushActive)
107     return;
108   std::map<char*, Events_MessageGroup*>::iterator aMyGroup = myGroups.find(theID.eventText());
109   if (aMyGroup != myGroups.end()) {  // really sends
110     Events_MessageGroup* aGroup = aMyGroup->second;
111     myGroups.erase(aMyGroup);
112     send(*aGroup, false);
113     delete aGroup;
114   }
115 }
116
117 void Events_Loop::activateFlushes(const bool theActivate)
118 {
119   myFlushActive = theActivate;
120 }