]> Dogcows Code - chaz/yoink/blob - src/dispatcher.cc
509b69d151bd039e5bf85d8f1d717e172c6321e7
[chaz/yoink] / src / 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 dc {
35
36
37 notification::~notification() {}
38
39
40 class dispatcher_impl
41 {
42 public:
43 dispatcher_impl() : id(1) {}
44
45 dispatcher::handler getNewHandlerID()
46 {
47 id += 4;
48 return (dispatcher::handler)id;
49 }
50
51 typedef std::pair<dispatcher::handler,dispatcher::function> callback_t;
52 typedef std::multimap<std::string,callback_t> callback_lookup_t;
53 typedef callback_lookup_t::iterator callback_it_t;
54
55 typedef std::multimap<dispatcher::handler,std::string> handler_lookup_t;
56 typedef handler_lookup_t::iterator handler_it_t;
57
58 unsigned long long id;
59
60 callback_lookup_t callbacks;
61 handler_lookup_t handlers;
62 };
63
64
65 dispatcher::dispatcher() : impl(new dispatcher_impl) {}
66
67
68 dispatcher::handler dispatcher::addHandler(const std::string& message,
69 const function& callback)
70 {
71 return addHandler(message, callback, impl->getNewHandlerID());
72 }
73
74 dispatcher::handler dispatcher::addHandler(const std::string& message,
75 const function& callback, handler id)
76 {
77 std::pair<std::string,dispatcher_impl::callback_t>
78 callbackPair(message, dispatcher_impl::callback_t(id, callback));
79
80 std::pair<handler,std::string> handlerPair(id, message);
81
82 impl->callbacks.insert(callbackPair);
83 impl->handlers.insert(handlerPair);
84
85 return id;
86 }
87
88
89 void dispatcher::removeHandler(handler id)
90 {
91 std::pair<dispatcher_impl::handler_it_t,dispatcher_impl::handler_it_t>
92 handlers(impl->handlers.equal_range(id));
93
94 dispatcher_impl::handler_it_t i;
95 for (i = handlers.first; i != handlers.second; i++)
96 {
97 dispatcher_impl::callback_it_t it = impl->callbacks.find((*i).second);
98 dispatcher_impl::callback_it_t last = impl->callbacks.end();
99
100 dispatcher_impl::callback_it_t j;
101 for (j = it; j != last; j++)
102 {
103 if (((*j).second).first == id)
104 {
105 impl->callbacks.erase(j);
106 break;
107 }
108 }
109 }
110
111 impl->handlers.erase(id);
112 }
113
114
115 void dispatcher::dispatch(const std::string& message)
116 {
117 dispatch(message, notification());
118 }
119
120 void dispatcher::dispatch(const std::string& message, const notification& param)
121 {
122 std::pair<dispatcher_impl::callback_it_t,dispatcher_impl::callback_it_t>
123 callbacks(impl->callbacks.equal_range(message));
124
125 dispatcher_impl::callback_it_t i;
126 for (i = callbacks.first; i != callbacks.second; i++)
127 {
128 function callback = ((*i).second).second;
129 callback(param);
130 }
131 }
132
133
134 } // namespace dc
135
This page took 0.037073 seconds and 3 git commands to generate.