]> git.tdb.fi Git - libs/core.git/blob - source/eventdispatcher.cpp
Drop copyright and license notices from files
[libs/core.git] / source / eventdispatcher.cpp
1 #include <msp/time/units.h>
2 #include "base.h"
3 #include "eventdispatcher.h"
4 #include "poll.h"
5
6 namespace Msp {
7 namespace IO {
8
9 EventDispatcher::EventDispatcher()
10 { }
11
12 void EventDispatcher::add(Base &obj)
13 {
14         SlotMap::iterator i = objects.find(&obj);
15         if(i==objects.end())
16         {
17                 i = objects.insert(SlotMap::value_type(&obj, Slot(&obj))).first;
18                 i->second.evch_conn = obj.signal_events_changed.connect(sigc::bind(sigc::mem_fun(this, &EventDispatcher::object_events_changed), &obj));
19                 i->second.del_conn = obj.signal_deleted.connect(sigc::bind(sigc::mem_fun(this, &EventDispatcher::object_deleted), &obj));
20
21                 if(obj.get_events())
22                         poller.set_object(obj, obj.get_events());
23         }
24 }
25
26 void EventDispatcher::remove(Base &obj)
27 {
28         SlotMap::iterator i = objects.find(&obj);
29         if(i!=objects.end())
30         {
31                 i->second.evch_conn.disconnect();
32                 i->second.del_conn.disconnect();
33                 objects.erase(i);
34
35                 poller.set_object(obj, P_NONE);
36         }
37 }
38
39 void EventDispatcher::tick()
40 {
41         if(objects.empty())
42                 return;
43
44         if(poller.poll()>0)
45                 dispatch();
46 }
47
48 void EventDispatcher::tick(const Time::TimeDelta &dt)
49 {
50         if(objects.empty())
51                 return;
52
53         if(poller.poll(dt)>0)
54                 dispatch();
55 }
56
57 void EventDispatcher::object_events_changed(PollEvent ev, Base *obj)
58 {
59         poller.set_object(*obj, ev);
60 }
61
62 void EventDispatcher::object_deleted(Base *obj)
63 {
64         remove(*obj);
65 }
66
67 void EventDispatcher::dispatch()
68 {
69         const Poller::SlotSeq &result = poller.get_result();
70         for(Poller::SlotSeq::const_iterator i=result.begin(); i!=result.end(); ++i)
71                 i->object->event(i->events);
72 }
73
74 } // namespace IO
75 } // namespace Msp