/** * * * * \class COMET::IDbiRegistry * * * \brief Type safe heterogenous collection of key value pairs. * * Stored in a std::map, keyed by a string. Values can be int, char, string, double or IDbiRegistryItem. * Actuall all stored internally as IDbiRegistryItem s * Contact: bv@bnl.gov * * Created on: Wed Oct 25 17:13:16 2000 * */ #ifndef TDBIREGISTRY_H #define TDBIREGISTRY_H #include #include #include #include class IDbiRegistryItem; class type_info; #include "IDbiRegistryItemXxx.hxx" //Needed for LinkDef namespace COMET { class IDbiRegistry : public TNamed { public: typedef std::map tRegMap; typedef void (*ErrorHandler)(void); /// Create a IDbiRegistry. If readonly is false, any key's value can /// be set multiple times, otherwise only the first setting is allowed. /// See methods below regarding locking of keys and values. explicit IDbiRegistry(bool readonly = true); /// Deep copy constructor. IDbiRegistry(const IDbiRegistry& rhs); virtual ~IDbiRegistry(); /// Deep assignment. IDbiRegistry& operator=(const IDbiRegistry& rhs); /// Copy rhs into this, respects this's locks. void Merge(const IDbiRegistry& rhs); /// Return number of entries. unsigned int Size() const { return fMap.size(); } /// Check if key exists. bool KeyExists(const char* key) const; void RemoveKey(const char* key); /// Clear IDbiRegistry - deletes all items. void Clear(Option_t *option=""); //*MENU* /// Dump to cerr. void Dump(void) const; //*MENU* /// Print to cout (without extraneous bits of Dump()). virtual std::ostream& PrintStream(std::ostream& os) const; virtual std::istream& ReadStream(std::istream& is); // TObject::Print(). virtual void Print(Option_t *option="") const; //*MENU* virtual std::ostream& PrettyPrint(std::ostream& os) const; // TObject::Browse() /// The default implementation crashes TBrowser, doing nothing is better. virtual void Browse(TBrowser*){} /// Control if an existing value can be set. virtual bool ValuesLocked(void) const { return fValuesLocked; } /// Control if an existing value can be set. virtual void LockValues(void) { fValuesLocked = true; } //*MENU* /// Control if an existing value can be set. virtual void UnLockValues(void) { fValuesLocked = false; } //*MENU* /// Control if new key/value pairs can be added. virtual bool KeysLocked(void) const { return fKeysLocked; } /// Control if new key/value pairs can be added. virtual void LockKeys(void) { fKeysLocked = true; } //*MENU* /// Control if new key/value pairs can be added. virtual void UnLockKeys(void) { fKeysLocked = false; } //*MENU* /// Access an internal "dirty" flag IDbiRegistry maintains (but does /// not use) It will be set any time a non-const method is /// accessed, or explicitly via SetDirty(). Initially a IDbiRegistry /// is dirty (original sin?). void SetDirty(bool is_dirty = true) { fDirty = is_dirty; } bool IsDirty() { return fDirty; } void SetErrorHandler(ErrorHandler eh) { fErrorHandler = eh; } // bool Get(const char* key, bool& b) const; /// Access a char value. Return true and set second argument if key is /// found, else returns false. bool Get(const char* key, char& c) const; /// Access a string value. Return true and set second argument if key is /// found, else returns false. bool Get(const char* key, const char*& s) const; /// Access an int value. Return true and set second argument if key is /// found, else returns false. bool Get(const char* key, int& i) const; /// Access a double value. Return true and set second argument if key is /// found, else returns false. bool Get(const char* key, double& d) const; /// Access a IDbiRegistry value. Return true and set second argument if key is /// found, else returns false. bool Get(const char* key, IDbiRegistry& r) const; /// Return the type_info of the value corresponding to the given /// key. If key doesn't exist, type_info for type void is returned. const type_info& GetType(const char* key) const; /// Return "int", "double", "char", "string", "IDbiRegistry" or "void" std::string GetTypeAsString(const char* key) const; /// see format.txt std::string GetValueAsString(const char* key) const; // bool GetBool(const char* key) const; /// Access a char value. Returns value if key lookup succeeds, else /// prints warning message. Use above Get() methods for a safer /// access method. char GetChar(const char* key) const; /// Access a string value. Returns value if key lookup succeeds, else /// prints warning message. Use above Get() methods for a safer /// access method. const char* GetCharString(const char* key) const; /// Access an int value. Returns value if key lookup succeeds, else /// prints warning message. Use above Get() methods for a safer /// access method. int GetInt(const char* key) const; /// Access a double value. Returns value if key lookup succeeds, else /// prints warning message. Use above Get() methods for a safer /// access method. double GetDouble(const char* key) const; /// Access an COMET::IDbiRegistry value. Returns value if key lookup succeeds, else /// prints warning message. Use above Get() methods for a safer /// access method. IDbiRegistry GetTDbiRegistry(const char* key) const; // bool Set(const char* key, bool b); /// Set the value associated with the given key. Return false if /// locks prevent setting or if type mismatch. bool Set(const char* key, char c); bool Set(const char* key, const char* s); bool Set(const char* key, int i); bool Set(const char* key, double d); bool Set(const char* key, IDbiRegistry r); /// Container for a registry key. Contains a std::map of key value pairs where key /// is a string and value is a registry item. Also stores pointer to a IDbiRegistry object. class IDbiRegistryKey { public: IDbiRegistryKey(); IDbiRegistryKey(const IDbiRegistry* r); virtual ~IDbiRegistryKey(); const char* operator()(void); private: const IDbiRegistry* fReg; std::map::iterator fIt; }; // end of class IDbiRegistryKey IDbiRegistryKey Key(void) const; private: bool fValuesLocked; bool fKeysLocked; ErrorHandler fErrorHandler; //! not written out #ifndef __CINT__ friend class IDbiRegistryKey; // template bool SetPtr(std::string key, T* val); tRegMap fMap; #endif bool fDirty; ClassDef(COMET::IDbiRegistry,1) }; // end of class IDbiRegistry } inline std::ostream& operator<<(std::ostream& os, const COMET::IDbiRegistry& r) { return r.PrintStream(os); } #include "IDbiRegistryItemXxx.hxx" #endif // TDBIREGISTRY_H