4911c12367ccdd8a28ba11929c8e5a4b0eeab9e3
[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 public:
38
39 /**
40 * Interface for a notification class.
41 */
42
43 class Message
44 {
45 public:
46 virtual ~Message() {};
47 };
48
49
50 class Handle
51 {
52 public:
53
54 Handle() :
55 mId(0) {}
56
57 Handle(boost::weak_ptr<Impl> dispatch, unsigned id) :
58 mDispatch(dispatch),
59 mId(id) {}
60
61 Handle(const Handle& handle) :
62 mDispatch(handle.mDispatch),
63 mId(handle.mId)
64 {
65 handle.mId = 0;
66 }
67
68 ~Handle()
69 {
70 clear();
71 }
72
73 Handle& operator = (const Handle& handle)
74 {
75 clear();
76 mDispatch = handle.mDispatch;
77 mId = handle.mId;
78 handle.mId = 0;
79 return *this;
80 }
81
82 unsigned getId() const
83 {
84 return mId;
85 }
86
87 void clear();
88
89 private:
90
91 boost::weak_ptr<Impl> mDispatch;
92 mutable unsigned mId;
93 };
94
95 typedef boost::function<void(const Message*)> Function;
96
97
98 Dispatch();
99
100 Handle addTarget(const std::string& event, const Function& callback);
101 Handle addTarget(const std::string& event, const Function& callback,
102 Handle handle);
103
104 void dispatch(const std::string& event, const Message* message = 0);
105
106 static Dispatch& global();
107 };
108
109
110 } // namespace Mf
111
112 #endif // _MOOF_DISPATCH_HH_
113
This page took 0.032902 seconds and 3 git commands to generate.