presage  0.8.7
dispatcher.h
Go to the documentation of this file.
00001 
00002 /******************************************************
00003  *  Presage, an extensible predictive text entry system
00004  *  ---------------------------------------------------
00005  *
00006  *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License along
00019     with this program; if not, write to the Free Software Foundation, Inc.,
00020     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021                                                                              *
00022                                                                 **********(*)*/
00023 
00024 
00025 #ifndef PRESAGE_DISPATCHER
00026 #define PRESAGE_DISPATCHER
00027 
00028 #if HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #include <map>
00033 #include <string>
00034 #include <iostream>
00035 
00036 #include "observable.h"
00037 
00041 template <class class_t>
00042 class Dispatcher {
00043 public:
00044     typedef void (class_t::* mbr_func_ptr_t) (const std::string& value);
00045     typedef std::map<std::string, mbr_func_ptr_t> dispatch_map_t;
00046 
00047     Dispatcher(class_t* obj)
00048     {
00049         object = obj;
00050     }
00051 
00052     ~Dispatcher()
00053     {
00054         for (std::list<Observable*>::iterator it = observables.begin();
00055              it != observables.end();
00056              it++) {
00057             (*it)->detach (object);
00058         }
00059 
00060     }
00061 
00062     void map (Observable* var, const mbr_func_ptr_t& ptr)
00063     {
00064         var->attach (object);
00065         observables.push_back (var);
00066         dispatch_map[var->get_name ()] = ptr;
00067         dispatch (var);
00068 
00069         //std::cerr << "[Dispatcher] mapped " << var->string() << " => " 
00070         //        << object << "->" << ptr << std::endl;
00071     }
00072 
00073     void dispatch (const Observable* var) 
00074     {
00075         mbr_func_ptr_t handler_ptr = dispatch_map[var->get_name ()];
00076         if (handler_ptr) {
00077             ((object)->*(handler_ptr)) (var->get_value ());
00078         } else {
00079             std::cerr << "[Dispatcher] Unable to handle notification from observable: "
00080                       << var->get_name () << " - " << var->get_value() << std::endl;
00081         }
00082     }
00083 
00084 private:
00085     class_t* object;
00086     dispatch_map_t dispatch_map;
00087     std::list<Observable*> observables;
00088 
00089 };
00090 
00091 #endif // PRESAGE_DISPATCHER