presage 0.9.1
dispatcher.h
Go to the documentation of this file.
1
2/******************************************************
3 * Presage, an extensible predictive text entry system
4 * ---------------------------------------------------
5 *
6 * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 **********(*)*/
23
24
25#ifndef PRESAGE_DISPATCHER
26#define PRESAGE_DISPATCHER
27
28#if HAVE_CONFIG_H
29# include <config.h>
30#endif
31
32#include <map>
33#include <string>
34#include <iostream>
35
36#include "observable.h"
37
41template <class class_t>
43public:
44 typedef void (class_t::* mbr_func_ptr_t) (const std::string& value);
45 typedef std::map<std::string, mbr_func_ptr_t> dispatch_map_t;
46
47 Dispatcher(class_t* obj)
48 {
49 object = obj;
50 }
51
53 {
54 for (std::list<Observable*>::iterator it = observables.begin();
55 it != observables.end();
56 it++) {
57 (*it)->detach (object);
58 }
59
60 }
61
62 void map (Observable* var, const mbr_func_ptr_t& ptr)
63 {
64 var->attach (object);
65 observables.push_back (var);
66 dispatch_map[var->get_name ()] = ptr;
67 dispatch (var);
68
69 //std::cerr << "[Dispatcher] mapped " << var->string() << " => "
70 // << object << "->" << ptr << std::endl;
71 }
72
73 void dispatch (const Observable* var)
74 {
75 mbr_func_ptr_t handler_ptr = dispatch_map[var->get_name ()];
76 if (handler_ptr) {
77 ((object)->*(handler_ptr)) (var->get_value ());
78 } else {
79 std::cerr << "[Dispatcher] Unable to handle notification from observable: "
80 << var->get_name () << " - " << var->get_value() << std::endl;
81 }
82 }
83
84private:
85 class_t* object;
87 std::list<Observable*> observables;
88
89};
90
91#endif // PRESAGE_DISPATCHER
void dispatch(const Observable *var)
Definition: dispatcher.h:73
class_t * object
Definition: dispatcher.h:85
void(class_t::* mbr_func_ptr_t)(const std::string &value)
Definition: dispatcher.h:44
Dispatcher(class_t *obj)
Definition: dispatcher.h:47
std::list< Observable * > observables
Definition: dispatcher.h:87
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:62
dispatch_map_t dispatch_map
Definition: dispatcher.h:86
std::map< std::string, mbr_func_ptr_t > dispatch_map_t
Definition: dispatcher.h:45
virtual void attach(Observer *observer)
Definition: observable.cpp:34
virtual std::string get_name() const =0
virtual std::string get_value() const =0