X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fdispatcher.cc;fp=src%2Fdispatcher.cc;h=509b69d151bd039e5bf85d8f1d717e172c6321e7;hb=79b5f738f2e38acb60cda7e09f54802933a17105;hp=0000000000000000000000000000000000000000;hpb=a891a2dcbbb63d9e771da6efff00a33da614e737;p=chaz%2Fyoink diff --git a/src/dispatcher.cc b/src/dispatcher.cc new file mode 100644 index 0000000..509b69d --- /dev/null +++ b/src/dispatcher.cc @@ -0,0 +1,135 @@ + +/******************************************************************************* + + 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 + +#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 callback_t; + typedef std::multimap callback_lookup_t; + typedef callback_lookup_t::iterator callback_it_t; + + typedef std::multimap 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 + callbackPair(message, dispatcher_impl::callback_t(id, callback)); + + std::pair handlerPair(id, message); + + impl->callbacks.insert(callbackPair); + impl->handlers.insert(handlerPair); + + return id; +} + + +void dispatcher::removeHandler(handler id) +{ + std::pair + 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 + 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 +