]> Dogcows Code - chaz/yoink/blob - src/Moof/Dispatch.hh
dispatch class not a singleton, engine is static
[chaz/yoink] / src / Moof / Dispatch.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_DISPATCH_HH_
30 #define _MOOF_DISPATCH_HH_
31
32 #include <string>
33
34 #include <boost/bind.hpp>
35 #include <boost/function.hpp>
36 #include <boost/shared_ptr.hpp>
37
38
39 namespace Mf {
40
41
42 /**
43 * Dispatcher of messages to interested parties.
44 */
45
46 class Dispatch
47 {
48 class Impl;
49 boost::shared_ptr<Impl> mImpl;
50
51 void removeHandler(unsigned id);
52
53 public:
54
55 /**
56 * Interface for a notification class.
57 */
58
59 class Message
60 {
61 public:
62 virtual ~Message() {};
63 };
64
65
66 class Handler
67 {
68 public:
69
70 Handler() :
71 mDispatch(0),
72 mId(0) {}
73
74 Handler(Impl* dispatch, unsigned id) :
75 mDispatch(dispatch),
76 mId(id) {}
77
78 Handler(const Handler& handler) :
79 mDispatch(handler.mDispatch),
80 mId(handler.mId)
81 {
82 handler.mId = 0;
83 }
84
85 ~Handler();
86
87 Handler& operator = (const Handler& handler)
88 {
89 mDispatch = handler.mDispatch;
90 mId = handler.mId;
91 handler.mId = 0;
92 return *this;
93 }
94
95 unsigned getId() const
96 {
97 return mId;
98 }
99
100 private:
101
102 Impl* mDispatch;
103 mutable unsigned mId;
104
105 };
106
107 typedef boost::function<void(const Message*)> Function;
108
109
110 Dispatch();
111
112 Handler addHandler(const std::string& event, const Function& callback);
113 Handler addHandler(const std::string& event, const Function& callback,
114 Handler handler);
115
116 void dispatch(const std::string& event, const Message* message = 0);
117 };
118
119
120 } // namespace Mf
121
122 #endif // _MOOF_DISPATCH_HH_
123
124 /** vim: set ts=4 sw=4 tw=80: *************************************************/
125
This page took 0.042665 seconds and 5 git commands to generate.