]> Dogcows Code - chaz/yoink/blob - src/Moof/Dispatch.cc
reformatting
[chaz/yoink] / src / Moof / Dispatch.cc
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 #include <map>
13
14 #include "Dispatch.hh"
15
16
17 namespace Mf {
18
19
20 class Dispatch::Impl
21 {
22 public:
23
24 Impl() :
25 mId(0) {}
26
27 Dispatch::Handler getNewHandler()
28 {
29 ++mId;
30 //return Dispatch::Handler(this, mId);
31 Dispatch::Handler handler(this, mId);
32 return handler;
33 }
34
35 typedef std::pair<unsigned,Dispatch::Function> Callback;
36 typedef std::multimap<std::string,Callback> CallbackLookup;
37 typedef CallbackLookup::iterator CallbackIter;
38
39 typedef std::multimap<unsigned,std::string> HandlerLookup;
40 typedef HandlerLookup::iterator HandlerIter;
41
42
43 inline Handler addHandler(const std::string& event,
44 const Function& callback, Handler handler)
45 {
46 mCallbacks.insert(std::make_pair(event,
47 std::make_pair(handler.getId(), callback)));
48 mHandlers.insert(std::make_pair(handler.getId(), event));
49
50 return handler;
51 }
52
53 inline void removeHandler(unsigned id)
54 {
55 std::pair<HandlerIter,HandlerIter>
56 matching(mHandlers.equal_range(id));
57
58 for (HandlerIter it = matching.first; it != matching.second; ++it)
59 {
60 CallbackIter first = mCallbacks.find((*it).second);
61 CallbackIter last = mCallbacks.end();
62
63 for (CallbackIter jt = first; jt != last; ++jt)
64 {
65 if ((*jt).second.first == id)
66 {
67 mCallbacks.erase(jt);
68 break;
69 }
70 }
71 }
72
73 mHandlers.erase(id);
74 }
75
76 void dispatch(const std::string& event, const Message* message)
77 {
78 std::pair<CallbackIter,CallbackIter>
79 callbacks(mCallbacks.equal_range(event));
80
81 for (CallbackIter it = callbacks.first; it != callbacks.second;
82 ++it)
83 {
84 Function callback = (*it).second.second;
85 callback(message);
86 }
87 }
88
89
90 unsigned mId;
91
92 CallbackLookup mCallbacks;
93 HandlerLookup mHandlers;
94 };
95
96
97 Dispatch::Handler::~Handler()
98 {
99 if (mId)
100 {
101 mDispatch->removeHandler(mId);
102 }
103 }
104
105
106 Dispatch::Dispatch() :
107 mImpl(new Dispatch::Impl) {}
108
109
110 Dispatch::Handler Dispatch::addHandler(const std::string& event,
111 const Function& callback)
112 {
113 return addHandler(event, callback, mImpl->getNewHandler());
114 }
115
116 Dispatch::Handler Dispatch::addHandler(const std::string& event,
117 const Function& callback,
118 Handler handler)
119 {
120 // pass through
121 return mImpl->addHandler(event, callback, handler);
122 }
123
124
125 void Dispatch::removeHandler(unsigned id)
126 {
127 // pass through
128 return mImpl->removeHandler(id);
129 }
130
131
132 void Dispatch::dispatch(const std::string& event, const Message* message)
133 {
134 // pass through
135 mImpl->dispatch(event, message);
136 }
137
138
139 } // namespace Mf
140
This page took 0.038928 seconds and 4 git commands to generate.