1 // File: Events_Loop.hxx
2 // Created: Thu Mar 13 2014
3 // Author: Mikhail PONIKAROV
5 #include <Events_Loop.h>
6 #include <Events_MessageGroup.h>
13 Events_Loop* Events_Loop::loop()
15 // initialized on initialization of the application
16 static Events_Loop MAIN_LOOP;
20 Events_ID Events_Loop::eventByName(const char* theName)
22 ///! All events created in this session, uniquely identified by the text and char pointer
23 static map<string, char*> CREATED_EVENTS;
25 string aName(theName);
26 map<string, char*>::iterator aFound = CREATED_EVENTS.find(aName);
27 if (aFound == CREATED_EVENTS.end()) { //not created yet
29 aResult = _strdup(theName); // copy to make unique internal pointer
31 aResult = strdup(theName); // copy to make unique internal pointer
33 CREATED_EVENTS[aName] = aResult;
35 aResult = aFound->second;
37 return Events_ID(aResult);
40 void Events_Loop::send(Events_Message& theMessage, bool isGroup)
42 // if it is grouped message, just accumulate it
44 Events_MessageGroup* aGroup = dynamic_cast<Events_MessageGroup*>(&theMessage);
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());
52 aMyGroup->second->Join(*aGroup);
57 // TO DO: make it in thread and with usage of semaphores
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(
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);
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);
80 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
83 map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
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());
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);
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
101 aListeners.push_back(theListener);
104 void Events_Loop::flush(const Events_ID& theID)
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);
117 void Events_Loop::activateFlushes(const bool theActivate)
119 myFlushActive = theActivate;