]> Dogcows Code - chaz/yoink/blobdiff - src/moof/dispatcher.hh
the massive refactoring effort
[chaz/yoink] / src / moof / dispatcher.hh
diff --git a/src/moof/dispatcher.hh b/src/moof/dispatcher.hh
new file mode 100644 (file)
index 0000000..a4207c4
--- /dev/null
@@ -0,0 +1,108 @@
+
+/*]  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_
+
+/**
+ * \file dispatcher.hh
+ * Classes that deal with message dispatching.
+ */
+
+#include <string>
+
+#include <boost/bind.hpp>
+#include <boost/function.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/weak_ptr.hpp>
+
+
+namespace moof {
+
+
+/**
+ * Dispatcher of messages to interested parties.
+ */
+
+class dispatcher
+{
+       class impl;
+       boost::shared_ptr<impl> impl_;
+
+       void remove_target(unsigned id);
+
+
+public:
+
+       class handle
+       {
+       public:
+
+               handle() :
+                       id_(0) {}
+
+               handle(boost::weak_ptr<impl> 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<impl>   dispatcher_;
+               mutable unsigned                id_;
+       };
+
+       typedef boost::function<void(void)> 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_
+
This page took 0.022087 seconds and 4 git commands to generate.