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 notification::~notification() {}
43 dispatcher_impl() : id(1) {}
45 dispatcher::handler
getNewHandlerID()
48 return (dispatcher::handler
)id
;
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
;
55 typedef std::multimap
<dispatcher::handler
,std::string
> handler_lookup_t
;
56 typedef handler_lookup_t::iterator handler_it_t
;
58 unsigned long long id
;
60 callback_lookup_t callbacks
;
61 handler_lookup_t handlers
;
65 dispatcher::dispatcher() : impl(new dispatcher_impl
) {}
68 dispatcher::handler
dispatcher::addHandler(const std::string
& message
,
69 const function
& callback
)
71 return addHandler(message
, callback
, impl
->getNewHandlerID());
74 dispatcher::handler
dispatcher::addHandler(const std::string
& message
,
75 const function
& callback
, handler id
)
77 std::pair
<std::string
,dispatcher_impl::callback_t
>
78 callbackPair(message
, dispatcher_impl::callback_t(id
, callback
));
80 std::pair
<handler
,std::string
> handlerPair(id
, message
);
82 impl
->callbacks
.insert(callbackPair
);
83 impl
->handlers
.insert(handlerPair
);
89 void dispatcher::removeHandler(handler id
)
91 std::pair
<dispatcher_impl::handler_it_t
,dispatcher_impl::handler_it_t
>
92 handlers(impl
->handlers
.equal_range(id
));
94 dispatcher_impl::handler_it_t i
;
95 for (i
= handlers
.first
; i
!= handlers
.second
; i
++)
97 dispatcher_impl::callback_it_t it
= impl
->callbacks
.find((*i
).second
);
98 dispatcher_impl::callback_it_t last
= impl
->callbacks
.end();
100 dispatcher_impl::callback_it_t j
;
101 for (j
= it
; j
!= last
; j
++)
103 if (((*j
).second
).first
== id
)
105 impl
->callbacks
.erase(j
);
111 impl
->handlers
.erase(id
);
115 void dispatcher::dispatch(const std::string
& message
)
117 dispatch(message
, notification());
120 void dispatcher::dispatch(const std::string
& message
, const notification
& param
)
122 std::pair
<dispatcher_impl::callback_it_t
,dispatcher_impl::callback_it_t
>
123 callbacks(impl
->callbacks
.equal_range(message
));
125 dispatcher_impl::callback_it_t i
;
126 for (i
= callbacks
.first
; i
!= callbacks
.second
; i
++)
128 function callback
= ((*i
).second
).second
;