TDbiCfgPromptConfigurable.hxx

Go to the documentation of this file.
00001 #ifndef CFGPROMPTCONFIGURABLE
00002 #define CFGPROMPTCONFIGURABLE
00003 
00004 ////////////////////////////////////////////////////////////////////////////////////
00005 ///
00006 /// \class TDbiCfgPromptConfigurable
00007 ///
00008 /// \brief A nice base class to use with configurable objects.
00009 /// 
00010 /// Like the TDbiCfg(Lazy)Configurable class, but doesn't use
00011 /// lazy execution.
00012 ///
00013 /// How to use it:
00014 /// Set up defaults in the constructor (or somehwere else that happens soon)
00015 /// and call InitializeConfig() with them.
00016 ///
00017 /// At any time, use the Set() commands to modify your object.
00018 /// By default, your object has keys locked and values unlocked:
00019 /// This means that:
00020 ///    - If you try to set a key that doesn't exist, it fails. (Typo protection)
00021 ///    - If you try to set a key to different type, it fails. (Type protection)
00022 ///
00023 /// Note that the Set() commands include:
00024 /// Set( key, int )           | 
00025 /// Set( key, double )        |- Work like TDbiRegistry() commands, but with above safeties 
00026 /// Set( key, const char* )   |
00027 /// Set( key, TDbiRegistry )      |
00028 /// Set( TDbiRegistry )            - Works like Merge(), but with above safeties
00029 /// Set( TDbiRegistry, true)       - Ditto, but Merge()s sub-registries as well, with the same rules.
00030 /// Set( string )              - Works like JobControl parser
00031 ///                              i.e. Set("myint=1 myfloat=2.0 mystring=blahDeblah");
00032 ///
00033 /// If the configuration changes, ConfigModified() will be called, letting
00034 /// you read the new configuration variables.  But it's smart: your function
00035 /// only gets called if a variable has changed, not if they've stayed the same. 
00036 /// 
00037 /// Example:
00038 ///\verbatim
00039 ///class MyClass : public TDbiCfgPromptConfigurable 
00040 ///{
00041 ///   MyClass() {
00042 ///     TDbiRegistry r;
00043 ///     r.Set("Default1",1);
00044 ///     r.Set("Default2",2.0);
00045 ///     InitializeConfig(r);
00046 ///   };
00047 ///
00048 ///   void ConfigModified() {
00049 ///     GetConfig().Get("Default1",fDefault1);
00050 ///     .. etc ..
00051 ///   }
00052 /// \endverbatim
00053 ///};
00054 
00055 
00056 #include <string>
00057 #include "TDbiRegistry.hxx"
00058 
00059 class TDbiCfgDialog;
00060 
00061 class TDbiCfgPromptConfigurable
00062 {
00063  public:
00064   TDbiCfgPromptConfigurable();
00065   TDbiCfgPromptConfigurable(const TDbiRegistry& r) {InitializeConfig(r);};
00066   virtual ~TDbiCfgPromptConfigurable() {};
00067 
00068   // For reading out current configuration.
00069   const TDbiRegistry& GetConfig() const { return fConfig; };
00070 
00071   // Wrappers for TDbiRegistry setters.
00072   void UnLockKeys()   { fConfig.UnLockKeys(); };
00073   void LockKeys()     { fConfig.LockKeys(); };
00074   void UnLockValues() { fConfig.UnLockValues(); };
00075   void LockValues()   { fConfig.LockValues(); };
00076 
00077   // These functions all modify the current registry, and call ConfigModified() if 
00078   // a valid key changes.
00079   void Set(const char* key, char val);
00080   void Set(const char* key, const char* val);
00081   void Set(const char* key, double val);
00082   void Set(const char* key, int val);
00083   void Set(const char* key, const TDbiRegistry& val);
00084   void Set(const char* setstring);  // like modules.
00085   void Set(TDbiCfgDialog* d=0);
00086   
00087 
00088   // This is the one that does all the work:
00089   void Set(const TDbiRegistry& stuff, Bool_t recursive = false); // Sets multiple things at once.
00090 
00091   // String operation routines.
00092   static Bool_t Split(const char* line, char sep, std::string& a, std::string& b);
00093   static Bool_t Split(const std::string& line, char sep, std::string& a, std::string& b) 
00094   { return Split(line.c_str(),sep,a,b); };
00095   static Bool_t IsInt(const std::string& s, Int_t& val);
00096   static Bool_t IsFloat(const std::string& s, Double_t& val);
00097 
00098   // TDbiRegistry operation routines.
00099   static Bool_t SafeMerge(TDbiRegistry& modify, 
00100                           const TDbiRegistry& stuff, 
00101                           Bool_t recursive = false );
00102 
00103 
00104  protected:
00105   // This call sets up the object in it's 'default' condition,
00106   // and defines all the valid keys. Should be called from constructor.
00107   void         InitializeConfig(const TDbiRegistry& initConfig);  
00108 
00109   // This method is called whenever the current configuration changes in any significant way.
00110   virtual void ConfigModified(void)=0; // To be defined by user.
00111 
00112  private:
00113   TDbiRegistry fConfig;
00114   TDbiRegistry fDefaultConfig;
00115 
00116   ClassDef(TDbiCfgPromptConfigurable,1);
00117 };
00118 
00119 
00120 #endif

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1