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(const boost::shared_ptr<Events_Message>& theMessage, bool isGroup)
42 // if it is grouped message, just accumulate it
44 boost::shared_ptr<Events_MessageGroup> aGroup =
45 boost::dynamic_pointer_cast<Events_MessageGroup>(theMessage);
47 std::map<char*, boost::shared_ptr<Events_Message> >::iterator aMyGroup = myGroups.find(
48 aGroup->eventID().eventText());
49 if (aMyGroup == myGroups.end()) { // create a new group of messages for accumulation
50 myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
51 aMyGroup = myGroups.find(aGroup->eventID().eventText());
53 boost::shared_ptr<Events_MessageGroup> aStored =
54 boost::dynamic_pointer_cast<Events_MessageGroup>(aMyGroup->second);
55 aStored->Join(aGroup);
60 // TO DO: make it in thread and with usage of semaphores
62 map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
63 theMessage->eventID().eventText());
64 if (aFindID != myListeners.end()) {
65 map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
66 theMessage->sender());
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);
72 if (theMessage->sender()) { // also call for NULL senders registered
73 aFindSender = aFindID->second.find(NULL);
74 if (aFindSender != aFindID->second.end()) {
75 list<Events_Listener*>& aListeners = aFindSender->second;
76 for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
77 (*aL)->processEvent(theMessage);
83 void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
86 map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
88 if (aFindID == myListeners.end()) { // create container associated with ID
89 myListeners[theID.eventText()] = map<void*, list<Events_Listener*> >();
90 aFindID = myListeners.find(theID.eventText());
93 map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(theSender);
94 if (aFindSender == aFindID->second.end()) { // create container associated with sender
95 aFindID->second[theSender] = list<Events_Listener*>();
96 aFindSender = aFindID->second.find(theSender);
98 // check that listener was not registered wit hsuch parameters before
99 list<Events_Listener*>& aListeners = aFindSender->second;
100 for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
101 if (*aL == theListener)
102 return; // avoid duplicates
104 aListeners.push_back(theListener);
107 void Events_Loop::flush(const Events_ID& theID)
111 std::map<char*, boost::shared_ptr<Events_Message>>::iterator aMyGroup =
112 myGroups.find(theID.eventText());
113 if (aMyGroup != myGroups.end()) { // really sends
114 boost::shared_ptr<Events_Message> aGroup = aMyGroup->second;
115 myGroups.erase(aMyGroup);
120 void Events_Loop::activateFlushes(const bool theActivate)
122 myFlushActive = theActivate;