]> Dogcows Code - chaz/yoink/blob - src/Moof/Dispatch.cc
removed logging from script to fix compile error
[chaz/yoink] / src / Moof / Dispatch.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <map>
13
14 #include "Dispatch.hh"
15
16
17 namespace Mf {
18
19
20 class Dispatch::Impl
21 {
22 public:
23
24 Impl(Dispatch* dispatch) :
25 mDispatch(dispatch),
26 mId(0) {}
27
28 Dispatch::Handle getNewHandle()
29 {
30 ++mId;
31 Dispatch::Handle handle(mDispatch->mImpl, mId);
32 return handle;
33 }
34
35 typedef std::pair<unsigned,Dispatch::Function> Callback;
36 typedef std::multimap<std::string,Callback> CallbackLookup;
37 typedef CallbackLookup::iterator CallbackIt;
38
39 typedef std::multimap<unsigned,std::string> HandleLookup;
40 typedef HandleLookup::iterator HandleIt;
41
42
43 inline Handle addTarget(const std::string& event,
44 const Function& callback, Handle handle)
45 {
46 mCallbacks.insert(std::make_pair(event,
47 std::make_pair(handle.getId(), callback)));
48 mHandles.insert(std::make_pair(handle.getId(), event));
49
50 return handle;
51 }
52
53 inline void removeTarget(unsigned id)
54 {
55 std::pair<HandleIt,HandleIt> matching(mHandles.equal_range(id));
56
57 for (HandleIt it = matching.first; it != matching.second; ++it)
58 {
59 CallbackIt first = mCallbacks.find((*it).second);
60 CallbackIt last = mCallbacks.end();
61
62 for (CallbackIt jt = first; jt != last; ++jt)
63 {
64 if ((*jt).second.first == id)
65 {
66 mCallbacks.erase(jt);
67 break;
68 }
69 }
70 }
71
72 mHandles.erase(id);
73 }
74
75 void dispatch(const std::string& event, const Message* message)
76 {
77 std::pair<CallbackIt,CallbackIt>
78 callbacks(mCallbacks.equal_range(event));
79
80 for (CallbackIt it = callbacks.first; it != callbacks.second; ++it)
81 {
82 Function callback = (*it).second.second;
83 callback(message);
84 }
85 }
86
87
88 Dispatch* mDispatch;
89
90 unsigned mId;
91
92 CallbackLookup mCallbacks;
93 HandleLookup mHandles;
94 };
95
96
97 void Dispatch::Handle::clear()
98 {
99 boost::shared_ptr<Impl> dispatch;
100 if (mId && (dispatch = mDispatch.lock()))
101 {
102 dispatch->removeTarget(mId);
103 mId = 0;
104 }
105 }
106
107
108 Dispatch::Dispatch() :
109 mImpl(new Dispatch::Impl(this)) {}
110
111
112 Dispatch::Handle Dispatch::addTarget(const std::string& event,
113 const Function& callback)
114 {
115 return addTarget(event, callback, mImpl->getNewHandle());
116 }
117
118 Dispatch::Handle Dispatch::addTarget(const std::string& event,
119 const Function& callback,
120 Handle handle)
121 {
122 // pass through
123 return mImpl->addTarget(event, callback, handle);
124 }
125
126
127 void Dispatch::removeTarget(unsigned id)
128 {
129 // pass through
130 return mImpl->removeTarget(id);
131 }
132
133
134 void Dispatch::dispatch(const std::string& event, const Message* message)
135 {
136 // pass through
137 mImpl->dispatch(event, message);
138 }
139
140
141 Dispatch& Dispatch::global()
142 {
143 static Dispatch dispatch;
144 return dispatch;
145 }
146
147
148 } // namespace Mf
149
This page took 0.039158 seconds and 4 git commands to generate.