]> Dogcows Code - chaz/yoink/blob - src/dispatcher.cc
big batch of progress
[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::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::dispatcher_impl) {}
66
67
68 // TODO these methods are ugly
69
70 dispatcher::handler dispatcher::addHandler(const std::string& message,
71 const function& callback)
72 {
73 return addHandler(message, callback, impl->getNewHandlerID());
74 }
75
76 dispatcher::handler dispatcher::addHandler(const std::string& message,
77 const function& callback, handler id)
78 {
79 std::pair<std::string,dispatcher::dispatcher_impl::callback_t>
80 callbackPair(message, dispatcher::dispatcher_impl::callback_t(id, callback));
81
82 std::pair<handler,std::string> handlerPair(id, message);
83
84 impl->callbacks.insert(callbackPair);
85 impl->handlers.insert(handlerPair);
86
87 return id;
88 }
89
90
91 void dispatcher::removeHandler(handler id)
92 {
93 std::pair<dispatcher::dispatcher_impl::handler_it_t,dispatcher::dispatcher_impl::handler_it_t>
94 handlers(impl->handlers.equal_range(id));
95
96 dispatcher::dispatcher_impl::handler_it_t i;
97 for (i = handlers.first; i != handlers.second; i++)
98 {
99 dispatcher::dispatcher_impl::callback_it_t it = impl->callbacks.find((*i).second);
100 dispatcher::dispatcher_impl::callback_it_t last = impl->callbacks.end();
101
102 dispatcher::dispatcher_impl::callback_it_t j;
103 for (j = it; j != last; j++)
104 {
105 if (((*j).second).first == id)
106 {
107 impl->callbacks.erase(j);
108 break;
109 }
110 }
111 }
112
113 impl->handlers.erase(id);
114 }
115
116
117 void dispatcher::dispatch(const std::string& message)
118 {
119 dispatch(message, notification());
120 }
121
122 void dispatcher::dispatch(const std::string& message, const notification& param)
123 {
124 std::pair<dispatcher::dispatcher_impl::callback_it_t,dispatcher::dispatcher_impl::callback_it_t>
125 callbacks(impl->callbacks.equal_range(message));
126
127 dispatcher::dispatcher_impl::callback_it_t i;
128 for (i = callbacks.first; i != callbacks.second; i++)
129 {
130 function callback = ((*i).second).second;
131 callback(param);
132 }
133 }
134
135
136 } // namespace dc
137
138 /** vim: set ts=4 sw=4 tw=80: *************************************************/
139
This page took 0.033346 seconds and 4 git commands to generate.