]> Dogcows Code - chaz/yoink/blob - src/Moof/Dispatch.cc
port to NetBSD
[chaz/yoink] / src / Moof / Dispatch.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <map>
30
31 #include "Dispatch.hh"
32
33
34 namespace Mf {
35
36
37 class Dispatch::Impl
38 {
39 public:
40
41 Impl() :
42 mId(0) {}
43
44 Dispatch::Handler getNewHandler()
45 {
46 ++mId;
47 //return Dispatch::Handler(this, mId);
48 Dispatch::Handler handler(this, mId);
49 return handler;
50 }
51
52 typedef std::pair<unsigned,Dispatch::Function> Callback;
53 typedef std::multimap<std::string,Callback> CallbackLookup;
54 typedef CallbackLookup::iterator CallbackIter;
55
56 typedef std::multimap<unsigned,std::string> HandlerLookup;
57 typedef HandlerLookup::iterator HandlerIter;
58
59
60 inline Handler addHandler(const std::string& event,
61 const Function& callback, Handler handler)
62 {
63 mCallbacks.insert(std::make_pair(event,
64 std::make_pair(handler.getId(), callback)));
65 mHandlers.insert(std::make_pair(handler.getId(), event));
66
67 return handler;
68 }
69
70 inline void removeHandler(unsigned id)
71 {
72 std::pair<HandlerIter,HandlerIter> matching(mHandlers.equal_range(id));
73
74 for (HandlerIter it = matching.first; it != matching.second; ++it)
75 {
76 CallbackIter first = mCallbacks.find((*it).second);
77 CallbackIter last = mCallbacks.end();
78
79 for (CallbackIter jt = first; jt != last; ++jt)
80 {
81 if ((*jt).second.first == id)
82 {
83 mCallbacks.erase(jt);
84 break;
85 }
86 }
87 }
88
89 mHandlers.erase(id);
90 }
91
92 void dispatch(const std::string& event, const Message* message)
93 {
94 std::pair<CallbackIter,CallbackIter>
95 callbacks(mCallbacks.equal_range(event));
96
97 for (CallbackIter it = callbacks.first; it != callbacks.second; ++it)
98 {
99 Function callback = (*it).second.second;
100 callback(message);
101 }
102 }
103
104
105 unsigned mId;
106
107 CallbackLookup mCallbacks;
108 HandlerLookup mHandlers;
109 };
110
111
112 Dispatch::Handler::~Handler()
113 {
114 if (mId)
115 {
116 mDispatch->removeHandler(mId);
117 }
118 }
119
120
121 Dispatch::Dispatch() :
122 mImpl(new Dispatch::Impl) {}
123
124 Dispatch::~Dispatch() {}
125
126
127 Dispatch& Dispatch::getInstance()
128 {
129 static Dispatch dispatch;
130 return dispatch;
131 }
132
133
134 Dispatch::Handler Dispatch::addHandler(const std::string& event,
135 const Function& callback)
136 {
137 return addHandler(event, callback, mImpl->getNewHandler());
138 }
139
140 Dispatch::Handler Dispatch::addHandler(const std::string& event,
141 const Function& callback, Handler handler)
142 {
143 // pass through
144 return mImpl->addHandler(event, callback, handler);
145 }
146
147
148 void Dispatch::removeHandler(unsigned id)
149 {
150 // pass through
151 return mImpl->removeHandler(id);
152 }
153
154
155 void Dispatch::dispatch(const std::string& event, const Message* message)
156 {
157 // pass through
158 mImpl->dispatch(event, message);
159 }
160
161
162 } // namespace Mf
163
164 /** vim: set ts=4 sw=4 tw=80: *************************************************/
165
This page took 0.036463 seconds and 4 git commands to generate.