X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FDispatcher.cc;h=fd93ccfb3948c249e543882b47fde522c8330ca6;hp=fa7b3281818ca6a06a4f07753638c4509209d04a;hb=5fa5f117f28922a7e539a432367960c1a61f837d;hpb=c2321281bf12a7efaedde930422c7ddbc92080d4 diff --git a/src/Moof/Dispatcher.cc b/src/Moof/Dispatcher.cc index fa7b328..fd93ccf 100644 --- a/src/Moof/Dispatcher.cc +++ b/src/Moof/Dispatcher.cc @@ -37,14 +37,14 @@ namespace Mf { Notification::~Notification() {} -class Dispatcher::DispatcherImpl +struct Dispatcher::Impl { -public: - DispatcherImpl() : id(1) {} + Impl() : + id(1) {} - Dispatcher::Handler getNewHandlerID() + Dispatcher::Handler getNewHandler() { - id += 4; + id += 2; return (Dispatcher::Handler)id; } @@ -55,80 +55,87 @@ public: typedef std::multimap HandlerLookup; typedef HandlerLookup::iterator HandlerIter; - unsigned long long id; - CallbackLookup callbacks; - HandlerLookup handlers; + inline Handler addHandler(const std::string& message, + const Function& callback, Handler id) + { + callbacks.insert(std::make_pair(message, std::make_pair(id, callback))); + handlers.insert(std::make_pair(id, message)); + + return id; + } + + inline void removeHandler(Handler id) + { + std::pair matching(handlers.equal_range(id)); + + for (HandlerIter it = matching.first; it != matching.second; ++it) + { + CallbackIter first = callbacks.find((*it).second); + CallbackIter last = callbacks.end(); + + for (CallbackIter jt = first; jt != last; ++jt) + { + if (((*jt).second).first == id) + { + callbacks.erase(jt); + break; + } + } + } + + handlers.erase(id); + } + + unsigned long id; + + CallbackLookup callbacks; + HandlerLookup handlers; }; Dispatcher::Dispatcher() : - impl_(new Dispatcher::DispatcherImpl) {} + impl_(new Dispatcher::Impl) {} +Dispatcher::~Dispatcher() {} + + +Dispatcher& Dispatcher::getInstance() +{ + static Dispatcher dispatcher; + return dispatcher; +} -// TODO these methods are ugly Dispatcher::Handler Dispatcher::addHandler(const std::string& message, const Function& callback) { - return addHandler(message, callback, impl_->getNewHandlerID()); + return addHandler(message, callback, impl_->getNewHandler()); } Dispatcher::Handler Dispatcher::addHandler(const std::string& message, const Function& callback, Handler id) { - std::pair - callbackPair(message, Dispatcher::DispatcherImpl::Callback(id, callback)); - - std::pair handlerPair(id, message); - - impl_->callbacks.insert(callbackPair); - impl_->handlers.insert(handlerPair); - - return id; + // pass through + return impl_->addHandler(message, callback, id); } void Dispatcher::removeHandler(Handler id) { - std::pair - handlers(impl_->handlers.equal_range(id)); - - Dispatcher::DispatcherImpl::HandlerIter i; - for (i = handlers.first; i != handlers.second; i++) - { - Dispatcher::DispatcherImpl::CallbackIter it = impl_->callbacks.find((*i).second); - Dispatcher::DispatcherImpl::CallbackIter last = impl_->callbacks.end(); - - Dispatcher::DispatcherImpl::CallbackIter j; - for (j = it; j != last; j++) - { - if (((*j).second).first == id) - { - impl_->callbacks.erase(j); - break; - } - } - } - - impl_->handlers.erase(id); + // pass through + return impl_->removeHandler(id); } -void Dispatcher::dispatch(const std::string& message) -{ - dispatch(message, Notification()); -} - -void Dispatcher::dispatch(const std::string& message, const Notification& param) +void Dispatcher::dispatch(const std::string& message, const Notification* param) { - std::pair + std::pair callbacks(impl_->callbacks.equal_range(message)); - Dispatcher::DispatcherImpl::CallbackIter i; - for (i = callbacks.first; i != callbacks.second; i++) + for (Impl::CallbackIter it = callbacks.first; it != callbacks.second; ++it) { - Function callback = ((*i).second).second; + Function callback = ((*it).second).second; callback(param); } }