00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef CONTROLER_H_
00041 #define CONTROLER_H_
00042 #include <string>
00043 #include <map>
00044 #include <utility>
00045 #include "Exceptions.h"
00046 #include "log4cxx/logger.h"
00047
00048 using namespace std;
00049 using namespace log4cxx;
00050
00052 template<class T>
00053 class Controller {
00054
00055 public:
00056
00057 typedef typename map<string, T>::iterator TIterator;
00058 typedef pair<TIterator, TIterator> TIterPair;
00059
00061 Controller();
00063 ~Controller();
00065
00070 T* exists(string s);
00071
00073
00078 bool remove(string s);
00079
00081
00085 void registerReference(string s);
00086
00088
00092 void unregisterReference(string s);
00093
00098 pair<TIterator, TIterator> getMapIterators();
00105 int cleanup();
00106 private:
00107 bool check_remove(T);
00108
00109 protected:
00111
00118 T* _create(string) throw (ControlerInsertError);
00119
00120 static LoggerPtr logger;
00121 map<string, T> ObjectMap;
00122 map<string, int> ReferenceMap;
00123 };
00124
00125 template<class T> LoggerPtr Controller<T>::logger(Logger::getLogger(
00126 "Controller"));
00127
00128 template<class T> Controller<T>::Controller(){
00129 LOG4CXX_DEBUG(logger,"New Controller Created");
00130 }
00131
00132 template<class T> Controller<T>::~Controller(){
00133
00134 LOG4CXX_DEBUG(logger,"Controller Destroyed");
00135 }
00136
00137 template<class T> T* Controller<T>::exists(string s){
00138 typename map<string, T>::iterator it = this->ObjectMap.find(s);
00139 if (it != this->ObjectMap.end()) {
00140 LOG4CXX_DEBUG(logger,"Object "<<s.c_str()<<
00141 " already exists");
00142 return &(it->second);
00143 }
00144 LOG4CXX_DEBUG(logger,"Object "<<s.c_str()<< " does not exist");
00145 return NULL;
00146 }
00147
00148 template<class T> bool Controller<T>::remove(string s){
00149
00150
00151
00152
00153
00154 if (this->check_remove(this->ObjectMap[s])) {
00155 this->ObjectMap.erase(s);
00156 LOG4CXX_DEBUG(logger,"Object "<<s.c_str()<<
00157 " removed");
00158 return true;
00159 } else
00160 return false;
00161 }
00162
00163 template<class T> bool Controller<T>::check_remove(T obj){
00164 return true;
00165 }
00166
00167
00168 template<class T> T* Controller<T>::_create(string s)
00169 throw (ControlerInsertError){
00170 pair<TIterator, bool> ret;
00171 try {
00172 T* newT = new T();
00173 ret = this->ObjectMap.insert(pair<string, T> (s, *newT));
00174 if (ret.second == false) {
00175 ControlerInsertError e;
00176 throw e;
00177 }
00178 LOG4CXX_DEBUG(logger,"Object "<<s.c_str()<< " created");
00179 delete newT;
00180 } catch (ControlerInsertError e) {
00181 LOG4CXX_DEBUG(logger,"Object "<<s.c_str()<<
00182 " already exist");
00183 throw e;
00184 }
00185
00186 return &(ret.first->second);
00187 }
00188
00189 template<class T> int Controller<T>::cleanup(){
00190 map<string,int>::iterator rit;
00191 int count=0;
00192 for (rit = ReferenceMap.begin(); rit != ReferenceMap.end(); rit++){
00193 if(rit->second == 0){
00194
00195 LOG4CXX_DEBUG(logger, "Cleaning non-referenced instance of "<<rit->first);
00196
00197 this->ObjectMap[rit->first].prepareCleanup();
00198
00199 this->ObjectMap.erase(rit->first);
00200
00201 this->ReferenceMap.erase(rit);
00202 count++;
00203 }
00204 }
00205 return count;
00206 }
00207
00208 template<class T> void Controller<T>::registerReference(string s){
00209 if(this->ReferenceMap.count(s) == 0){
00210 this->ReferenceMap[s]=0;
00211 }
00212 this->ReferenceMap[s]++;
00213 }
00214
00215 template<class T> void Controller<T>::unregisterReference(string s){
00216 if(this->ReferenceMap.count(s) != 0){
00217 this->ReferenceMap[s]--;
00218 LOG4CXX_DEBUG(logger,"unregistering one reference of "<< s<< ". Still left :"
00219 <<this->ReferenceMap[s]);
00220 }
00221 }
00222
00223 template<class T> typename Controller<T>::TIterPair Controller<T>::getMapIterators(){
00224 pair<TIterator, TIterator> p;
00225 p.first = this->ObjectMap.begin();
00226 p.second = this->ObjectMap.end();
00227 return p;
00228 }
00229
00230 #endif