+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
// File: Events_Loop.hxx
// Created: Thu Mar 13 2014
// Author: Mikhail PONIKAROV
return Events_ID(aResult);
}
-void Events_Loop::send(Events_Message& theMessage, bool isGroup)
+void Events_Loop::send(const std::shared_ptr<Events_Message>& theMessage, bool isGroup)
{
+ if (myImmediateListeners.find(theMessage->eventID().eventText()) != myImmediateListeners.end()) {
+ myImmediateListeners[theMessage->eventID().eventText()]->processEvent(theMessage);
+ }
// if it is grouped message, just accumulate it
- if (isGroup) {
- Events_MessageGroup* aGroup = dynamic_cast<Events_MessageGroup*>(&theMessage);
+ if (isGroup && myFlushed.find(theMessage->eventID().myID) == myFlushed.end()) {
+ std::shared_ptr<Events_MessageGroup> aGroup =
+ std::dynamic_pointer_cast<Events_MessageGroup>(theMessage);
if (aGroup) {
- std::map<char*, Events_MessageGroup*>::iterator aMyGroup = myGroups.find(
+ std::map<char*, std::shared_ptr<Events_Message> >::iterator aMyGroup = myGroups.find(
aGroup->eventID().eventText());
if (aMyGroup == myGroups.end()) { // create a new group of messages for accumulation
myGroups[aGroup->eventID().eventText()] = aGroup->newEmpty();
aMyGroup = myGroups.find(aGroup->eventID().eventText());
}
- aMyGroup->second->Join(*aGroup);
+ std::shared_ptr<Events_MessageGroup> aStored =
+ std::dynamic_pointer_cast<Events_MessageGroup>(aMyGroup->second);
+ aStored->Join(aGroup);
return;
}
}
- // TO DO: make it in thread and with usage of semaphores
+ // TODO: make it in thread and with usage of semaphores
map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
- theMessage.eventID().eventText());
+ theMessage->eventID().eventText());
if (aFindID != myListeners.end()) {
map<void*, list<Events_Listener*> >::iterator aFindSender = aFindID->second.find(
- theMessage.sender());
+ theMessage->sender());
if (aFindSender != aFindID->second.end()) {
list<Events_Listener*>& aListeners = aFindSender->second;
for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
- (*aL)->processEvent(&theMessage);
+ (*aL)->processEvent(theMessage);
}
- if (theMessage.sender()) { // also call for NULL senders registered
+ if (theMessage->sender()) { // also call for NULL senders registered
aFindSender = aFindID->second.find(NULL);
if (aFindSender != aFindID->second.end()) {
list<Events_Listener*>& aListeners = aFindSender->second;
for (list<Events_Listener*>::iterator aL = aListeners.begin(); aL != aListeners.end(); aL++)
- (*aL)->processEvent(&theMessage);
+ (*aL)->processEvent(theMessage);
}
}
}
}
void Events_Loop::registerListener(Events_Listener* theListener, const Events_ID theID,
- void* theSender)
+ void* theSender, bool theImmediate)
{
+ if (theImmediate) { // just register as an immediate
+ myImmediateListeners[theID.eventText()] = theListener;
+ return;
+ }
map<char*, map<void*, list<Events_Listener*> > >::iterator aFindID = myListeners.find(
theID.eventText());
if (aFindID == myListeners.end()) { // create container associated with ID
void Events_Loop::flush(const Events_ID& theID)
{
- std::map<char*, Events_MessageGroup*>::iterator aMyGroup = myGroups.find(theID.eventText());
+ if (!myFlushActive)
+ return;
+ std::map<char*, std::shared_ptr<Events_Message> >::iterator aMyGroup;
+ for(aMyGroup = myGroups.find(theID.eventText());
+ aMyGroup != myGroups.end(); aMyGroup = myGroups.find(theID.eventText()))
+ { // really sends
+ bool aWasFlushed = myFlushed.find(theID.myID) != myFlushed.end();
+ if (!aWasFlushed)
+ myFlushed.insert(theID.myID);
+ std::shared_ptr<Events_Message> aGroup = aMyGroup->second;
+ myGroups.erase(aMyGroup);
+ send(aGroup, false);
+
+ if (!aWasFlushed)
+ myFlushed.erase(myFlushed.find(theID.myID));
+ }
+}
+
+void Events_Loop::activateFlushes(const bool theActivate)
+{
+ myFlushActive = theActivate;
+}
+
+void Events_Loop::clear(const Events_ID& theID)
+{
+ std::map<char*, std::shared_ptr<Events_Message>>::iterator aMyGroup =
+ myGroups.find(theID.eventText());
if (aMyGroup != myGroups.end()) { // really sends
- Events_MessageGroup* aGroup = aMyGroup->second;
myGroups.erase(aMyGroup);
- send(*aGroup, false);
- delete aGroup;
}
}
+
+void Events_Loop::autoFlush(const Events_ID& theID, const bool theAuto)
+{
+ if (theAuto)
+ myFlushed.insert(theID.myID);
+ else
+ myFlushed.erase(myFlushed.find(theID.myID));
+}
+
+bool Events_Loop::isFlushed(const Events_ID& theID)
+{
+ return myFlushed.find(theID.myID) != myFlushed.end();
+}
+
+void Events_Loop::setFlushed(const Events_ID& theID, const bool theValue)
+{
+ if (theValue)
+ myFlushed.insert(theID.myID);
+ else
+ myFlushed.erase(myFlushed.find(theID.myID));
+}