--- /dev/null
+
+/*******************************************************************************
+
+ Copyright (c) 2009, Charles McGarvey
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+*******************************************************************************/
+
+#include <map>
+
+#include "dispatcher.hh"
+
+
+namespace dc {
+
+
+notification::~notification() {}
+
+
+class dispatcher_impl
+{
+public:
+ dispatcher_impl() : id(1) {}
+
+ dispatcher::handler getNewHandlerID()
+ {
+ id += 4;
+ return (dispatcher::handler)id;
+ }
+
+ typedef std::pair<dispatcher::handler,dispatcher::function> callback_t;
+ typedef std::multimap<std::string,callback_t> callback_lookup_t;
+ typedef callback_lookup_t::iterator callback_it_t;
+
+ typedef std::multimap<dispatcher::handler,std::string> handler_lookup_t;
+ typedef handler_lookup_t::iterator handler_it_t;
+
+ unsigned long long id;
+
+ callback_lookup_t callbacks;
+ handler_lookup_t handlers;
+};
+
+
+dispatcher::dispatcher() : impl(new dispatcher_impl) {}
+
+
+dispatcher::handler dispatcher::addHandler(const std::string& message,
+ const function& callback)
+{
+ return addHandler(message, callback, impl->getNewHandlerID());
+}
+
+dispatcher::handler dispatcher::addHandler(const std::string& message,
+ const function& callback, handler id)
+{
+ std::pair<std::string,dispatcher_impl::callback_t>
+ callbackPair(message, dispatcher_impl::callback_t(id, callback));
+
+ std::pair<handler,std::string> handlerPair(id, message);
+
+ impl->callbacks.insert(callbackPair);
+ impl->handlers.insert(handlerPair);
+
+ return id;
+}
+
+
+void dispatcher::removeHandler(handler id)
+{
+ std::pair<dispatcher_impl::handler_it_t,dispatcher_impl::handler_it_t>
+ handlers(impl->handlers.equal_range(id));
+
+ dispatcher_impl::handler_it_t i;
+ for (i = handlers.first; i != handlers.second; i++)
+ {
+ dispatcher_impl::callback_it_t it = impl->callbacks.find((*i).second);
+ dispatcher_impl::callback_it_t last = impl->callbacks.end();
+
+ dispatcher_impl::callback_it_t j;
+ for (j = it; j != last; j++)
+ {
+ if (((*j).second).first == id)
+ {
+ impl->callbacks.erase(j);
+ break;
+ }
+ }
+ }
+
+ impl->handlers.erase(id);
+}
+
+
+void dispatcher::dispatch(const std::string& message)
+{
+ dispatch(message, notification());
+}
+
+void dispatcher::dispatch(const std::string& message, const notification& param)
+{
+ std::pair<dispatcher_impl::callback_it_t,dispatcher_impl::callback_it_t>
+ callbacks(impl->callbacks.equal_range(message));
+
+ dispatcher_impl::callback_it_t i;
+ for (i = callbacks.first; i != callbacks.second; i++)
+ {
+ function callback = ((*i).second).second;
+ callback(param);
+ }
+}
+
+
+} // namespace dc
+