X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FDispatcher.hh;h=7945752df0b21ff3b97aa2104ee1b6bc1c3e4c0b;hp=ec143b46fdce105a7021f79cc4a2ecb1027144f4;hb=c9e20ac06383b20ceb5404c9237e319c2e90d157;hpb=c2321281bf12a7efaedde930422c7ddbc92080d4 diff --git a/src/Moof/Dispatcher.hh b/src/Moof/Dispatcher.hh index ec143b4..7945752 100644 --- a/src/Moof/Dispatcher.hh +++ b/src/Moof/Dispatcher.hh @@ -31,10 +31,9 @@ #include -#include +#include #include - -#include +#include namespace Mf { @@ -47,7 +46,7 @@ namespace Mf { class Notification { public: - virtual ~Notification(); + virtual ~Notification() {}; }; @@ -55,13 +54,24 @@ public: * Dispatcher of notifications to interested parties. */ -class Dispatcher : public Singleton +class Dispatcher { + class Impl; + boost::shared_ptr 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 Function; + typedef boost::function 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, @@ -69,15 +79,37 @@ public: 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 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_