00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Robot Group Leipzig * 00003 * martius@informatik.uni-leipzig.de * 00004 * fhesse@informatik.uni-leipzig.de * 00005 * der@informatik.uni-leipzig.de * 00006 * guettler@informatik.uni-leipzig.de * 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 * 00019 * along with this program; if not, write to the * 00020 * Free Software Foundation, Inc., * 00021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00022 *************************************************************************** 00023 * * 00024 * DESCRIPTION * 00025 * Uses design pattern mediator: * 00026 * BackCaller - subject and changemanager (combined) * 00027 * Callbackable - observer * 00028 * * 00029 * $Log: backcaller.h,v $ 00030 * Revision 1.3 2010/01/07 14:15:16 der 00031 * make it compile on 4.1.2 00032 * 00033 * Revision 1.2 2009/12/01 17:32:10 martius 00034 * adapted Makefiles to ignore backward compat. errors 00035 * 00036 * Revision 1.1 2009/08/10 07:31:04 guettler 00037 * -new BackCaller class to provide common 00038 * functions used for callback 00039 * -Callbackable interface modified 00040 * -callBack now supports different types 00041 * of CallBackable types 00042 * * 00043 * * 00044 **************************************************************************/ 00045 #ifndef __BACKCALLER_H_ 00046 #define __BACKCALLER_H_ 00047 00048 #include <vector> 00049 // Georg: 00050 // either use backward/hash_map or 00051 // tr1/unordered_map or tr1/functional in the future 00052 #if __GNUC__ > 3 00053 #include <ext/hash_map> 00054 #else 00055 #include <ext/hash_map> 00056 #endif 00057 00058 class Callbackable; 00059 00060 /** 00061 * Class prototype which provides functions to handle callbackable classes. 00062 * If a class implements this class, just use the function callBack which 00063 * calls all registered callbackable classes. 00064 * If you use different callbackable pools, just use the overloaded functions 00065 * callBack(CallbackableType type), 00066 * addCallbackable(CallbackableType, Callbackable* cb) and 00067 * removeCallbackable(CallbackableType, Callbackable* cb). 00068 */ 00069 class BackCaller 00070 { 00071 public: 00072 typedef unsigned long CallbackableType; 00073 00074 /** 00075 * This is the default Callbackable type. 00076 * If you derive from BackCaller, just define your own CallbackableTypes. 00077 */ 00078 static const CallbackableType DEFAULT_CALLBACKABLE_TYPE = 0; 00079 00080 BackCaller(); 00081 virtual ~BackCaller(); 00082 00083 /** 00084 * Adds a Callbackable instance to this caller instance. 00085 * @param type the desired CallbackableType of the Callbackable class. 00086 * @param callbackableInstance the instance to add 00087 */ 00088 virtual void addCallbackable(Callbackable* callbackableInstance, CallbackableType type = BackCaller::DEFAULT_CALLBACKABLE_TYPE); 00089 00090 /** 00091 * Removes a Callbackable instance from this caller instance. 00092 * @param type the CallbackableType of the Callbackable class. 00093 * @param callbackableInstance 00094 */ 00095 virtual void removeCallbackable(Callbackable* callbackableInstance, CallbackableType type = BackCaller::DEFAULT_CALLBACKABLE_TYPE); 00096 00097 /** 00098 * Removes all Callbackable instances from this caller instance 00099 * @param type the CallbackableType of the Callbackable class to be removed. 00100 */ 00101 virtual void removeAllCallbackables(CallbackableType type /* = BackCaller::DEFAULT_CALLBACKABLE_TYPE */); 00102 00103 00104 /** 00105 * Calls all registered callbackable classes of the determined type. 00106 * This is done by Callbackable::doOnCallback(CallbackableType type). 00107 * You can make this function private/protected if you like. 00108 * @param type the CallbackableType of the Callbackable classes. 00109 */ 00110 virtual void callBack(CallbackableType type = BackCaller::DEFAULT_CALLBACKABLE_TYPE); 00111 00112 /** 00113 * Calls all registered callbackable classes of the determined type. 00114 * This is done by Callbackable::doOnCallback(CallbackableType type). 00115 * This function uses QUICKMP in order to parallelise the callbacks. 00116 * Remember that there is only shared the used CallbackableList. So if you 00117 * have other variables/objects to share, implement your own version. 00118 * You can make this function private/protected if you like. 00119 * @param type the CallbackableType of the Callbackable classes. 00120 */ 00121 virtual void callBackQMP(CallbackableType type = BackCaller::DEFAULT_CALLBACKABLE_TYPE); 00122 00123 private: 00124 struct CallbackableTypeHash 00125 { 00126 size_t operator() (const CallbackableType& type) const { return type; } 00127 }; 00128 00129 typedef std::vector<Callbackable*> callbackableListType; 00130 typedef __gnu_cxx::hash_map<CallbackableType, callbackableListType*, CallbackableTypeHash> callbackableMapType; 00131 /** 00132 * This hashmap holds every list of Callbackables for each CallbackableType. 00133 */ 00134 callbackableMapType callbackableMap; 00135 }; 00136 00137 #endif /* __BACKCALLER_H_ */