CfgPromptConfigurable Class Reference

#include <CfgPromptConfigurable.h>

Inheritance diagram for CfgPromptConfigurable:
CalScheme PhotonStatSummarizer PulserGainFit SpillTimeFinder Calibrator MIPCalScheme MuonDriftCalScheme PEGainAggCalScheme PEGainCalScheme PmtDriftCalScheme PulserDriftCalScheme PulserLinearityCalScheme PulserSigLinCalScheme PulserTimeCalScheme QuadLinearityCalScheme SimpleCalScheme StripAttenCalScheme StripToStripCalScheme TemperatureCalScheme TimeCalScheme VaLinearityCalScheme

List of all members.

Public Member Functions

 CfgPromptConfigurable ()
 CfgPromptConfigurable (const Registry &r)
virtual ~CfgPromptConfigurable ()
const RegistryGetConfig () 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 Registry &val)
void Set (const char *setstring)
void Set (CfgDialog *d=0)
void Set (const Registry &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 (Registry &modify, const Registry &stuff, Bool_t recursive=false)

Protected Member Functions

void InitializeConfig (const Registry &initConfig)
virtual void ConfigModified (void)=0

Private Member Functions

 ClassDef (CfgPromptConfigurable, 1)

Private Attributes

Registry fConfig
Registry fDefaultConfig

Detailed Description

Definition at line 59 of file CfgPromptConfigurable.h.


Constructor & Destructor Documentation

CfgPromptConfigurable::CfgPromptConfigurable (  ) 

CfgPromptConfigurable

A nice base class to use with configurable objects.

Like the Cfg(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:

  • If you try to set a key that doesn't exist, it fails. (Typo protection)
  • If you try to set a key to different type, it fails. (Type protection)

Note that the Set() commands include: Set( key, int ) | Set( key, double ) |- Work like Registry() commands, but with above safeties Set( key, const char* ) | Set( key, Registry ) | Set( Registry ) - Works like Merge(), but with above safeties Set( Registry, 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 CfgPromptConfigurable { MyClass() { Registry r; r.Set("Default1",1); r.Set("Default2",2.0); InitializeConfig(r); };

void ConfigModified() { GetConfig().Get("Default1",fDefault1); .. etc .. }

};

Definition at line 12 of file CfgPromptConfigurable.cxx.

00013 {
00062 }

CfgPromptConfigurable::CfgPromptConfigurable ( const Registry r  )  [inline]

Definition at line 63 of file CfgPromptConfigurable.h.

References InitializeConfig().

00063 {InitializeConfig(r);};

virtual CfgPromptConfigurable::~CfgPromptConfigurable (  )  [inline, virtual]

Definition at line 64 of file CfgPromptConfigurable.h.

00064 {};


Member Function Documentation

CfgPromptConfigurable::ClassDef ( CfgPromptConfigurable  ,
 
) [private]

Reimplemented in TimeCalScheme.

virtual void CfgPromptConfigurable::ConfigModified ( void   )  [protected, pure virtual]
const Registry& CfgPromptConfigurable::GetConfig (  )  const [inline]
void CfgPromptConfigurable::InitializeConfig ( const Registry initConfig  )  [protected]

Definition at line 64 of file CfgPromptConfigurable.cxx.

References Registry::Clear(), ConfigModified(), fConfig, fDefaultConfig, Registry::LockKeys(), Registry::SetDirty(), Registry::UnLockKeys(), and Registry::UnLockValues().

Referenced by Calibrator::Calibrator(), CfgPromptConfigurable(), PulserGainFit::Init(), PhotonStatSummarizer::PhotonStatSummarizer(), and SpillTimeFinder::SpillTimeFinder().

00065 {
00066   fConfig.UnLockKeys();
00067   fConfig.UnLockValues();
00068   fConfig.Clear();
00069   fConfig=initConfig;
00070   fConfig.SetDirty(false); // Do this whenever ConfigModified is called.
00071   fConfig.LockKeys();
00072   fConfig.UnLockValues();
00073 
00074   // For GUIs:
00075   fDefaultConfig=initConfig;
00076 
00077   ConfigModified();
00078 }

bool CfgPromptConfigurable::IsFloat ( const std::string &  s,
Double_t &  val 
) [static]

Definition at line 139 of file CfgPromptConfigurable.cxx.

Referenced by Set().

00140 {
00141 //======================================================================
00142 // Does the string s represent an integer?
00143 //======================================================================
00144   char* endptr;
00145   double d = strtod(s.c_str(), &endptr);
00146   if (endptr==s && d==0.0) return false; // Conversion to double failed...
00147   
00148   // All checks for "floatness" passed
00149   val = d;
00150   return true;
00151 }

bool CfgPromptConfigurable::IsInt ( const std::string &  s,
Int_t &  val 
) [static]

Definition at line 115 of file CfgPromptConfigurable.cxx.

Referenced by Set().

00116 {
00117   //======================================================================
00118   // Does the string s represent an integer?
00119   //======================================================================
00120   const char* ss = s.c_str();
00121   char* endptr;
00122   double d = strtod(ss, &endptr);
00123   if (endptr==ss && d==0.0) return false; // Conversion to double failed...
00124   
00125   // Check if this number is int or float
00126   if (strchr(ss,'.')) return false;
00127   if (strchr(ss,'E')) return false;
00128   if (strchr(ss,'e')) return false;
00129 
00130   // All checks for "intness" passed  
00131   i = atoi(ss);
00132   
00133   return true;
00134 }

void CfgPromptConfigurable::LockKeys (  )  [inline]

Definition at line 71 of file CfgPromptConfigurable.h.

References fConfig, and Registry::LockKeys().

00071 { fConfig.LockKeys(); };

void CfgPromptConfigurable::LockValues (  )  [inline]

Definition at line 73 of file CfgPromptConfigurable.h.

References fConfig, and Registry::LockValues().

00073 { fConfig.LockValues(); };

Bool_t CfgPromptConfigurable::SafeMerge ( Registry modify,
const Registry 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 Registry::Merge()

Definition at line 250 of file CfgPromptConfigurable.cxx.

References Registry::Get(), Registry::GetType(), Registry::GetTypeAsString(), Registry::GetValueAsString(), MsgService::Instance(), Registry::Key(), Registry::KeyExists(), Registry::KeysLocked(), Msg::kFatal, Msg::kInfo, Msg::kWarning, Registry::LockKeys(), MSG, Registry::PrettyPrint(), Registry::Set(), Registry::UnLockValues(), and Registry::ValuesLocked().

Referenced by Set().

00253 {
00258 
00259   // First, see if we're locked up too tight to set anything.
00260   if(modify.ValuesLocked()) {
00261     MSG("Configurable",Msg::kWarning) << "Configurable has values locked. Can't set:" << endl;
00262     MSG("Configurable",Msg::kWarning) << stuff << endl;
00263     return false;
00264   }
00265 
00266   
00267   bool modified = false;
00268   
00269   // Iterate through all the keys in stuff.
00270   Registry::RegistryKey keyitr = stuff.Key();
00271   const char* key;
00272   while( (key = keyitr()) ) {
00273                                                 
00274     // Does modify have this key?
00275     if(!modify.KeyExists(key)) {
00276       // Key doesn't exist. 
00277 
00278       if(modify.KeysLocked()) {
00279 
00280         // Keys are locked, so throw away with an error.
00281         MSG("Configurable",Msg::kWarning) << "Key " << key << " is not initialized in this Configurable. Skipping." << endl;
00282         MSG("Configurable",Msg::kInfo)    << "Current key/values are:" <<endl;
00283         if( MsgService::Instance()->IsActive("Configurable",Msg::kInfo) )
00284           modify.PrettyPrint(std::cout);
00285 
00286         MSG("Configurable",Msg::kFatal) << "\"" << key << "\" is an illegal key! Fix your script!" << endl;
00287         assert(0);
00288         continue;
00289 
00290       } else {
00291         // Key exists. Go on to save this one.
00292       }
00293 
00294     } else {
00295       // Key exists. 
00296 
00297       // Is the key the right type?
00298       if(modify.GetType(key)!=stuff.GetType(key)) {
00299         MSG("Configurable",Msg::kWarning) << "Key \"" << key << "\" is not initialized as type (" 
00300                                           << modify.GetType(key).name() 
00301                                           << ") but you've tried to set it to type (" 
00302                                           << stuff.GetType(key).name() 
00303                                           << ")" << endl;
00304         continue;
00305       }
00306       
00307       if( modify.GetValueAsString(key)==stuff.GetValueAsString(key) ) {
00308         // The values are identical. So save the work and skip it.
00309         continue;
00310       }
00311     }
00312 
00313     // Switch on type:
00314     int    vint;
00315     double vdouble;
00316     char   vchar;
00317     const char* vcharstar;
00318     Registry vregistry;
00319 
00320     const std::type_info& theType(modify.GetType(key));
00321 
00322     if((theType==typeid(char))&&(stuff.Get(key,vchar)))   modify.Set(key,vchar);
00323     if((theType==typeid(int))&&(stuff.Get(key,vint)))    modify.Set(key,vint);
00324     if((theType==typeid(double))&&(stuff.Get(key,vdouble))) modify.Set(key,vdouble);
00325     if((theType==typeid(const char*))&&(stuff.Get(key,vcharstar))) modify.Set(key,vcharstar);
00326     if(stuff.GetTypeAsString(key) == "Registry"){
00327       if(stuff.Get(key,vregistry)) {
00328         // Registries are harder. Merge using this function.
00329         if(recursive) {
00330 
00331           Registry old;
00332           if(modify.Get(key,old)) {
00333             Registry old;
00334             // Just for safety:
00335             old.UnLockValues();
00336             old.LockKeys();
00337             if(SafeMerge(old,vregistry)) modified = true;
00338             modify.Set(key,old);
00339           }
00340 
00341         } else {
00342           modify.Set(key,vregistry);
00343         }
00344       }
00345     }
00346 
00347     modified = true;
00348   };
00349 
00350   return modified;
00351 }

void CfgPromptConfigurable::Set ( const Registry stuff,
Bool_t  recursive = false 
)

Definition at line 241 of file CfgPromptConfigurable.cxx.

References ConfigModified(), fConfig, SafeMerge(), and Registry::SetDirty().

00242 {
00243   if(SafeMerge(fConfig,stuff,recursive)) {
00244     // Something got changed, so allow user to change stuff:
00245     ConfigModified();
00246     fConfig.SetDirty(false);
00247   }
00248 }

void CfgPromptConfigurable::Set ( CfgDialog d = 0  ) 

Definition at line 154 of file CfgPromptConfigurable.cxx.

References fConfig, fDefaultConfig, CfgDialog::Query(), Set(), CfgDialog::SetCurrent(), and CfgDialog::SetDefault().

00155 {
00156 //======================================================================
00157 // Update the configuration parameters. Allow a CfgDialog object to be
00158 // passed in. If none is passed in use the default, text based dialog
00159 // object.
00160 //======================================================================
00161   bool deleteDialog = false;
00162   if (d==0) {
00163     d = new CfgDialog();
00164     deleteDialog = true;
00165   }
00166 
00167   // Set up d with the default configuration parameters
00168   d->SetDefault(fDefaultConfig);
00169   d->SetCurrent(fConfig);
00170   
00171   // Do the querry
00172   Registry r = d->Query();
00173   Set(r);
00174 
00175   // Clean up the dialog
00176   if (deleteDialog) { delete d; d = 0; }
00177 }

void CfgPromptConfigurable::Set ( const char *  setstring  ) 

Definition at line 200 of file CfgPromptConfigurable.cxx.

References IsFloat(), IsInt(), Msg::kWarning, MSG, Set(), Registry::Set(), and Split().

00201 { 
00202   // Parse a whole string.
00203   
00204   // First, look for spaces to seperate entries.
00205   Registry r;
00206   string a,b;
00207   string line = setstring;
00208   bool split;
00209   do {
00210     split = Split(line,' ',a,b);
00211     
00212     // a contains the possible code.
00213     string key, val;
00214     if(a.size()>0) {
00215       if(Split(a,'=',key,val)) {
00216         Int_t i;
00217         Double_t f;
00218         if(IsInt(val,i)) {
00219           r.Set(key.c_str(),i);
00220         } else if(IsFloat(val,f)) {
00221           r.Set(key.c_str(),f);
00222         } else {
00223           r.Set(key.c_str(),val.c_str());
00224         };
00225         
00226         
00227       } else {
00228         MSG("Configurable",Msg::kWarning) << "Can't parse fragment: " << a << endl;
00229       }
00230     }
00231     
00232     line = b;
00233   } while ( split );
00234 
00235   // Add the data.
00236   Set(r);
00237 }

void CfgPromptConfigurable::Set ( const char *  key,
const Registry val 
)

Definition at line 196 of file CfgPromptConfigurable.cxx.

References Set(), and Registry::Set().

00197 { Registry r;  r.Set(key,val); Set(r); }

void CfgPromptConfigurable::Set ( const char *  key,
int  val 
)

Definition at line 192 of file CfgPromptConfigurable.cxx.

References Set(), and Registry::Set().

00193 { Registry r;  r.Set(key,val); Set(r); }

void CfgPromptConfigurable::Set ( const char *  key,
double  val 
)

Definition at line 188 of file CfgPromptConfigurable.cxx.

References Set(), and Registry::Set().

00189 { Registry r;  r.Set(key,val); Set(r); }

void CfgPromptConfigurable::Set ( const char *  key,
const char *  val 
)

Definition at line 183 of file CfgPromptConfigurable.cxx.

References Set(), and Registry::Set().

00184 { Registry r;  r.Set(key,val); Set(r); }

void CfgPromptConfigurable::Set ( const char *  key,
char  val 
)
static Bool_t CfgPromptConfigurable::Split ( const std::string &  line,
char  sep,
std::string &  a,
std::string &  b 
) [inline, static]

Definition at line 91 of file CfgPromptConfigurable.h.

References Split().

Referenced by Split().

00092   { return Split(line.c_str(),sep,a,b); };

static Bool_t CfgPromptConfigurable::Split ( const char *  line,
char  sep,
std::string &  a,
std::string &  b 
) [static]

Referenced by Set().

void CfgPromptConfigurable::UnLockKeys (  )  [inline]

Definition at line 70 of file CfgPromptConfigurable.h.

References fConfig, and Registry::UnLockKeys().

00070 { fConfig.UnLockKeys(); };

void CfgPromptConfigurable::UnLockValues (  )  [inline]

Definition at line 72 of file CfgPromptConfigurable.h.

References fConfig, and Registry::UnLockValues().

00072 { fConfig.UnLockValues(); };


Member Data Documentation

Definition at line 112 of file CfgPromptConfigurable.h.

Referenced by InitializeConfig(), and Set().


The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1