dispatcher.h
Go to the documentation of this file.
/******************************************************
* Presage, an extensible predictive text entry system
* ---------------------------------------------------
*
* Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********(*)*/
#ifndef PRESAGE_DISPATCHER
#define PRESAGE_DISPATCHER
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <map>
#include <string>
#include <iostream>
#include "observable.h"
template <class class_t>
class Dispatcher {
public:
typedef void (class_t::* mbr_func_ptr_t) (const std::string& value);
typedef std::map<std::string, mbr_func_ptr_t> dispatch_map_t;
Dispatcher(class_t* obj)
{
object = obj;
}
~Dispatcher()
{
for (std::list<Observable*>::iterator it = observables.begin();
it != observables.end();
it++) {
(*it)->detach (object);
}
}
void map (Observable* var, const mbr_func_ptr_t& ptr)
{
var->attach (object);
observables.push_back (var);
dispatch_map[var->get_name ()] = ptr;
dispatch (var);
//std::cerr << "[Dispatcher] mapped " << var->string() << " => "
// << object << "->" << ptr << std::endl;
}
void dispatch (const Observable* var)
{
mbr_func_ptr_t handler_ptr = dispatch_map[var->get_name ()];
if (handler_ptr) {
((object)->*(handler_ptr)) (var->get_value ());
} else {
std::cerr << "[Dispatcher] Unable to handle notification from observable: "
<< var->get_name () << " - " << var->get_value() << std::endl;
}
}
private:
class_t* object;
dispatch_map_t dispatch_map;
std::list<Observable*> observables;
};
#endif // PRESAGE_DISPATCHER
Dispatcher::object
class_t * object
Definition: dispatcher.h:103
Dispatcher::dispatch_map_t
std::map< std::string, mbr_func_ptr_t > dispatch_map_t
Definition: dispatcher.h:63
Observable::get_name
virtual std::string get_name() const =0
Dispatcher::map
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:80
Observable::get_value
virtual std::string get_value() const =0
Dispatcher::observables
std::list< Observable * > observables
Definition: dispatcher.h:105
Dispatcher::~Dispatcher
~Dispatcher()
Definition: dispatcher.h:70
Dispatcher::Dispatcher
Dispatcher(class_t *obj)
Definition: dispatcher.h:65
Dispatcher::dispatch
void dispatch(const Observable *var)
Definition: dispatcher.h:91
Observable::attach
virtual void attach(Observer *observer)
Definition: observable.cpp:33
Dispatcher::mbr_func_ptr_t
void(class_t::* mbr_func_ptr_t)(const std::string &value)
Definition: dispatcher.h:62
endl
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:277
Dispatcher::dispatch_map
dispatch_map_t dispatch_map
Definition: dispatcher.h:104
Observable
Definition: observable.h:36
Dispatcher
Definition: dispatcher.h:41
observable.h