/*] 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 namespace Mf { /** * Dispatcher of messages to interested parties. */ class Dispatch { class Impl; boost::shared_ptr mImpl; void removeHandler(unsigned id); public: /** * Interface for a notification class. */ class Message { public: virtual ~Message() {}; }; class Handler { public: Handler() : mDispatch(0), mId(0) {} Handler(Impl* dispatch, unsigned id) : mDispatch(dispatch), mId(id) {} Handler(const Handler& handler) : mDispatch(handler.mDispatch), mId(handler.mId) { handler.mId = 0; } ~Handler(); Handler& operator = (const Handler& handler) { mDispatch = handler.mDispatch; mId = handler.mId; handler.mId = 0; return *this; } unsigned getId() const { return mId; } private: Impl* mDispatch; mutable unsigned mId; }; typedef boost::function Function; Dispatch(); 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_DISPATCH_HH_