A nice base class to use with configurable objects. More...
#include <TDbiCfgPromptConfigurable.hxx>
Public Member Functions | |
TDbiCfgPromptConfigurable () | |
TDbiCfgPromptConfigurable (const TDbiRegistry &r) | |
virtual | ~TDbiCfgPromptConfigurable () |
const TDbiRegistry & | GetConfig () const |
void | UnLockKeys () |
void | LockKeys () |
void | UnLockValues () |
void | LockValues () |
void | Set (const char *key, char val) |
void | Set (const char *key, const char *val) |
void | Set (const char *key, double val) |
void | Set (const char *key, int val) |
void | Set (const char *key, const TDbiRegistry &val) |
void | Set (const char *setstring) |
void | Set (TDbiCfgDialog *d=0) |
void | Set (const TDbiRegistry &stuff, Bool_t recursive=false) |
Static Public Member Functions | |
static Bool_t | Split (const char *line, char sep, std::string &a, std::string &b) |
static Bool_t | Split (const std::string &line, char sep, std::string &a, std::string &b) |
static Bool_t | IsInt (const std::string &s, Int_t &val) |
static Bool_t | IsFloat (const std::string &s, Double_t &val) |
static Bool_t | SafeMerge (TDbiRegistry &modify, const TDbiRegistry &stuff, Bool_t recursive=false) |
Protected Member Functions | |
void | InitializeConfig (const TDbiRegistry &initConfig) |
virtual void | ConfigModified (void)=0 |
Private Member Functions | |
ClassDef (TDbiCfgPromptConfigurable, 1) | |
Private Attributes | |
TDbiRegistry | fConfig |
TDbiRegistry | fDefaultConfig |
A nice base class to use with configurable objects.
Like the TDbiCfg(Lazy)Configurable class, but doesn't use lazy execution.
How to use it: Set up defaults in the constructor (or somehwere else that happens soon) and call InitializeConfig() with them.
At any time, use the Set() commands to modify your object. By default, your object has keys locked and values unlocked: This means that:
Note that the Set() commands include: Set( key, int ) | Set( key, double ) |- Work like TDbiRegistry() commands, but with above safeties Set( key, const char* ) | Set( key, TDbiRegistry ) | Set( TDbiRegistry ) - Works like Merge(), but with above safeties Set( TDbiRegistry, true) - Ditto, but Merge()s sub-registries as well, with the same rules. Set( string ) - Works like JobControl parser i.e. Set("myint=1 myfloat=2.0 mystring=blahDeblah");
If the configuration changes, ConfigModified() will be called, letting you read the new configuration variables. But it's smart: your function only gets called if a variable has changed, not if they've stayed the same.
Example:
class MyClass : public TDbiCfgPromptConfigurable { MyClass() { TDbiRegistry r; r.Set("Default1",1); r.Set("Default2",2.0); InitializeConfig(r); }; void ConfigModified() { GetConfig().Get("Default1",fDefault1); .. etc .. }
};
Definition at line 61 of file TDbiCfgPromptConfigurable.hxx.
TDbiCfgPromptConfigurable::TDbiCfgPromptConfigurable | ( | ) |
TDbiCfgPromptConfigurable::TDbiCfgPromptConfigurable | ( | const TDbiRegistry & | r | ) | [inline] |
Definition at line 65 of file TDbiCfgPromptConfigurable.hxx.
References InitializeConfig().
00065 {InitializeConfig(r);};
virtual TDbiCfgPromptConfigurable::~TDbiCfgPromptConfigurable | ( | ) | [inline, virtual] |
Definition at line 66 of file TDbiCfgPromptConfigurable.hxx.
TDbiCfgPromptConfigurable::ClassDef | ( | TDbiCfgPromptConfigurable | , | |
1 | ||||
) | [private] |
virtual void TDbiCfgPromptConfigurable::ConfigModified | ( | void | ) | [protected, pure virtual] |
const TDbiRegistry& TDbiCfgPromptConfigurable::GetConfig | ( | ) | const [inline] |
Definition at line 69 of file TDbiCfgPromptConfigurable.hxx.
References fConfig.
00069 { return fConfig; };
void TDbiCfgPromptConfigurable::InitializeConfig | ( | const TDbiRegistry & | initConfig | ) | [protected] |
Definition at line 66 of file TDbiCfgPromptConfigurable.cxx.
References TDbiRegistry::Clear(), ConfigModified(), fConfig, fDefaultConfig, TDbiRegistry::LockKeys(), TDbiRegistry::SetDirty(), TDbiRegistry::UnLockKeys(), and TDbiRegistry::UnLockValues().
Referenced by TDbiCfgPromptConfigurable().
00067 { 00068 fConfig.UnLockKeys(); 00069 fConfig.UnLockValues(); 00070 fConfig.Clear(); 00071 fConfig=initConfig; 00072 fConfig.SetDirty(false); // Do this whenever ConfigModified is called. 00073 fConfig.LockKeys(); 00074 fConfig.UnLockValues(); 00075 00076 // For GUIs: 00077 fDefaultConfig=initConfig; 00078 00079 ConfigModified(); 00080 }
bool TDbiCfgPromptConfigurable::IsFloat | ( | const std::string & | s, | |
Double_t & | val | |||
) | [static] |
Definition at line 141 of file TDbiCfgPromptConfigurable.cxx.
Referenced by Set().
00142 { 00143 //====================================================================== 00144 // Does the string s represent an integer? 00145 //====================================================================== 00146 char* endptr; 00147 double d = strtod(s.c_str(), &endptr); 00148 if (endptr==s && d==0.0) return false; // Conversion to double failed... 00149 00150 // All checks for "floatness" passed 00151 val = d; 00152 return true; 00153 }
bool TDbiCfgPromptConfigurable::IsInt | ( | const std::string & | s, | |
Int_t & | val | |||
) | [static] |
Definition at line 117 of file TDbiCfgPromptConfigurable.cxx.
Referenced by Set().
00118 { 00119 //====================================================================== 00120 // Does the string s represent an integer? 00121 //====================================================================== 00122 const char* ss = s.c_str(); 00123 char* endptr; 00124 double d = strtod(ss, &endptr); 00125 if (endptr==ss && d==0.0) return false; // Conversion to double failed... 00126 00127 // Check if this number is int or float 00128 if (strchr(ss,'.')) return false; 00129 if (strchr(ss,'E')) return false; 00130 if (strchr(ss,'e')) return false; 00131 00132 // All checks for "intness" passed 00133 i = atoi(ss); 00134 00135 return true; 00136 }
void TDbiCfgPromptConfigurable::LockKeys | ( | ) | [inline] |
Definition at line 73 of file TDbiCfgPromptConfigurable.hxx.
References fConfig, and TDbiRegistry::LockKeys().
void TDbiCfgPromptConfigurable::LockValues | ( | ) | [inline] |
Definition at line 75 of file TDbiCfgPromptConfigurable.hxx.
References fConfig, and TDbiRegistry::LockValues().
00075 { fConfig.LockValues(); };
Bool_t TDbiCfgPromptConfigurable::SafeMerge | ( | TDbiRegistry & | modify, | |
const TDbiRegistry & | stuff, | |||
Bool_t | recursive = false | |||
) | [static] |
Sets multiple things at once in registry. If anything gets modified (to a new value) it returns true. Works recursively on owned registries. Keeps type-safety.. better than TDbiRegistry::Merge()
Throws EBadTDbiRegistryKeys();
Definition at line 258 of file TDbiCfgPromptConfigurable.cxx.
References TDbiRegistry::Get(), TDbiRegistry::GetType(), TDbiRegistry::GetTypeAsString(), TDbiRegistry::GetValueAsString(), TDbiRegistry::Key(), TDbiRegistry::KeyExists(), TDbiRegistry::KeysLocked(), TDbiRegistry::LockKeys(), TDbiRegistry::PrettyPrint(), TDbiRegistry::Set(), SK_DBI_Info, SK_DBI_Severe, SK_DBI_Warn, TDbiRegistry::UnLockValues(), and TDbiRegistry::ValuesLocked().
Referenced by Set().
00261 { 00262 00263 // First, see if we're locked up too tight to set anything. 00264 if(modify.ValuesLocked()) { 00265 SK_DBI_Warn( "Configurable has values locked. Can't set:" << " "); 00266 SK_DBI_Warn( stuff << " "); 00267 return false; 00268 } 00269 00270 00271 bool modified = false; 00272 00273 // Iterate through all the keys in stuff. 00274 TDbiRegistry::TDbiRegistryKey keyitr = stuff.Key(); 00275 const char* key; 00276 while( (key = keyitr()) ) { 00277 00278 // Does modify have this key? 00279 if(!modify.KeyExists(key)) { 00280 // Key doesn't exist. 00281 00282 if(modify.KeysLocked()) { 00283 00284 // Keys are locked, so throw away with an error. 00285 SK_DBI_Warn( "Key " << key << " is not initialized in this Configurable. Skipping." << " "); 00286 SK_DBI_Info("Current key/values are:" <<" "); 00287 modify.PrettyPrint(std::cout); 00288 SK_DBI_Severe("FATAL: " << "\"" << key << "\" is an illegal key! Fix your script!" << " "); 00289 throw EBadTDbiRegistryKeys(); 00290 continue; 00291 00292 } else { 00293 // Key exists. Go on to save this one. 00294 } 00295 00296 } else { 00297 // Key exists. 00298 00299 // Is the key the right type? 00300 if(modify.GetType(key)!=stuff.GetType(key)) { 00301 SK_DBI_Warn( "Key \"" << key << "\" is not initialized as type (" 00302 << modify.GetType(key).name() 00303 << ") but you've tried to set it to type (" 00304 << stuff.GetType(key).name() 00305 << ")" << " "); 00306 continue; 00307 } 00308 00309 if( modify.GetValueAsString(key)==stuff.GetValueAsString(key) ) { 00310 // The values are identical. So save the work and skip it. 00311 continue; 00312 } 00313 } 00314 00315 // Switch on type: 00316 int vint; 00317 double vdouble; 00318 char vchar; 00319 const char* vcharstar; 00320 TDbiRegistry vregistry; 00321 00322 const type_info& theType(modify.GetType(key)); 00323 00324 if((theType==typeid(char))&&(stuff.Get(key,vchar))) modify.Set(key,vchar); 00325 if((theType==typeid(int))&&(stuff.Get(key,vint))) modify.Set(key,vint); 00326 if((theType==typeid(double))&&(stuff.Get(key,vdouble))) modify.Set(key,vdouble); 00327 if((theType==typeid(const char*))&&(stuff.Get(key,vcharstar))) modify.Set(key,vcharstar); 00328 if(stuff.GetTypeAsString(key) == "TDbiRegistry"){ 00329 if(stuff.Get(key,vregistry)) { 00330 // Registries are harder. Merge using this function. 00331 if(recursive) { 00332 00333 TDbiRegistry old; 00334 if(modify.Get(key,old)) { 00335 TDbiRegistry old; 00336 // Just for safety: 00337 old.UnLockValues(); 00338 old.LockKeys(); 00339 if(SafeMerge(old,vregistry)) modified = true; 00340 modify.Set(key,old); 00341 } 00342 00343 } else { 00344 modify.Set(key,vregistry); 00345 } 00346 } 00347 } 00348 00349 modified = true; 00350 }; 00351 00352 return modified; 00353 }
void TDbiCfgPromptConfigurable::Set | ( | const TDbiRegistry & | stuff, | |
Bool_t | recursive = false | |||
) |
Definition at line 244 of file TDbiCfgPromptConfigurable.cxx.
References ConfigModified(), fConfig, SafeMerge(), and TDbiRegistry::SetDirty().
00245 { 00246 if(SafeMerge(fConfig,stuff,recursive)) { 00247 // Something got changed, so allow user to change stuff: 00248 ConfigModified(); 00249 fConfig.SetDirty(false); 00250 } 00251 }
void TDbiCfgPromptConfigurable::Set | ( | TDbiCfgDialog * | d = 0 |
) |
Definition at line 156 of file TDbiCfgPromptConfigurable.cxx.
References fConfig, fDefaultConfig, TDbiCfgDialog::Query(), Set(), TDbiCfgDialog::SetCurrent(), and TDbiCfgDialog::SetDefault().
00157 { 00158 //====================================================================== 00159 // Update the configuration parameters. Allow a TDbiCfgDialog object to be 00160 // passed in. If none is passed in use the default, text based dialog 00161 // object. 00162 //====================================================================== 00163 bool deleteDialog = false; 00164 if (d==0) { 00165 d = new TDbiCfgDialog(); 00166 deleteDialog = true; 00167 } 00168 00169 // Set up d with the default configuration parameters 00170 d->SetDefault(fDefaultConfig); 00171 d->SetCurrent(fConfig); 00172 00173 // Do the querry 00174 TDbiRegistry r = d->Query(); 00175 Set(r); 00176 00177 // Clean up the dialog 00178 if (deleteDialog) { delete d; d = 0; } 00179 }
void TDbiCfgPromptConfigurable::Set | ( | const char * | setstring | ) |
Definition at line 202 of file TDbiCfgPromptConfigurable.cxx.
References IsFloat(), IsInt(), Set(), TDbiRegistry::Set(), SK_DBI_Warn, and Split().
00203 { 00204 // Parse a whole string. 00205 00206 // First, look for spaces to seperate entries. 00207 TDbiRegistry r; 00208 string a,b; 00209 string line = setstring; 00210 bool split; 00211 do { 00212 split = Split(line,' ',a,b); 00213 00214 // a contains the possible code. 00215 string key, val; 00216 if(a.size()>0) { 00217 if(Split(a,'=',key,val)) { 00218 Int_t i; 00219 Double_t f; 00220 if(IsInt(val,i)) { 00221 r.Set(key.c_str(),i); 00222 } else if(IsFloat(val,f)) { 00223 r.Set(key.c_str(),f); 00224 } else { 00225 r.Set(key.c_str(),val.c_str()); 00226 }; 00227 00228 00229 } else { 00230 SK_DBI_Warn( "Can't parse fragment: " << a << " "); 00231 } 00232 } 00233 00234 line = b; 00235 } while ( split ); 00236 00237 // Add the data. 00238 Set(r); 00239 }
void TDbiCfgPromptConfigurable::Set | ( | const char * | key, | |
const TDbiRegistry & | val | |||
) |
Definition at line 198 of file TDbiCfgPromptConfigurable.cxx.
References Set(), and TDbiRegistry::Set().
00199 { TDbiRegistry r; r.Set(key,val); Set(r); }
void TDbiCfgPromptConfigurable::Set | ( | const char * | key, | |
int | val | |||
) |
Definition at line 194 of file TDbiCfgPromptConfigurable.cxx.
References Set(), and TDbiRegistry::Set().
00195 { TDbiRegistry r; r.Set(key,val); Set(r); }
void TDbiCfgPromptConfigurable::Set | ( | const char * | key, | |
double | val | |||
) |
Definition at line 190 of file TDbiCfgPromptConfigurable.cxx.
References Set(), and TDbiRegistry::Set().
00191 { TDbiRegistry r; r.Set(key,val); Set(r); }
void TDbiCfgPromptConfigurable::Set | ( | const char * | key, | |
const char * | val | |||
) |
Definition at line 185 of file TDbiCfgPromptConfigurable.cxx.
References Set(), and TDbiRegistry::Set().
00186 { TDbiRegistry r; r.Set(key,val); Set(r); }
void TDbiCfgPromptConfigurable::Set | ( | const char * | key, | |
char | val | |||
) |
Definition at line 181 of file TDbiCfgPromptConfigurable.cxx.
References TDbiRegistry::Set().
Referenced by Set().
00182 { TDbiRegistry r; r.Set(key,val); Set(r); }
static Bool_t TDbiCfgPromptConfigurable::Split | ( | const std::string & | line, | |
char | sep, | |||
std::string & | a, | |||
std::string & | b | |||
) | [inline, static] |
Definition at line 93 of file TDbiCfgPromptConfigurable.hxx.
References Split().
Referenced by Split().
00094 { return Split(line.c_str(),sep,a,b); };
static Bool_t TDbiCfgPromptConfigurable::Split | ( | const char * | line, | |
char | sep, | |||
std::string & | a, | |||
std::string & | b | |||
) | [static] |
void TDbiCfgPromptConfigurable::UnLockKeys | ( | ) | [inline] |
Definition at line 72 of file TDbiCfgPromptConfigurable.hxx.
References fConfig, and TDbiRegistry::UnLockKeys().
00072 { fConfig.UnLockKeys(); };
void TDbiCfgPromptConfigurable::UnLockValues | ( | ) | [inline] |
Definition at line 74 of file TDbiCfgPromptConfigurable.hxx.
References fConfig, and TDbiRegistry::UnLockValues().
00074 { fConfig.UnLockValues(); };
Definition at line 113 of file TDbiCfgPromptConfigurable.hxx.
Referenced by GetConfig(), InitializeConfig(), LockKeys(), LockValues(), Set(), UnLockKeys(), and UnLockValues().
Definition at line 114 of file TDbiCfgPromptConfigurable.hxx.
Referenced by InitializeConfig(), and Set().