X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FDispatcher.cc;h=fd93ccfb3948c249e543882b47fde522c8330ca6;hp=e0d3d21bb839668593629818e281c7ff1f5ed9b3;hb=5fa5f117f28922a7e539a432367960c1a61f837d;hpb=493ddb59a8620b49dfa0ff62ce93395ebfd02e86 diff --git a/src/Moof/Dispatcher.cc b/src/Moof/Dispatcher.cc index e0d3d21..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,78 +55,85 @@ 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 it; - for (it = handlers.first; it != handlers.second; ++it) - { - Dispatcher::DispatcherImpl::CallbackIter first = impl_->callbacks.find((*it).second); - Dispatcher::DispatcherImpl::CallbackIter last = impl_->callbacks.end(); - - Dispatcher::DispatcherImpl::CallbackIter jt; - for (jt = first; jt != last; ++jt) - { - if (((*jt).second).first == id) - { - impl_->callbacks.erase(jt); - 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 it; - for (it = callbacks.first; it != callbacks.second; ++it) + for (Impl::CallbackIter it = callbacks.first; it != callbacks.second; ++it) { Function callback = ((*it).second).second; callback(param);