]> Dogcows Code - chaz/yoink/blob - src/moof/dispatcher.hh
fixed documentation about where to find licenses
[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 #include <string>
14
15 #include <boost/bind.hpp>
16 #include <boost/function.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/weak_ptr.hpp>
19
20
21 /**
22 * \file dispatcher.hh
23 * Classes that deal with message dispatching.
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.034945 seconds and 4 git commands to generate.