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