X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FDispatch.hh;fp=src%2FMoof%2FDispatcher.hh;h=bbf37f3c3cf31baad892a6a0c2eb26a520d243c1;hp=7945752df0b21ff3b97aa2104ee1b6bc1c3e4c0b;hb=e495074443d9fd7bc16137084cf9de3d031b75c4;hpb=c9e20ac06383b20ceb5404c9237e319c2e90d157 diff --git a/src/Moof/Dispatcher.hh b/src/Moof/Dispatch.hh similarity index 54% rename from src/Moof/Dispatcher.hh rename to src/Moof/Dispatch.hh index 7945752..bbf37f3 100644 --- a/src/Moof/Dispatcher.hh +++ b/src/Moof/Dispatch.hh @@ -26,8 +26,8 @@ *******************************************************************************/ -#ifndef _MOOF_DISPATCHER_HH_ -#define _MOOF_DISPATCHER_HH_ +#ifndef _MOOF_DISPATCH_HH_ +#define _MOOF_DISPATCH_HH_ #include @@ -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 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 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 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 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: *************************************************/