00001 00020 #ifndef _CONFIG 00021 #define _CONFIG 1 00022 00023 #include "ConfigFile.h" 00024 #include <string> 00025 #include <iostream> 00026 00027 using namespace std; 00028 00029 00089 class Config { 00090 private: 00091 typedef ConfigFile::key_not_found key_not_found; 00092 ConfigFile fileconfig; 00093 string m_strategy; 00094 string m_type; 00095 string m_name; 00096 00097 private: 00103 string extractType(void); 00104 00112 string extractName(void); 00113 00114 public: 00115 00122 struct config_error : public std::exception{ 00123 string m_msg; 00124 string m_strategy; 00125 config_error( const string& strategy, const string& msg) 00126 : m_msg(msg), m_strategy(strategy) {} 00127 virtual const char* what() const throw() { 00128 string output = "Config Error: " +m_msg; 00129 return output.c_str(); 00130 } 00131 00132 virtual ~config_error() throw() {} 00133 }; 00134 00135 public: 00141 Config(string strategy, string name) : fileconfig(name) { 00142 m_strategy = strategy; 00143 m_type = extractType(); 00144 m_name = extractName(); 00145 } 00146 00151 Config(string strategy, ConfigFile &config) : fileconfig(config) { 00152 printf("Creando fileconfig"); 00153 m_strategy = strategy; 00154 m_type = extractType(); 00155 m_name = extractName(); 00156 } 00157 00158 Config(string strategy, Config *config) : fileconfig(config->fileconfig) { 00159 m_strategy = strategy; 00160 m_type = extractType(); 00161 m_name = extractName(); 00162 } 00163 00164 00168 string getName() { 00169 if (m_name != "") { 00170 return m_name; 00171 } 00172 else { 00173 return m_type; 00174 } 00175 } 00176 00180 string getType() { 00181 return m_type; 00182 } 00183 00184 00188 void setType(string name) { 00189 cerr <<"setType : '" <<name <<"'" <<endl; 00190 if (name != "") { 00191 m_type = name; 00192 m_name = extractName(); 00193 cerr <<"Hecho asignacion de extractName" <<endl; 00194 } 00195 00196 cerr <<"Realizado setType : '" <<name <<"'" <<endl; 00197 } 00198 00202 void setName(string name) { 00203 if (name != "") { 00204 m_name = name; 00205 } 00206 } 00207 00208 00215 template <class T> 00216 void getParam(const char *param, T &value) { 00217 string begin = m_strategy +"."; 00218 string last = "."; 00219 last += param; 00220 string key = begin +m_type +last; 00221 string key_name_default = begin +m_name +last; 00222 string key_type_default = begin + param; 00223 00224 if (fileconfig.readInto(value, key) ) { 00225 } 00226 else if (fileconfig.readInto(value, key_name_default)) { 00227 } 00228 else if (fileconfig.readInto(value, key_type_default)) { 00229 } 00230 else { 00231 throw ConfigFile::key_not_found(key_type_default); 00232 } 00233 } 00234 00235 template <class T> 00236 void getParam(const string ¶m, T &value) { 00237 getParam(param.c_str(), value); 00238 } 00239 00240 }; 00241 00242 typedef Config* ConfigPtr; 00243 00244 #endif