jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
event_dispatcher.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_CLUSTERING_EVENT_DISPATCHER_HPP_
18 #define JUBATUS_CORE_CLUSTERING_EVENT_DISPATCHER_HPP_
19 
20 #include <map>
21 #include <vector>
22 #include "jubatus/util/lang/function.h"
23 
24 namespace jubatus {
25 namespace core {
26 namespace clustering {
27 
28 // TODO(beam2d): Delete it or move it to jubatus::common.
29 template <typename EventType, typename EventData>
31  typedef jubatus::util::lang::function<void (const EventData&)> callback_t;
32  typedef std::vector<callback_t> callbacks_t;
33  typedef std::map<EventType, callbacks_t> event_type_table;
34 
35  public:
36  virtual ~event_dispatcher() {}
37 
38  void add_event_listener(const EventType& type, callback_t callback);
39 
40  protected:
41  void dispatch(const EventType& type, const EventData& data) const;
42  event_type_table events_;
43 };
44 
45 template <typename EventType, typename EventData>
47  const EventType& type,
48  callback_t callback) {
49  events_[type].push_back(callback);
50 }
51 
52 template <typename EventType, typename EventData>
54  const EventType& type,
55  const EventData& data) const {
56  typename event_type_table::const_iterator ev = events_.find(type);
57  if (ev == events_.end()) {
58  return;
59  }
60  typename callbacks_t::const_iterator it;
61  for (it = ev->second.begin(); it != ev->second.end(); ++it) {
62  (*it)(data);
63  }
64 }
65 
66 } // namespace clustering
67 } // namespace core
68 } // namespace jubatus
69 
70 #endif // JUBATUS_CORE_CLUSTERING_EVENT_DISPATCHER_HPP_
void dispatch(const EventType &type, const EventData &data) const
jubatus::util::lang::function< void(const EventData &)> callback_t
void add_event_listener(const EventType &type, callback_t callback)
std::map< EventType, callbacks_t > event_type_table