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