1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Events_Loop.h>
22 #include <Events_MessageGroup.h>
27 Events_Loop* Events_Loop::loop()
29 // initialized on initialization of the application
30 static Events_Loop MAIN_LOOP;
34 Events_ID Events_Loop::eventByName(const char* theName)
36 ///! All events created in this session, uniquely identified by the text and char pointer
37 static std::map<std::string, char*> CREATED_EVENTS;
39 std::string aName(theName);
40 std::map<std::string, char*>::iterator aFound = CREATED_EVENTS.find(aName);
41 if (aFound == CREATED_EVENTS.end()) { //not created yet
43 aResult = _strdup(theName); // copy to make unique internal pointer
45 aResult = strdup(theName); // copy to make unique internal pointer
47 CREATED_EVENTS[aName] = aResult;
49 aResult = aFound->second;
51 return Events_ID(aResult);
54 void Events_Loop::sendProcessEvent(const std::shared_ptr<Events_Message>& theMessage,
55 std::list<Events_Listener*>& theListeners, const bool theFlushedNow)
57 std::list<Events_Listener*>::iterator aL = theListeners.begin();
58 for (; aL != theListeners.end(); aL++) {
59 if (theFlushedNow && (*aL)->groupMessages()) {
60 (*aL)->groupWhileFlush(theMessage);
62 (*aL)->processEvent(theMessage);
67 void Events_Loop::send(const std::shared_ptr<Events_Message>& theMessage, bool isGroup)
69 if (myImmediateListeners.find(theMessage->eventID().eventText()) != myImmediateListeners.end()) {
70 myImmediateListeners[theMessage->eventID().eventText()]->processEvent(theMessage);
72 // if it is grouped message, just accumulate it
73 bool isFlushedNow = myFlushed.find(theMessage->eventID().myID) != myFlushed.end();
74 if (isGroup && !isFlushedNow) {
75 std::shared_ptr<Events_MessageGroup> aGroup =
76 std::dynamic_pointer_cast<Events_MessageGroup>(theMessage);
78 std::map<char*, std::shared_ptr<Events_Message> >::iterator aMyGroup = myGroups.find(
79 aGroup->eventID().eventText());
80 if (aMyGroup == myGroups.end()) { // create a new group of messages for accumulation
81 myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
82 aMyGroup = myGroups.find(aGroup->eventID().eventText());
84 std::shared_ptr<Events_MessageGroup> aStored =
85 std::dynamic_pointer_cast<Events_MessageGroup>(aMyGroup->second);
86 aStored->Join(aGroup);
91 std::map<char*, std::map<void*, std::list<Events_Listener*> > >::iterator aFindID =
92 myListeners.find(theMessage->eventID().eventText());
93 if (aFindID != myListeners.end()) {
94 std::map<void*, std::list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
95 theMessage->sender());
96 if (aFindSender != aFindID->second.end()) {
97 sendProcessEvent(theMessage, aFindSender->second, isFlushedNow && isGroup);
99 if (theMessage->sender()) { // also call for NULL senders registered
100 aFindSender = aFindID->second.find(NULL);
101 if (aFindSender != aFindID->second.end()) {
102 sendProcessEvent(theMessage, aFindSender->second, isFlushedNow && isGroup);
108 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
109 void* theSender, bool theImmediate)
111 if (theImmediate) { // just register as an immediate
112 myImmediateListeners[theID.eventText()] = theListener;
115 std::map<char*, std::map<void*, std::list<Events_Listener*> > >::iterator aFindID =
116 myListeners.find(theID.eventText());
117 if (aFindID == myListeners.end()) { // create container associated with ID
118 myListeners[theID.eventText()] = std::map<void*, std::list<Events_Listener*> >();
119 aFindID = myListeners.find(theID.eventText());
122 std::map<void*, std::list<Events_Listener*> >::iterator aFindSender =
123 aFindID->second.find(theSender);
124 if (aFindSender == aFindID->second.end()) { // create container associated with sender
125 aFindID->second[theSender] = std::list<Events_Listener*>();
126 aFindSender = aFindID->second.find(theSender);
128 // check that listener was not registered wit hsuch parameters before
129 std::list<Events_Listener*>& aListeners = aFindSender->second;
130 for (std::list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
131 if (*aL == theListener)
132 return; // avoid duplicates
134 aListeners.push_back(theListener);
137 void Events_Loop::removeListener(Events_Listener* theListener)
139 // remove the listener in myListeners map
140 std::map<char*, std::map<void*, std::list<Events_Listener*> > >::const_reverse_iterator
141 anIt = myListeners.rbegin();
142 while(anIt != myListeners.rend()) {
143 std::map<void*, std::list<Events_Listener*> > aLMap = anIt->second;
144 std::map<void*, std::list<Events_Listener*> >::const_reverse_iterator aLIt = aLMap.rbegin();
145 while (aLIt != aLMap.rend()) {
146 std::list<Events_Listener*> aListeners = aLIt->second;
147 std::list<Events_Listener*>::const_reverse_iterator aLsIt = aListeners.rbegin();
148 for (; aLsIt != aListeners.rend(); aLsIt++) {
149 if (*aLsIt == theListener) {
150 aListeners.remove(theListener);
151 aLMap[aLIt->first] = aListeners;
152 myListeners[anIt->first] = aLMap;
156 if (aListeners.empty()) {
157 aLMap.erase(aLIt->first);
158 myListeners[anIt->first] = aLMap;
160 break; // avoid incrementation of the iterator if the the container is empty
164 if (anIt->second.empty()) {
165 myListeners.erase(anIt->first);
166 if (myListeners.empty())
167 break; // avoid incrementation of the iterator if the the container is empty
172 // remove the listener in myImmediateListeners map
173 std::map<char*, Events_Listener*>::const_reverse_iterator anImIt = myImmediateListeners.rbegin();
174 while(anImIt != myImmediateListeners.rend()) {
175 if (anImIt->second == theListener) {
176 myImmediateListeners.erase(anImIt->first);
177 if (myImmediateListeners.empty())
178 break; // avoid incrementation of the iterator if the the container is empty
184 void Events_Loop::flush(const Events_ID& theID)
188 bool hasEventsToFlush = !myGroups.empty();
189 std::map<char*, std::shared_ptr<Events_Message> >::iterator aMyGroup;
190 for(aMyGroup = myGroups.find(theID.eventText());
191 aMyGroup != myGroups.end(); aMyGroup = myGroups.find(theID.eventText()))
193 bool aWasFlushed = myFlushed.find(theID.myID) != myFlushed.end();
195 myFlushed.insert(theID.myID);
196 std::shared_ptr<Events_Message> aGroup = aMyGroup->second;
197 myGroups.erase(aMyGroup);
201 // TODO: Stabilization fix. Check later.
202 if(myFlushed.find(theID.myID) != myFlushed.end()) {
203 myFlushed.erase(myFlushed.find(theID.myID));
205 bool aProblem = true;
208 // send accumulated messages to "groupListeners"
209 std::map<char*, std::map<void*, std::list<Events_Listener*> > >::iterator aFindID =
210 myListeners.find(theID.eventText());
211 if (aFindID != myListeners.end()) {
212 std::map<void*, std::list<Events_Listener*> >::iterator aFindSender =
213 aFindID->second.begin();
214 for(; aFindSender != aFindID->second.end(); aFindSender++) {
215 std::list<Events_Listener*>::iterator aListener = aFindSender->second.begin();
216 for(; aListener != aFindSender->second.end(); aListener++) {
217 if ((*aListener)->groupMessages()) {
218 (*aListener)->flushGrouped(theID);
224 if (hasEventsToFlush && myGroups.empty() && myFlushed.empty()) {
225 // no more messages left in the queue, so, finalize the sketch processing
226 static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
227 std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
228 Events_Loop::loop()->send(aMsg, false);
232 void Events_Loop::eraseMessages(const Events_ID& theID)
234 std::map<char*, std::shared_ptr<Events_Message> >::iterator aMyGroup =
235 myGroups.find(theID.eventText());
236 if (aMyGroup != myGroups.end()) {
237 myGroups.erase(aMyGroup);
242 bool Events_Loop::activateFlushes(const bool theActivate)
244 bool isActive = myFlushActive;
245 myFlushActive = theActivate;
249 void Events_Loop::clear(const Events_ID& theID)
251 std::map<char*, std::shared_ptr<Events_Message>>::iterator aMyGroup =
252 myGroups.find(theID.eventText());
253 if (aMyGroup != myGroups.end()) { // really sends
254 myGroups.erase(aMyGroup);
258 bool Events_Loop::isFlushed(const Events_ID& theID)
260 return myFlushed.find(theID.myID) != myFlushed.end();
263 void Events_Loop::setFlushed(const Events_ID& theID, const bool theValue)
266 myFlushed.insert(theID.myID);
268 myFlushed.erase(myFlushed.find(theID.myID));
271 bool Events_Loop::hasGrouppedEvent(const Events_ID& theID)
273 return myGroups.find(theID.myID) != myGroups.end();