*******************************************************************************/
-#ifndef _MOOF_DISPATCHER_HH_
-#define _MOOF_DISPATCHER_HH_
+#ifndef _MOOF_DISPATCH_HH_
+#define _MOOF_DISPATCH_HH_
#include <string>
/**
- * 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: *************************************************/