X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fdispatcher.cc;fp=src%2Fdispatcher.cc;h=0000000000000000000000000000000000000000;hb=c2321281bf12a7efaedde930422c7ddbc92080d4;hp=d42077a2ec8b20a300051ae6f69e87b43ee4d775;hpb=87bc17e55b0c1dc73ecc66df856d3f08fd7a7724;p=chaz%2Fyoink diff --git a/src/dispatcher.cc b/src/dispatcher.cc deleted file mode 100644 index d42077a..0000000 --- a/src/dispatcher.cc +++ /dev/null @@ -1,139 +0,0 @@ - -/******************************************************************************* - - 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::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::dispatcher_impl) {} - - -// TODO these methods are ugly - -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::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::dispatcher_impl::handler_it_t i; - for (i = handlers.first; i != handlers.second; i++) - { - dispatcher::dispatcher_impl::callback_it_t it = impl->callbacks.find((*i).second); - dispatcher::dispatcher_impl::callback_it_t last = impl->callbacks.end(); - - dispatcher::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::dispatcher_impl::callback_it_t i; - for (i = callbacks.first; i != callbacks.second; i++) - { - function callback = ((*i).second).second; - callback(param); - } -} - - -} // namespace dc - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ -