1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Events_Loop.hxx
4 // Created: Thu Mar 13 2014
5 // Author: Mikhail PONIKAROV
7 #include <Events_Loop.h>
8 #include <Events_MessageGroup.h>
15 Events_Loop* Events_Loop::loop()
17 // initialized on initialization of the application
18 static Events_Loop MAIN_LOOP;
22 Events_ID Events_Loop::eventByName(const char* theName)
24 ///! All events created in this session, uniquely identified by the text and char pointer
25 static map<string, char*> CREATED_EVENTS;
27 string aName(theName);
28 map<string, char*>::iterator aFound = CREATED_EVENTS.find(aName);
29 if (aFound == CREATED_EVENTS.end()) { //not created yet
31 aResult = _strdup(theName); // copy to make unique internal pointer
33 aResult = strdup(theName); // copy to make unique internal pointer
35 CREATED_EVENTS[aName] = aResult;
37 aResult = aFound->second;
39 return Events_ID(aResult);
42 void Events_Loop::send(const std::shared_ptr<Events_Message>& theMessage, bool isGroup)
44 if (myImmediateListeners.find(theMessage->eventID().eventText()) != myImmediateListeners.end()) {
45 myImmediateListeners[theMessage->eventID().eventText()]->processEvent(theMessage);
47 // if it is grouped message, just accumulate it
48 if (isGroup && myFlushed.find(theMessage->eventID().myID) == myFlushed.end()) {
49 std::shared_ptr<Events_MessageGroup> aGroup =
50 std::dynamic_pointer_cast<Events_MessageGroup>(theMessage);
52 std::map<char*, std::shared_ptr<Events_Message> >::iterator aMyGroup = myGroups.find(
53 aGroup->eventID().eventText());
54 if (aMyGroup == myGroups.end()) { // create a new group of messages for accumulation
55 myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
56 aMyGroup = myGroups.find(aGroup->eventID().eventText());
58 std::shared_ptr<Events_MessageGroup> aStored =
59 std::dynamic_pointer_cast<Events_MessageGroup>(aMyGroup->second);
60 aStored->Join(aGroup);
65 // TODO: make it in thread and with usage of semaphores
67 map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
68 theMessage->eventID().eventText());
69 if (aFindID != myListeners.end()) {
70 map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
71 theMessage->sender());
72 if (aFindSender != aFindID->second.end()) {
73 list<Events_Listener*>& aListeners = aFindSender->second;
74 for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
75 (*aL)->processEvent(theMessage);
77 if (theMessage->sender()) { // also call for NULL senders registered
78 aFindSender = aFindID->second.find(NULL);
79 if (aFindSender != aFindID->second.end()) {
80 list<Events_Listener*>& aListeners = aFindSender->second;
81 for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
82 (*aL)->processEvent(theMessage);
88 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
89 void* theSender, bool theImmediate)
91 if (theImmediate) { // just register as an immediate
92 myImmediateListeners[theID.eventText()] = theListener;
95 map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
97 if (aFindID == myListeners.end()) { // create container associated with ID
98 myListeners[theID.eventText()] = map<void*, list<Events_Listener*> >();
99 aFindID = myListeners.find(theID.eventText());
102 map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(theSender);
103 if (aFindSender == aFindID->second.end()) { // create container associated with sender
104 aFindID->second[theSender] = list<Events_Listener*>();
105 aFindSender = aFindID->second.find(theSender);
107 // check that listener was not registered wit hsuch parameters before
108 list<Events_Listener*>& aListeners = aFindSender->second;
109 for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
110 if (*aL == theListener)
111 return; // avoid duplicates
113 aListeners.push_back(theListener);
116 void Events_Loop::flush(const Events_ID& theID)
120 std::map<char*, std::shared_ptr<Events_Message> >::iterator aMyGroup;
121 for(aMyGroup = myGroups.find(theID.eventText());
122 aMyGroup != myGroups.end(); aMyGroup = myGroups.find(theID.eventText()))
124 bool aWasFlushed = myFlushed.find(theID.myID) != myFlushed.end();
126 myFlushed.insert(theID.myID);
127 std::shared_ptr<Events_Message> aGroup = aMyGroup->second;
128 myGroups.erase(aMyGroup);
132 myFlushed.erase(myFlushed.find(theID.myID));
136 void Events_Loop::activateFlushes(const bool theActivate)
138 myFlushActive = theActivate;
141 void Events_Loop::clear(const Events_ID& theID)
143 std::map<char*, std::shared_ptr<Events_Message>>::iterator aMyGroup =
144 myGroups.find(theID.eventText());
145 if (aMyGroup != myGroups.end()) { // really sends
146 myGroups.erase(aMyGroup);
150 void Events_Loop::autoFlush(const Events_ID& theID, const bool theAuto)
153 myFlushed.insert(theID.myID);
155 myFlushed.erase(myFlushed.find(theID.myID));
158 bool Events_Loop::isFlushed(const Events_ID& theID)
160 return myFlushed.find(theID.myID) != myFlushed.end();
163 void Events_Loop::setFlushed(const Events_ID& theID, const bool theValue)
166 myFlushed.insert(theID.myID);
168 myFlushed.erase(myFlushed.find(theID.myID));