]> Dogcows Code - chaz/yoink/blob - src/Moof/Dispatcher.cc
minor refactoring and state progress
[chaz/yoink] / src / Moof / Dispatcher.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 "Dispatcher.hh"
32
33
34 namespace Mf {
35
36
37 class Dispatcher::Impl
38 {
39 friend class Dispatcher;
40
41 Impl() :
42 mId(1) {}
43
44 Dispatcher::Handler getNewHandler()
45 {
46 mId += 2;
47 return (Dispatcher::Handler)mId;
48 }
49
50 typedef std::pair<Dispatcher::Handler,Dispatcher::Function> Callback;
51 typedef std::multimap<std::string,Callback> CallbackLookup;
52 typedef CallbackLookup::iterator CallbackIter;
53
54 typedef std::multimap<Dispatcher::Handler,std::string> HandlerLookup;
55 typedef HandlerLookup::iterator HandlerIter;
56
57
58 inline Handler addHandler(const std::string& message,
59 const Function& callback, Handler id)
60 {
61 mCallbacks.insert(std::make_pair(message, std::make_pair(id, callback)));
62 mHandlers.insert(std::make_pair(id, message));
63
64 return id;
65 }
66
67 inline void removeHandler(Handler id)
68 {
69 std::pair<HandlerIter,HandlerIter> matching(mHandlers.equal_range(id));
70
71 for (HandlerIter it = matching.first; it != matching.second; ++it)
72 {
73 CallbackIter first = mCallbacks.find((*it).second);
74 CallbackIter last = mCallbacks.end();
75
76 for (CallbackIter jt = first; jt != last; ++jt)
77 {
78 if ((*jt).second.first == id)
79 {
80 mCallbacks.erase(jt);
81 break;
82 }
83 }
84 }
85
86 mHandlers.erase(id);
87 }
88
89 void dispatch(const std::string& message, const Notification* param)
90 {
91 std::pair<CallbackIter,CallbackIter>
92 callbacks(mCallbacks.equal_range(message));
93
94 for (CallbackIter it = callbacks.first; it != callbacks.second; ++it)
95 {
96 Function callback = (*it).second.second;
97 callback(param);
98 }
99 }
100
101
102 unsigned long mId;
103
104 CallbackLookup mCallbacks;
105 HandlerLookup mHandlers;
106 };
107
108
109 Dispatcher::Dispatcher() :
110 mImpl(new Dispatcher::Impl) {}
111
112 Dispatcher::~Dispatcher() {}
113
114
115 Dispatcher& Dispatcher::getInstance()
116 {
117 static Dispatcher dispatcher;
118 return dispatcher;
119 }
120
121
122 Dispatcher::Handler Dispatcher::addHandler(const std::string& message,
123 const Function& callback)
124 {
125 return addHandler(message, callback, mImpl->getNewHandler());
126 }
127
128 Dispatcher::Handler Dispatcher::addHandler(const std::string& message,
129 const Function& callback, Handler id)
130 {
131 // pass through
132 return mImpl->addHandler(message, callback, id);
133 }
134
135
136 void Dispatcher::removeHandler(Handler id)
137 {
138 // pass through
139 return mImpl->removeHandler(id);
140 }
141
142
143 void Dispatcher::dispatch(const std::string& message, const Notification* param)
144 {
145 // pass through
146 mImpl->dispatch(message, param);
147 }
148
149
150 } // namespace Mf
151
152 /** vim: set ts=4 sw=4 tw=80: *************************************************/
153
This page took 0.036681 seconds and 4 git commands to generate.