]> Dogcows Code - chaz/yoink/blob - src/Moof/Dispatch.hh
fd328fe37d733a49dba95f84176a68f279923e99
[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
21
22 namespace Mf {
23
24
25 /**
26 * Dispatcher of messages to interested parties.
27 */
28
29 class Dispatch
30 {
31 class Impl;
32 boost::shared_ptr<Impl> mImpl;
33
34 void removeHandler(unsigned id);
35
36 public:
37
38 /**
39 * Interface for a notification class.
40 */
41
42 class Message
43 {
44 public:
45 virtual ~Message() {};
46 };
47
48
49 class Handler
50 {
51 public:
52
53 Handler() :
54 mDispatch(0),
55 mId(0) {}
56
57 Handler(Impl* dispatch, unsigned id) :
58 mDispatch(dispatch),
59 mId(id) {}
60
61 Handler(const Handler& handler) :
62 mDispatch(handler.mDispatch),
63 mId(handler.mId)
64 {
65 handler.mId = 0;
66 }
67
68 ~Handler();
69
70 Handler& operator = (const Handler& handler)
71 {
72 mDispatch = handler.mDispatch;
73 mId = handler.mId;
74 handler.mId = 0;
75 return *this;
76 }
77
78 unsigned getId() const
79 {
80 return mId;
81 }
82
83 private:
84
85 Impl* mDispatch;
86 mutable unsigned mId;
87
88 };
89
90 typedef boost::function<void(const Message*)> Function;
91
92
93 Dispatch();
94
95 Handler addHandler(const std::string& event, const Function& callback);
96 Handler addHandler(const std::string& event, const Function& callback,
97 Handler handler);
98
99 void dispatch(const std::string& event, const Message* message = 0);
100 };
101
102
103 } // namespace Mf
104
105 #endif // _MOOF_DISPATCH_HH_
106
This page took 0.036271 seconds and 3 git commands to generate.