#include <string>
-#include <boost/shared_ptr.hpp>
+#include <boost/bind.hpp>
#include <boost/function.hpp>
-
-#include <Moof/Singleton.hh>
+#include <boost/shared_ptr.hpp>
namespace Mf {
class Notification
{
public:
- virtual ~Notification();
+ virtual ~Notification() {};
};
* Dispatcher of notifications to interested parties.
*/
-class Dispatcher : public Singleton<Dispatcher>
+class Dispatcher
{
+ class Impl;
+ boost::shared_ptr<Impl> mImpl;
+
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;
+ typedef boost::function<void(const Notification*)> Function;
Dispatcher();
+ ~Dispatcher();
+
+ // get global instance
+ static Dispatcher& getInstance();
Handler addHandler(const std::string& message, const Function& callback);
Handler addHandler(const std::string& message, const Function& callback,
void removeHandler(Handler id);
- void dispatch(const std::string& message);
- void dispatch(const std::string& message, const Notification& param);
-
-private:
- class DispatcherImpl;
- boost::shared_ptr<DispatcherImpl> impl_;
+ void dispatch(const std::string& message, const Notification* param = 0);
};
+namespace dispatcher {
+
+inline Dispatcher::Handler addHandler(const std::string& message,
+ const Dispatcher::Function& callback)
+{
+ return Dispatcher::getInstance().addHandler(message, callback);
+}
+
+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);
+}
+
+inline void dispatch(const std::string& message, const Notification* param = 0)
+{
+ Dispatcher::getInstance().dispatch(message, param);
+}
+
+} // namespace dispatcher
+
+
} // namespace Mf
#endif // _MOOF_DISPATCHER_HH_