Salome HOME
Sources formated according to the codeing standards
[modules/shaper.git] / src / Events / Events_LongOp.cpp
1 // File:        Events_LongOp.cpp
2 // Created:     29 Jul 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Events_LongOp.h>
6 #include <Events_Loop.h>
7
8 /// senders of the long operation collected, ends the long operation event only
9 /// if all senders are stopped
10 std::map<void*, int> MY_SENDERS;
11
12 Events_LongOp::Events_LongOp(void* theSender)
13     : Events_Message(Events_LongOp::eventID(), theSender)
14 {
15 }
16
17 Events_LongOp::~Events_LongOp()
18 {
19 }
20
21 Events_ID Events_LongOp::eventID()
22 {
23   Events_Loop* aLoop = Events_Loop::loop();
24   return aLoop->eventByName("LongOperation");
25 }
26
27 void Events_LongOp::start(void* theSender)
28 {
29   bool toSend = MY_SENDERS.empty();
30   if (MY_SENDERS.find(theSender) == MY_SENDERS.end())
31     MY_SENDERS[theSender] = 1;
32   else
33     MY_SENDERS[theSender]++;
34
35   if (toSend) {
36     Events_LongOp anEvent(theSender);
37     Events_Loop::loop()->send(anEvent);
38   }
39 }
40
41 void Events_LongOp::end(void* theSender)
42 {
43   if (MY_SENDERS.find(theSender) != MY_SENDERS.end()) {
44     int aCount = MY_SENDERS[theSender];
45     if (aCount <= 1)
46       MY_SENDERS.erase(theSender);
47     else
48       MY_SENDERS[theSender] = aCount - 1;
49   }
50   if (MY_SENDERS.empty()) {
51     Events_LongOp anEvent(theSender);
52     Events_Loop::loop()->send(anEvent);
53   }
54 }
55
56 bool Events_LongOp::isPerformed()
57 {
58   return !MY_SENDERS.empty();
59 }