]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Dispatcher.cc
cleaned up dispatcher
[chaz/yoink] / src / Moof / Dispatcher.cc
index e0d3d21bb839668593629818e281c7ff1f5ed9b3..9e652a37a8983614010cf974f92172c310caae88 100644 (file)
@@ -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,76 @@ public:
        typedef std::multimap<Dispatcher::Handler,std::string>          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<HandlerIter,HandlerIter> 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) {}
 
 
-// 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<std::string,Dispatcher::DispatcherImpl::Callback>
-               callbackPair(message, Dispatcher::DispatcherImpl::Callback(id, callback));
-
-       std::pair<Handler,std::string> 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<Dispatcher::DispatcherImpl::HandlerIter,Dispatcher::DispatcherImpl::HandlerIter>
-               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<Dispatcher::DispatcherImpl::CallbackIter,Dispatcher::DispatcherImpl::CallbackIter>
+       std::pair<Impl::CallbackIter,Impl::CallbackIter>
                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);
This page took 0.021471 seconds and 4 git commands to generate.