]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Dispatch.hh
foundational changes; tying up some loose ends
[chaz/yoink] / src / Moof / Dispatch.hh
similarity index 54%
rename from src/Moof/Dispatcher.hh
rename to src/Moof/Dispatch.hh
index 7945752df0b21ff3b97aa2104ee1b6bc1c3e4c0b..bbf37f3c3cf31baad892a6a0c2eb26a520d243c1 100644 (file)
@@ -26,8 +26,8 @@
 
 *******************************************************************************/
 
-#ifndef _MOOF_DISPATCHER_HH_
-#define _MOOF_DISPATCHER_HH_
+#ifndef _MOOF_DISPATCH_HH_
+#define _MOOF_DISPATCH_HH_
 
 #include <string>
 
@@ -40,79 +40,90 @@ namespace Mf {
 
 
 /**
- * Interface for a notification class.
+ * Dispatcher of messages to interested parties.
  */
 
-class Notification
+class Dispatch
 {
+       class Impl;
+       boost::shared_ptr<Impl> mImpl;
+
+       void removeHandler(unsigned id);
+
 public:
-       virtual ~Notification() {};
-};
 
+       /**
+        * Interface for a notification class.
+        */
 
-/**
- * Dispatcher of notifications to interested parties.
- */
+       class Message
+       {
+       public:
+               virtual ~Message() {};
+       };
 
-class Dispatcher
-{
-       class Impl;
-       boost::shared_ptr<Impl> mImpl;
 
-public:
+       class Handler
+       {
+       public:
 
-       // TODO - the Handler would be even better as an object which automagically
-       // removes itself from the dispatcher on destruction, so users don't have to
-       // worry about forgetting
-       typedef void* Handler;
-       typedef boost::function<void(const Notification*)> Function;
+               Handler() :
+                       mDispatch(0),
+                       mId(0) {}
 
-       Dispatcher();
-       ~Dispatcher();
+               Handler(Impl* dispatch, unsigned id) :
+                       mDispatch(dispatch),
+                       mId(id) {}
 
-       // get global instance
-       static Dispatcher& getInstance();
+               Handler(const Handler& handler) :
+                       mDispatch(handler.mDispatch),
+                       mId(handler.mId)
+               {
+                       handler.mId = 0;
+               }
 
-       Handler addHandler(const std::string& message, const Function& callback);
-       Handler addHandler(const std::string& message, const Function& callback,
-                       Handler id);
+               ~Handler();
 
-       void removeHandler(Handler id);
+               Handler& operator = (const Handler& handler)
+               {
+                       mDispatch = handler.mDispatch;
+                       mId = handler.mId;
+                       handler.mId = 0;
+                       return *this;
+               }
 
-       void dispatch(const std::string& message, const Notification* param = 0);
-};
+               unsigned getId() const
+               {
+                       return mId;
+               }
+       
+       private:
 
+               Impl*                           mDispatch;
+               mutable unsigned        mId;
 
-namespace dispatcher {
+       };
 
-inline Dispatcher::Handler addHandler(const std::string& message,
-               const Dispatcher::Function& callback)
-{
-       return Dispatcher::getInstance().addHandler(message, callback);
-}
+       typedef boost::function<void(const Message*)> Function;
 
-inline Dispatcher::Handler addHandler(const std::string& message,
-               const Dispatcher::Function& callback, Dispatcher::Handler id)
-{
-       return Dispatcher::getInstance().addHandler(message, callback, id);
-}
 
-inline void removeHandler(Dispatcher::Handler id)
-{
-       Dispatcher::getInstance().removeHandler(id);
-}
+       Dispatch();
+       ~Dispatch();
 
-inline void dispatch(const std::string& message, const Notification* param = 0)
-{
-       Dispatcher::getInstance().dispatch(message, param);
-}
+       // create and/or get a global instance
+       static Dispatch& getInstance();
 
-} // namespace dispatcher
+       Handler addHandler(const std::string& event, const Function& callback);
+       Handler addHandler(const std::string& event, const Function& callback,
+                       Handler handler);
+
+       void dispatch(const std::string& event, const Message* message = 0);
+};
 
 
 } // namespace Mf
 
-#endif // _MOOF_DISPATCHER_HH_
+#endif // _MOOF_DISPATCH_HH_
 
 /** vim: set ts=4 sw=4 tw=80: *************************************************/
 
This page took 0.025126 seconds and 4 git commands to generate.