/*] Copyright (c) 2009-2010, Charles McGarvey [************************** **] All rights reserved. * * vi:ts=4 sw=4 tw=75 * * Distributable under the terms and conditions of the 2-clause BSD license; * see the file COPYING for a complete text of the license. * **************************************************************************/ #ifndef _MOOF_DISPATCH_HH_ #define _MOOF_DISPATCH_HH_ #include #include #include #include #include namespace Mf { /** * Dispatcher of messages to interested parties. */ class Dispatch { class Impl; boost::shared_ptr mImpl; void removeTarget(unsigned id); public: class Handle { public: Handle() : mId(0) {} Handle(boost::weak_ptr dispatch, unsigned id) : mDispatch(dispatch), mId(id) {} Handle(const Handle& handle) : mDispatch(handle.mDispatch), mId(handle.mId) { handle.mId = 0; } ~Handle() { clear(); } Handle& operator = (const Handle& handle) { clear(); mDispatch = handle.mDispatch; mId = handle.mId; handle.mId = 0; return *this; } unsigned getId() const { return mId; } void clear(); private: boost::weak_ptr mDispatch; mutable unsigned mId; }; typedef boost::function Function; Dispatch(); 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); static Dispatch& global(); }; } // namespace Mf #endif // _MOOF_DISPATCH_HH_