/*] Copyright (c) 2009-2011, Charles McGarvey [***************************** **] All rights reserved. * * 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 /** * \file dispatcher.hh * Classes that deal with message dispatching. */ namespace moof { /** * Dispatcher of messages to interested parties. */ class dispatcher { class impl; boost::shared_ptr impl_; void remove_target(unsigned id); public: class handle { public: handle() : id_(0) {} handle(boost::weak_ptr dispatcher, unsigned id) : dispatcher_(dispatcher), id_(id) {} handle(const handle& handle) : dispatcher_(handle.dispatcher_), id_(handle.id_) { handle.id_ = 0; } ~handle() { clear(); } handle& operator = (const handle& handle) { clear(); dispatcher_ = handle.dispatcher_; id_ = handle.id_; handle.id_ = 0; return *this; } unsigned id() const { return id_; } void clear(); private: boost::weak_ptr dispatcher_; mutable unsigned id_; }; typedef boost::function function; dispatcher(); handle add_target(const std::string& event, const function& callback); handle add_target(const std::string& event, const function& callback, handle handle); void dispatch(const std::string& event); static dispatcher& global(); }; } // namespace moof #endif // _MOOF_DISPATCH_HH_