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
28 aResult = strdup(theName); // copy to make unique internal pointer
29 CREATED_EVENTS[aName] = aResult;
31 aResult = aFound->second;
33 return Events_ID(aResult);
36 void Events_Loop::send(Events_Message& theMessage, bool isGroup)
38 // if it is grouped message, just accumulate it
40 Events_MessageGroup* aGroup = dynamic_cast<Events_MessageGroup*>(&theMessage);
42 std::map<char*, Events_MessageGroup*>::iterator aMyGroup =
43 myGroups.find(aGroup->eventID().eventText());
44 if (aMyGroup == myGroups.end()) { // create a new group of messages for accumulation
45 myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
46 aMyGroup = myGroups.find(aGroup->eventID().eventText());
48 aMyGroup->second->Join(*aGroup);
53 // TO DO: make it in thread and with usage of semaphores
55 map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
56 theMessage.eventID().eventText());
57 if (aFindID != myListeners.end()) {
58 map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
60 if (aFindSender != aFindID->second.end()) {
61 list<Events_Listener*>& aListeners = aFindSender->second;
62 for(list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
63 (*aL)->processEvent(&theMessage);
65 if (theMessage.sender()) { // also call for NULL senders registered
66 aFindSender = aFindID->second.find(NULL);
67 if (aFindSender != aFindID->second.end()) {
68 list<Events_Listener*>& aListeners = aFindSender->second;
69 for(list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
70 (*aL)->processEvent(&theMessage);
76 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
79 map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
81 if (aFindID == myListeners.end()) { // create container associated with ID
82 myListeners[theID.eventText()] = map<void*, list<Events_Listener*> >();
83 aFindID = myListeners.find(theID.eventText());
86 map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(theSender);
87 if (aFindSender == aFindID->second.end()) { // create container associated with sender
88 aFindID->second[theSender] = list<Events_Listener*>();
89 aFindSender = aFindID->second.find(theSender);
91 // check that listener was not registered wit hsuch parameters before
92 list<Events_Listener*>& aListeners = aFindSender->second;
93 for(list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
94 if (*aL == theListener)
95 return; // avoid duplicates
97 aListeners.push_back(theListener);
100 void Events_Loop::flush(const Events_ID& theID)
102 std::map<char*, Events_MessageGroup*>::iterator aMyGroup =
103 myGroups.find(theID.eventText());
104 if (aMyGroup != myGroups.end()) { // really sends
105 Events_MessageGroup* aGroup = aMyGroup->second;
106 myGroups.erase(aMyGroup);
107 send(*aGroup, false);