]> Dogcows Code - chaz/yoink/blob - src/Moof/Dispatcher.cc
revamped scene drawing in preparation for octree
[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 class Dispatcher::DispatcherImpl
41 {
42 public:
43 DispatcherImpl() : 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;
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 unsigned long long id;
59
60 CallbackLookup callbacks;
61 HandlerLookup handlers;
62 };
63
64
65 Dispatcher::Dispatcher() :
66 impl_(new Dispatcher::DispatcherImpl) {}
67
68
69 // TODO these methods are ugly
70
71 Dispatcher::Handler Dispatcher::addHandler(const std::string& message,
72 const Function& callback)
73 {
74 return addHandler(message, callback, impl_->getNewHandlerID());
75 }
76
77 Dispatcher::Handler Dispatcher::addHandler(const std::string& message,
78 const Function& callback, Handler id)
79 {
80 std::pair<std::string,Dispatcher::DispatcherImpl::Callback>
81 callbackPair(message, Dispatcher::DispatcherImpl::Callback(id, callback));
82
83 std::pair<Handler,std::string> handlerPair(id, message);
84
85 impl_->callbacks.insert(callbackPair);
86 impl_->handlers.insert(handlerPair);
87
88 return id;
89 }
90
91
92 void Dispatcher::removeHandler(Handler id)
93 {
94 std::pair<Dispatcher::DispatcherImpl::HandlerIter,Dispatcher::DispatcherImpl::HandlerIter>
95 handlers(impl_->handlers.equal_range(id));
96
97 Dispatcher::DispatcherImpl::HandlerIter it;
98 for (it = handlers.first; it != handlers.second; ++it)
99 {
100 Dispatcher::DispatcherImpl::CallbackIter first = impl_->callbacks.find((*it).second);
101 Dispatcher::DispatcherImpl::CallbackIter last = impl_->callbacks.end();
102
103 Dispatcher::DispatcherImpl::CallbackIter jt;
104 for (jt = first; jt != last; ++jt)
105 {
106 if (((*jt).second).first == id)
107 {
108 impl_->callbacks.erase(jt);
109 break;
110 }
111 }
112 }
113
114 impl_->handlers.erase(id);
115 }
116
117
118 void Dispatcher::dispatch(const std::string& message)
119 {
120 dispatch(message, Notification());
121 }
122
123 void Dispatcher::dispatch(const std::string& message, const Notification& param)
124 {
125 std::pair<Dispatcher::DispatcherImpl::CallbackIter,Dispatcher::DispatcherImpl::CallbackIter>
126 callbacks(impl_->callbacks.equal_range(message));
127
128 Dispatcher::DispatcherImpl::CallbackIter it;
129 for (it = callbacks.first; it != callbacks.second; ++it)
130 {
131 Function callback = ((*it).second).second;
132 callback(param);
133 }
134 }
135
136
137 } // namespace Mf
138
139 /** vim: set ts=4 sw=4 tw=80: *************************************************/
140
This page took 0.039209 seconds and 4 git commands to generate.