Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

PropertySet.h

Go to the documentation of this file.
00001 #ifndef Impala_Util_PropertySet_h
00002 #define Impala_Util_PropertySet_h
00003 
00004 #include "Basis/String.h"
00005 #include "Util/IOBuffer.h"
00006 
00007 namespace Impala
00008 {
00009 namespace Util
00010 {
00011 
00012 class PropertySet
00013 {
00014 public:
00015     /* please note that copy contructor or functions are not necessary because
00016        we only have 'simple' data members (i.e. no pointers) */
00017     PropertySet()
00018     {
00019     }
00020 
00025     PropertySet(const std::string& definition)
00026     {
00027         Parse(definition);
00028     }
00029 
00030     void Clear()
00031     {
00032         mNames.clear();
00033         mValues.clear();
00034     }
00035 
00036     void Parse(const std::string& definition)
00037     {
00038         Clear();
00039         std::istringstream iss(definition);
00040         //parse defintion string to initialise params
00041         while(!iss.eof())
00042         {
00043             std::string pair;
00044             iss >> pair;
00045             if(pair.length() > 0)
00046             {
00047                 std::string::size_type pos = pair.find('=');
00048                 if(pos == std::string::npos)
00049                     break;
00050                 std::string name;
00051                 std::string value;
00052                 name = pair.substr(0,pos);
00053                 value = pair.substr(pos+1);
00054                 Add(name, value);
00055             }
00056         }
00057 
00058     }
00059 
00060     std::string GetString(const std::string& name, const std::string defaultValue = "") const
00061     {
00062         for(int i=0 ; i<mNames.size() ; ++i)
00063             if(mNames[i] == name)
00064                 return mValues[i];
00065         return defaultValue;
00066     }
00067 
00068     double GetDouble(const std::string& name, double defaultValue = 0.) const
00069     {
00070         std::string val = GetString(name);
00071         if(val == "")
00072             return defaultValue;
00073         return atof(val);
00074     }
00075 
00076     int GetInt(const std::string& name, int defaultValue = 0) const
00077     {
00078         std::string val = GetString(name);
00079         if(val == "")
00080             return defaultValue;
00081         return atoi(val);
00082     }
00083 
00084     bool GetBool(const std::string& name, bool defaultValue = false) const
00085     {
00086         std::string val = GetString(name);
00087         if(val == "")
00088             return defaultValue;
00089         return StringToBool(val);
00090     }
00091 
00093     int Add(const std::string& name, double value)
00094     {
00095         return Add(name, MakeString(value));
00096     }
00097 
00099     int Add(const std::string& name, const std::string& value)
00100     {
00101         int i;
00102         for(i=0 ; i<mNames.size() ; ++i)
00103             if(mNames[i] == name)
00104             {
00105                 mValues[i] = value;
00106                 return i;
00107             }
00108         mNames.push_back(name);
00109         mValues.push_back(value);
00110         return i;
00111     }
00112 
00113     void Print(std::ostream& os) const
00114     {
00115         for(int i=0 ; i<mNames.size() ; ++i)
00116             os << mNames[i] << "=" << mValues[i] << " ";
00117     }
00118 
00119     void Print(IOBuffer* buf) const
00120     {
00121         std::string res;
00122         for(int i=0 ; i<mNames.size() ; ++i)
00123             res += mNames[i] + "=" + mValues[i] +" ";
00124         buf->Puts(res);
00125     }
00126 
00127     std::string GetDescription() const
00128     {
00129         std::ostringstream oss;
00130         Print(oss);
00131         return oss.str();
00132     }
00133 
00134     int Size() const
00135     {
00136         return mNames.size();
00137     }
00138 
00139     std::string GetName(int i) const
00140     {
00141         return mNames[i];
00142     }
00143 
00144     std::string GetValue(int i) const
00145     {
00146         return mValues[i];
00147     }
00148 
00149 private:
00150     std::vector<std::string> mNames;
00151     std::vector<std::string> mValues;
00152 };
00153 
00154 void Append(PropertySet* dst, const PropertySet* src)
00155 {
00156     int i;
00157     for(i=0 ; i<src->Size() ; ++i)
00158         dst->Add(src->GetName(i), src->GetValue(i));
00159 }
00160 
00161 std::ostream& operator<< (std::ostream& os, const PropertySet& ps)
00162 {
00163     ps.Print(os);
00164     return os;
00165 }
00166 
00167 }//namespace Util
00168 }//namespace Impala
00169 
00170 #endif
00171 

Generated on Fri Mar 19 09:31:47 2010 for ImpalaSrc by  doxygen 1.5.1