CMS 3D CMS Logo

IgConfigElement.h

Go to the documentation of this file.
00001 #ifndef IGUANA_STUDIO_IG_CONFIG_ELEMENT_H
00002 # define IGUANA_STUDIO_IG_CONFIG_ELEMENT_H
00003 
00004 //<<<<<< INCLUDES                                                       >>>>>>
00005 
00006 #include "Iguana/Studio/interface/config.h"
00007 #include "Iguana/Framework/interface/IgExtension.h"
00008 #include <map>
00009 #include <string>
00010 #include <sstream>
00011 #include <vector>
00012 
00013 //<<<<<< PUBLIC DEFINES                                                 >>>>>>
00014 //<<<<<< PUBLIC CONSTANTS                                               >>>>>>
00015 //<<<<<< PUBLIC TYPES                                                   >>>>>>
00016 
00017 class QDomNode;
00018 
00019 //<<<<<< PUBLIC VARIABLES                                               >>>>>>
00020 //<<<<<< PUBLIC FUNCTIONS                                               >>>>>>
00021 //<<<<<< CLASS DECLARATIONS                                             >>>>>>
00022 
00023 class IGUANA_STUDIO_API IgConfigElement : public IgExtension
00024 {
00025     IG_DECLARE_STATE_ELEMENT (IgConfigElement);
00026 public:
00027     typedef std::map<std::string, std::string> KeyMap;
00028     IgConfigElement (IgState* state);
00029     ~IgConfigElement ();
00030 
00031     void        save(const std::string& filename = "");
00032     template <class T>
00033     void        putValue(const std::string& key, const T& value,
00034                          bool override = true);
00035     void        putValue(const std::string& key, const std::string& value,
00036                          bool override  = true);
00037     template <class T>
00038     void        putValues(const std::string& key, 
00039                          const std::vector<T>& values,
00040                          bool override = true);
00041     void        putValues(const std::string& key,
00042                          const std::vector<std::string>& values,
00043                          bool override = true);
00044     template <class T>
00045     bool        getValue(const std::string& key, T& value) const;
00046     bool        getValue(const std::string& key, std::string& value) const;
00047 
00048     template <class T>
00049     bool        getValues(const std::string& key,
00050                           std::vector<T>& values) const;
00051     bool        getValues(const std::string& key,
00052                           std::vector<std::string>& values) const;
00053 
00054     void                setPrefix (const std::string& prefix);
00055     const std::string&  prefix (void) const;
00056     
00057     const KeyMap&       getKeyMap () const;
00058 
00059 private:
00060     template<class T>
00061     void                convert(const std::string& str, T& value) const;
00062     void                convert(const std::string& str, bool& value) const;
00063 
00064     template<class T>
00065     std::string         convert(const T& value) const;
00066 
00067     void                initConfiguration ();
00068     void                processNode (const QDomNode& node,
00069                                      const std::string& path);
00070     const std::string&  substituteValue(std::string& value) const;
00071 
00072     IgState*                            m_state;
00073     KeyMap                              m_config;
00074     std::string                         m_prefix;
00075     std::string                         m_file;
00076     bool                                m_dirty;
00077 };
00078 
00079 template<class T>
00080 void IgConfigElement::convert(const std::string& str, T& value) const
00081 {
00082     std::stringstream ios;
00083     int p = ios.precision();
00084     ios.setf(std::ios::fixed);
00085     ios.precision(20);
00086     ios << str.c_str();
00087     ios >> value;
00088     ios.precision(p);
00089 }
00090 
00091 template<class T>
00092 std::string IgConfigElement::convert(const T& value) const
00093 {
00094     std::string converted;
00095     std::stringstream ios;
00096     int p = ios.precision();
00097     ios.setf(std::ios::fixed);
00098     ios.precision(20);
00099     ios << value;
00100     ios >> converted;
00101     ios.precision(p);
00102     return converted;
00103 }
00104 
00105 
00106 template<class T>
00107 bool IgConfigElement::getValue(const std::string& key, T& value) const
00108 {
00109     std::string val;
00110     if (getValue(key, val))
00111     {
00112         convert (val, value);
00113         return true;
00114     }
00115     return false;
00116 }
00117 
00118 template <class T>
00119 bool
00120 IgConfigElement::getValues(const std::string& key,
00121                               std::vector<T>& values) const
00122 {
00123     std::vector<std::string> val;
00124     if (getValues(key, val))
00125     {
00126         T val1;
00127         for(size_t i=0; i<val.size(); i++)
00128         {
00129             convert(val[i], val1);
00130             values.push_back(val1);
00131         }
00132         return true;
00133     }
00134     return false;
00135 }
00136 
00137 template <class T>
00138 void
00139 IgConfigElement::putValue(const std::string& key,
00140                           const T& value,
00141                           bool override /* = true */)
00142 { putValue (key, convert(value), override); }
00143 
00144 template <class T>
00145 void
00146 IgConfigElement::putValues(const std::string& key,
00147                           const std::vector<T>& values,
00148                           bool override /* = true */)
00149 {
00150     std::vector<std::string> vec;
00151     for(size_t i=0; i<values.size(); i++)
00152         vec.push_back(convert(values[i]));
00153     putValues (key, vec, override);
00154 }
00155 
00156 
00157 //<<<<<< INLINE PUBLIC FUNCTIONS                                        >>>>>>
00158 //<<<<<< INLINE MEMBER FUNCTIONS                                        >>>>>>
00159 
00160 #endif // IGUANA_STUDIO_IG_CONFIG_ELEMENT_H

Generated on Tue Jun 9 17:38:48 2009 for CMSSW by  doxygen 1.5.4