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() {}
40 class dispatcher::dispatcher_impl
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::dispatcher_impl
) {}
68 // TODO these methods are ugly
70 dispatcher::handler
dispatcher::addHandler(const std::string
& message
,
71 const function
& callback
)
73 return addHandler(message
, callback
, impl
->getNewHandlerID());
76 dispatcher::handler
dispatcher::addHandler(const std::string
& message
,
77 const function
& callback
, handler id
)
79 std::pair
<std::string
,dispatcher::dispatcher_impl::callback_t
>
80 callbackPair(message
, dispatcher::dispatcher_impl::callback_t(id
, callback
));
82 std::pair
<handler
,std::string
> handlerPair(id
, message
);
84 impl
->callbacks
.insert(callbackPair
);
85 impl
->handlers
.insert(handlerPair
);
91 void dispatcher::removeHandler(handler id
)
93 std::pair
<dispatcher::dispatcher_impl::handler_it_t
,dispatcher::dispatcher_impl::handler_it_t
>
94 handlers(impl
->handlers
.equal_range(id
));
96 dispatcher::dispatcher_impl::handler_it_t i
;
97 for (i
= handlers
.first
; i
!= handlers
.second
; i
++)
99 dispatcher::dispatcher_impl::callback_it_t it
= impl
->callbacks
.find((*i
).second
);
100 dispatcher::dispatcher_impl::callback_it_t last
= impl
->callbacks
.end();
102 dispatcher::dispatcher_impl::callback_it_t j
;
103 for (j
= it
; j
!= last
; j
++)
105 if (((*j
).second
).first
== id
)
107 impl
->callbacks
.erase(j
);
113 impl
->handlers
.erase(id
);
117 void dispatcher::dispatch(const std::string
& message
)
119 dispatch(message
, notification());
122 void dispatcher::dispatch(const std::string
& message
, const notification
& param
)
124 std::pair
<dispatcher::dispatcher_impl::callback_it_t
,dispatcher::dispatcher_impl::callback_it_t
>
125 callbacks(impl
->callbacks
.equal_range(message
));
127 dispatcher::dispatcher_impl::callback_it_t i
;
128 for (i
= callbacks
.first
; i
!= callbacks
.second
; i
++)
130 function callback
= ((*i
).second
).second
;
138 /** vim: set ts=4 sw=4 tw=80: *************************************************/