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