TDbiRegistry.hxx

Go to the documentation of this file.
00001 /**
00002  *
00003  *
00004  *
00005  * \class TDbiRegistry
00006  *
00007  *
00008  * \brief Type safe heterogenous collection of key value pairs. 
00009  *
00010  * Stored in a std::map, keyed by a string. Values can be int, char, string, double or TDbiRegistryItem.
00011  * Actuall all stored internally as TDbiRegistryItem s
00012  * Contact: bv@bnl.gov
00013  *
00014  * Created on: Wed Oct 25 17:13:16 2000
00015  *
00016  */
00017 
00018 #ifndef TDBIREGISTRY_H
00019 #define TDBIREGISTRY_H
00020 
00021 #include <iostream>
00022 #include <map>
00023 #include <string>
00024 
00025 #include <TNamed.h>
00026 
00027 class TDbiRegistryItem;
00028 class type_info;
00029 
00030 #include "TDbiRegistryItemXxx.hxx"    //Needed for LinkDef
00031 
00032 
00033 class TDbiRegistry : public TNamed
00034 {
00035 public:
00036     typedef std::map<std::string,TDbiRegistryItem*> tRegMap;
00037     typedef void (*ErrorHandler)(void);
00038 
00039     /// Create a TDbiRegistry.  If readonly is false, any key's value can
00040     /// be set multiple times, otherwise only the first setting is allowed.
00041     /// See methods below regarding locking of keys and values.
00042     explicit TDbiRegistry(bool readonly = true);
00043 
00044     /// Deep copy constructor.
00045     TDbiRegistry(const TDbiRegistry& rhs);
00046 
00047     virtual ~TDbiRegistry();
00048 
00049     /// Deep assignment.
00050     TDbiRegistry& operator=(const TDbiRegistry& rhs);
00051 
00052     /// Copy rhs into this, respects this's locks.
00053     void Merge(const TDbiRegistry& rhs);
00054 
00055     /// Return number of entries.
00056     unsigned int Size() const { return fMap.size(); }
00057 
00058     /// Check if key exists.
00059     bool KeyExists(const char* key) const;
00060     void RemoveKey(const char* key);
00061 
00062     /// Clear TDbiRegistry - deletes all items.
00063     void Clear(Option_t *option=""); //*MENU*
00064 
00065     /// Dump to cerr.
00066     void Dump(void) const;      //*MENU*
00067 
00068     /// Print to cout (without extraneous bits of Dump()).
00069     virtual std::ostream& PrintStream(std::ostream& os) const;
00070     virtual std::istream& ReadStream(std::istream& is);
00071 
00072     // TObject::Print().
00073     virtual void Print(Option_t *option="") const; //*MENU*
00074     virtual std::ostream& PrettyPrint(std::ostream& os) const;
00075 
00076     // TObject::Browse()
00077     /// The default implementation crashes TBrowser, doing nothing is better.
00078     virtual void Browse(TBrowser*){}
00079 
00080     /// Control if an existing value can be set.
00081     virtual bool ValuesLocked(void) const { return fValuesLocked; }
00082     /// Control if an existing value can be set.
00083     virtual void LockValues(void) { fValuesLocked = true; } //*MENU*
00084     /// Control if an existing value can be set.
00085     virtual void UnLockValues(void) { fValuesLocked = false; } //*MENU*
00086 
00087     /// Control if new key/value pairs can be added.
00088     virtual bool KeysLocked(void) const { return fKeysLocked; }
00089         /// Control if new key/value pairs can be added.
00090     virtual void LockKeys(void) { fKeysLocked = true; } //*MENU*
00091         /// Control if new key/value pairs can be added.
00092     virtual void UnLockKeys(void) { fKeysLocked = false; } //*MENU*
00093 
00094     /// Access an internal "dirty" flag TDbiRegistry maintains (but does
00095     /// not use) It will be set any time a non-const method is
00096     /// accessed, or explicitly via SetDirty().  Initially a TDbiRegistry
00097     /// is dirty (original sin?).
00098     void SetDirty(bool is_dirty = true) { fDirty = is_dirty; }
00099     bool IsDirty() { return fDirty; }
00100 
00101 
00102     void SetErrorHandler(ErrorHandler eh) { fErrorHandler = eh; }
00103 
00104 //    bool Get(const char* key, bool& b) const;
00105     /// Access a char value.  Return true and set second argument if key is
00106     /// found, else returns false.
00107     bool Get(const char* key, char& c) const;
00108     /// Access a string value.  Return true and set second argument if key is
00109     /// found, else returns false.
00110     bool Get(const char* key, const char*& s) const;
00111     /// Access an int value.  Return true and set second argument if key is
00112     /// found, else returns false.
00113     bool Get(const char* key, int& i) const;
00114     /// Access a double  value.  Return true and set second argument if key is
00115     /// found, else returns false.
00116     bool Get(const char* key, double& d) const;
00117     /// Access a TDbiRegistry value.  Return true and set second argument if key is
00118     /// found, else returns false.
00119     bool Get(const char* key, TDbiRegistry& r) const;
00120 
00121     /// Return the type_info of the value corresponding to the given
00122     /// key.  If key doesn't exist, type_info for type void is returned.
00123     const type_info& GetType(const char* key) const;
00124     /// Return "int", "double", "char", "string", "TDbiRegistry" or "void"
00125     std::string GetTypeAsString(const char* key) const;
00126     /// see format.txt
00127     std::string GetValueAsString(const char* key) const;
00128 
00129 
00130    
00131 //    bool        GetBool(const char* key) const;
00132  /// Access a char value.  Returns value if key lookup succeeds, else
00133     /// prints warning message.  Use above Get() methods for a safer
00134     /// access method.
00135     char        GetChar(const char* key) const;
00136      /// Access a string value.  Returns value if key lookup succeeds, else
00137     /// prints warning message.  Use above Get() methods for a safer
00138     /// access method.
00139     const char* GetCharString(const char* key) const;
00140      /// Access an int value.  Returns value if key lookup succeeds, else
00141     /// prints warning message.  Use above Get() methods for a safer
00142     /// access method.
00143     int         GetInt(const char* key) const;
00144      /// Access a double value.  Returns value if key lookup succeeds, else
00145     /// prints warning message.  Use above Get() methods for a safer
00146     /// access method.
00147     double      GetDouble(const char* key) const;
00148      /// Access an TDbiRegistry value.  Returns value if key lookup succeeds, else
00149     /// prints warning message.  Use above Get() methods for a safer
00150     /// access method.
00151     TDbiRegistry    GetTDbiRegistry(const char* key) const;
00152 //    bool Set(const char* key, bool b);
00153     /// Set the value associated with the given key.  Return false if
00154     /// locks prevent setting or if type mismatch.
00155 
00156     bool Set(const char* key, char c);
00157     bool Set(const char* key, const char* s);
00158     bool Set(const char* key, int i);
00159     bool Set(const char* key, double d);
00160     bool Set(const char* key, TDbiRegistry r);
00161 /// Container for a registry key. Contains a std::map of key value pairs where key 
00162 /// is a string and value is a registry item. Also stores pointer to a TDbiRegistry object.
00163     class TDbiRegistryKey
00164     {
00165 
00166     public:
00167         TDbiRegistryKey();
00168         TDbiRegistryKey(const TDbiRegistry* r);
00169         virtual ~TDbiRegistryKey();
00170 
00171         const char* operator()(void);
00172 
00173     private:
00174 
00175         const TDbiRegistry* fReg;
00176         std::map<std::string,TDbiRegistryItem*>::iterator fIt;
00177     };                              // end of class TDbiRegistryKey
00178 
00179     TDbiRegistryKey Key(void) const;
00180 
00181 private:
00182     bool fValuesLocked;
00183     bool fKeysLocked;
00184     ErrorHandler fErrorHandler;  //! not written out
00185 #ifndef __CINT__
00186     friend class TDbiRegistryKey;
00187 //    template<class T> bool SetPtr(std::string key, T* val);
00188     tRegMap fMap;
00189 #endif
00190     bool fDirty;
00191 
00192     ClassDef(TDbiRegistry,1)
00193 };                              // end of class TDbiRegistry
00194 
00195 
00196 inline std::ostream& operator<<(std::ostream& os, const TDbiRegistry& r) { return r.PrintStream(os); }
00197 
00198 
00199 #include "TDbiRegistryItemXxx.hxx"
00200 
00201 #endif  // TDBIREGISTRY_H

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1