#ifndef CFGPROMPTCONFIGURABLE #define CFGPROMPTCONFIGURABLE //////////////////////////////////////////////////////////////////////////////////// /// /// \class COMET::IDbiCfgPromptConfigurable /// /// \brief A nice base class to use with configurable objects. /// /// Like the IDbiCfg(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 IDbiRegistry() commands, but with above safeties /// Set( key, const char* ) | /// Set( key, IDbiRegistry ) | /// Set( IDbiRegistry ) - Works like Merge(), but with above safeties /// Set( IDbiRegistry, 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: ///\verbatim ///class MyClass : public IDbiCfgPromptConfigurable ///{ /// MyClass() { /// IDbiRegistry r; /// r.Set("Default1",1); /// r.Set("Default2",2.0); /// InitializeConfig(r); /// }; /// /// void ConfigModified() { /// GetConfig().Get("Default1",fDefault1); /// .. etc .. /// } /// \endverbatim ///}; #include #include "IDbiRegistry.hxx" namespace COMET { class IDbiCfgDialog; } namespace COMET { class IDbiCfgPromptConfigurable { public: IDbiCfgPromptConfigurable(); IDbiCfgPromptConfigurable(const IDbiRegistry& r) {InitializeConfig(r);}; virtual ~IDbiCfgPromptConfigurable() {}; // For reading out current configuration. const IDbiRegistry& GetConfig() const { return fConfig; }; // Wrappers for IDbiRegistry setters. void UnLockKeys() { fConfig.UnLockKeys(); }; void LockKeys() { fConfig.LockKeys(); }; void UnLockValues() { fConfig.UnLockValues(); }; void LockValues() { fConfig.LockValues(); }; // These functions all modify the current registry, and call ConfigModified() if // a valid key changes. 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 IDbiRegistry& val); void Set(const char* setstring); // like modules. void Set(COMET::IDbiCfgDialog* d=0); // This is the one that does all the work: void Set(const IDbiRegistry& stuff, Bool_t recursive = false); // Sets multiple things at once. // String operation routines. 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) { return Split(line.c_str(),sep,a,b); }; static Bool_t IsInt(const std::string& s, Int_t& val); static Bool_t IsFloat(const std::string& s, Double_t& val); // IDbiRegistry operation routines. static Bool_t SafeMerge(IDbiRegistry& modify, const IDbiRegistry& stuff, Bool_t recursive = false ); protected: // This call sets up the object in it's 'default' condition, // and defines all the valid keys. Should be called from constructor. void InitializeConfig(const IDbiRegistry& initConfig); // This method is called whenever the current configuration changes in any significant way. virtual void ConfigModified(void)=0; // To be defined by user. private: IDbiRegistry fConfig; IDbiRegistry fDefaultConfig; ClassDef(IDbiCfgPromptConfigurable,1); }; }; #endif