]> Dogcows Code - chaz/yoink/blob - src/moof/dispatcher.hh
f2613e8fdeb2d11aba27b51c7758ef3e6fbdd989
[chaz/yoink] / src / moof / dispatcher.hh
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #ifndef _MOOF_DISPATCH_HH_
11 #define _MOOF_DISPATCH_HH_
12
13 /**
14 * \file dispatcher.hh
15 * Classes that deal with message dispatching.
16 */
17
18 #include <string>
19
20 #include <boost/bind.hpp>
21 #include <boost/function.hpp>
22 #include <boost/shared_ptr.hpp>
23 #include <boost/weak_ptr.hpp>
24
25
26 namespace moof {
27
28
29 /**
30 * Dispatcher of messages to interested parties.
31 */
32 class dispatcher
33 {
34 class impl;
35 boost::shared_ptr<impl> impl_;
36
37 void remove_target(unsigned id);
38
39 public:
40
41 class handle
42 {
43 public:
44
45 handle() :
46 id_(0) {}
47
48 handle(boost::weak_ptr<impl> dispatcher, unsigned id) :
49 dispatcher_(dispatcher),
50 id_(id) {}
51
52 handle(const handle& handle) :
53 dispatcher_(handle.dispatcher_),
54 id_(handle.id_)
55 {
56 handle.id_ = 0;
57 }
58
59 ~handle()
60 {
61 clear();
62 }
63
64 handle& operator = (const handle& handle)
65 {
66 clear();
67 dispatcher_ = handle.dispatcher_;
68 id_ = handle.id_;
69 handle.id_ = 0;
70 return *this;
71 }
72
73 unsigned id() const
74 {
75 return id_;
76 }
77
78 void clear();
79
80 private:
81
82 boost::weak_ptr<impl> dispatcher_;
83 mutable unsigned id_;
84 };
85
86 typedef boost::function<void(void)> function;
87
88 dispatcher();
89
90 handle add_target(const std::string& event, const function& callback);
91 handle add_target(const std::string& event, const function& callback,
92 handle handle);
93
94 void dispatch(const std::string& event);
95
96 static dispatcher& global();
97 };
98
99
100 } // namespace moof
101
102 #endif // _MOOF_DISPATCH_HH_
103
This page took 0.039198 seconds and 3 git commands to generate.