X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FDispatch.hh;h=4911c12367ccdd8a28ba11929c8e5a4b0eeab9e3;hp=fd328fe37d733a49dba95f84176a68f279923e99;hb=e0c0a3b5e7337cde55e520801d2e59e03dc97d9c;hpb=ed5fcf5f1357fc42749408f705e9ec55531ff006 diff --git a/src/Moof/Dispatch.hh b/src/Moof/Dispatch.hh index fd328fe..4911c12 100644 --- a/src/Moof/Dispatch.hh +++ b/src/Moof/Dispatch.hh @@ -17,6 +17,7 @@ #include #include #include +#include namespace Mf { @@ -31,7 +32,7 @@ class Dispatch class Impl; boost::shared_ptr mImpl; - void removeHandler(unsigned id); + void removeTarget(unsigned id); public: @@ -46,32 +47,35 @@ public: }; - class Handler + class Handle { public: - Handler() : - mDispatch(0), + Handle() : mId(0) {} - Handler(Impl* dispatch, unsigned id) : + Handle(boost::weak_ptr dispatch, unsigned id) : mDispatch(dispatch), mId(id) {} - Handler(const Handler& handler) : - mDispatch(handler.mDispatch), - mId(handler.mId) + Handle(const Handle& handle) : + mDispatch(handle.mDispatch), + mId(handle.mId) { - handler.mId = 0; + handle.mId = 0; } - ~Handler(); + ~Handle() + { + clear(); + } - Handler& operator = (const Handler& handler) + Handle& operator = (const Handle& handle) { - mDispatch = handler.mDispatch; - mId = handler.mId; - handler.mId = 0; + clear(); + mDispatch = handle.mDispatch; + mId = handle.mId; + handle.mId = 0; return *this; } @@ -79,12 +83,13 @@ public: { return mId; } + + void clear(); private: - Impl* mDispatch; - mutable unsigned mId; - + boost::weak_ptr mDispatch; + mutable unsigned mId; }; typedef boost::function Function; @@ -92,11 +97,13 @@ public: Dispatch(); - Handler addHandler(const std::string& event, const Function& callback); - Handler addHandler(const std::string& event, const Function& callback, - Handler handler); + Handle addTarget(const std::string& event, const Function& callback); + Handle addTarget(const std::string& event, const Function& callback, + Handle handle); void dispatch(const std::string& event, const Message* message = 0); + + static Dispatch& global(); };