]>
Dogcows Code - chaz/yoink/blob - src/Moof/Dispatcher.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
31 #include "Dispatcher.hh"
37 struct Dispatcher::Impl
42 Dispatcher::Handler
getNewHandler()
45 return (Dispatcher::Handler
)id
;
48 typedef std::pair
<Dispatcher::Handler
,Dispatcher::Function
> Callback
;
49 typedef std::multimap
<std::string
,Callback
> CallbackLookup
;
50 typedef CallbackLookup::iterator CallbackIter
;
52 typedef std::multimap
<Dispatcher::Handler
,std::string
> HandlerLookup
;
53 typedef HandlerLookup::iterator HandlerIter
;
56 inline Handler
addHandler(const std::string
& message
,
57 const Function
& callback
, Handler id
)
59 callbacks
.insert(std::make_pair(message
, std::make_pair(id
, callback
)));
60 handlers
.insert(std::make_pair(id
, message
));
65 inline void removeHandler(Handler id
)
67 std::pair
<HandlerIter
,HandlerIter
> matching(handlers
.equal_range(id
));
69 for (HandlerIter it
= matching
.first
; it
!= matching
.second
; ++it
)
71 CallbackIter first
= callbacks
.find((*it
).second
);
72 CallbackIter last
= callbacks
.end();
74 for (CallbackIter jt
= first
; jt
!= last
; ++jt
)
76 if ((*jt
).second
.first
== id
)
89 CallbackLookup callbacks
;
90 HandlerLookup handlers
;
94 Dispatcher::Dispatcher() :
95 impl_(new Dispatcher::Impl
) {}
97 Dispatcher::~Dispatcher() {}
100 Dispatcher
& Dispatcher::getInstance()
102 static Dispatcher dispatcher
;
107 Dispatcher::Handler
Dispatcher::addHandler(const std::string
& message
,
108 const Function
& callback
)
110 return addHandler(message
, callback
, impl_
->getNewHandler());
113 Dispatcher::Handler
Dispatcher::addHandler(const std::string
& message
,
114 const Function
& callback
, Handler id
)
117 return impl_
->addHandler(message
, callback
, id
);
121 void Dispatcher::removeHandler(Handler id
)
124 return impl_
->removeHandler(id
);
128 void Dispatcher::dispatch(const std::string
& message
, const Notification
* param
)
130 std::pair
<Impl::CallbackIter
,Impl::CallbackIter
>
131 callbacks(impl_
->callbacks
.equal_range(message
));
133 for (Impl::CallbackIter it
= callbacks
.first
; it
!= callbacks
.second
; ++it
)
135 Function callback
= (*it
).second
.second
;
143 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.04522 seconds and 4 git commands to generate.