]> Dogcows Code - chaz/yoink/blob - src/moof/dispatcher.cc
remove some unused stlplus modules
[chaz/yoink] / src / moof / dispatcher.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #include <map>
11
12 #include "dispatcher.hh"
13
14
15 namespace moof {
16
17
18 class dispatcher::impl
19 {
20 public:
21
22 impl(dispatcher* dispatcher) :
23 dispatcher_(dispatcher),
24 id_(0) {}
25
26 dispatcher::handle new_handle()
27 {
28 dispatcher::handle handle(dispatcher_->impl_, --id_);
29 return handle;
30 }
31
32 typedef std::pair<unsigned,dispatcher::function> callback;
33 typedef std::multimap<std::string,callback> callback_lookup;
34 typedef callback_lookup::iterator callback_it;
35
36 typedef std::multimap<unsigned,std::string> handle_lookup;
37 typedef handle_lookup::iterator handle_it;
38
39 handle add_target(const std::string& event,
40 const function& callback, handle handle)
41 {
42 callbacks_.insert(std::make_pair(event,
43 std::make_pair(handle.id(), callback)));
44 handles_.insert(std::make_pair(handle.id(), event));
45 return handle;
46 }
47
48 void remove_target(unsigned id)
49 {
50 std::pair<handle_it,handle_it> matching(handles_.equal_range(id));
51
52 for (handle_it it = matching.first;
53 it != matching.second; ++it)
54 {
55 callback_it first = callbacks_.find((*it).second);
56 callback_it last = callbacks_.end();
57
58 for (callback_it jt = first; jt != last; ++jt)
59 {
60 if ((*jt).second.first == id)
61 {
62 callbacks_.erase(jt);
63 break;
64 }
65 }
66 }
67
68 handles_.erase(id);
69 }
70
71 void dispatch(const std::string& event)
72 {
73 std::pair<callback_it,callback_it>
74 callbacks(callbacks_.equal_range(event));
75
76 for (callback_it it = callbacks.first; it != callbacks.second; ++it)
77 {
78 function callback = (*it).second.second;
79 callback();
80 }
81 }
82
83 dispatcher* dispatcher_;
84 unsigned id_;
85
86 callback_lookup callbacks_;
87 handle_lookup handles_;
88 };
89
90 void dispatcher::handle::clear()
91 {
92 boost::shared_ptr<impl> dispatcher;
93 if (id_ && (dispatcher = dispatcher_.lock()))
94 {
95 dispatcher->remove_target(id_);
96 id_ = 0;
97 }
98 }
99
100
101 dispatcher::dispatcher() :
102 impl_(new dispatcher::impl(this)) {}
103
104 dispatcher::handle
105 dispatcher::add_target(const std::string& event, const function& callback)
106 {
107 return add_target(event, callback, impl_->new_handle());
108 }
109
110 dispatcher::handle dispatcher::add_target(const std::string& event,
111 const function& callback, handle handle)
112 {
113 // pass through
114 return impl_->add_target(event, callback, handle);
115 }
116
117 void dispatcher::remove_target(unsigned id)
118 {
119 // pass through
120 return impl_->remove_target(id);
121 }
122
123 void dispatcher::dispatch(const std::string& event)
124 {
125 // pass through
126 impl_->dispatch(event);
127 }
128
129 dispatcher& dispatcher::global()
130 {
131 static dispatcher dispatcher;
132 return dispatcher;
133 }
134
135
136 } // namespace moof
137
This page took 0.034644 seconds and 4 git commands to generate.