sockets documentation and cleanup
[chaz/yoink] / src / Moof / Dispatch.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 #include <string>
16
17 #include <boost/bind.hpp>
18 #include <boost/function.hpp>
19 #include <boost/shared_ptr.hpp>
20 #include <boost/weak_ptr.hpp>
21
22
23 namespace Mf {
24
25
26 /**
27 * Dispatcher of messages to interested parties.
28 */
29
30 class Dispatch
31 {
32 class Impl;
33 boost::shared_ptr<Impl> mImpl;
34
35 void removeTarget(unsigned id);
36
37
38 public:
39
40 class Handle
41 {
42 public:
43
44 Handle() :
45 mId(0) {}
46
47 Handle(boost::weak_ptr<Impl> dispatch, unsigned id) :
48 mDispatch(dispatch),
49 mId(id) {}
50
51 Handle(const Handle& handle) :
52 mDispatch(handle.mDispatch),
53 mId(handle.mId)
54 {
55 handle.mId = 0;
56 }
57
58 ~Handle()
59 {
60 clear();
61 }
62
63 Handle& operator = (const Handle& handle)
64 {
65 clear();
66 mDispatch = handle.mDispatch;
67 mId = handle.mId;
68 handle.mId = 0;
69 return *this;
70 }
71
72 unsigned getId() const
73 {
74 return mId;
75 }
76
77 void clear();
78
79 private:
80
81 boost::weak_ptr<Impl> mDispatch;
82 mutable unsigned mId;
83 };
84
85 typedef boost::function<void(void)> Function;
86
87
88 Dispatch();
89
90 Handle addTarget(const std::string& event, const Function& callback);
91 Handle addTarget(const std::string& event, const Function& callback,
92 Handle handle);
93
94 void dispatch(const std::string& event);
95
96 static Dispatch& global();
97 };
98
99
100 } // namespace Mf
101
102 #endif // _MOOF_DISPATCH_HH_
103
This page took 0.032939 seconds and 4 git commands to generate.