table.hh

Go to the documentation of this file.
00001 /*************************************************************************
00002 
00003 Copyright Rice University, 2004.
00004 All rights reserved.
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a
00007 copy of this software and associated documentation files (the "Software"),
00008 to deal in the Software without restriction, including without limitation
00009 the rights to use, copy, modify, merge, publish, distribute, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, provided that the above copyright notice(s) and this
00012 permission notice appear in all copies of the Software and that both the
00013 above copyright notice(s) and this permission notice appear in supporting
00014 documentation.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
00019 RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
00020 NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
00021 DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00022 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00023 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
00024 THIS SOFTWARE.
00025 
00026 Except as contained in this notice, the name of a copyright holder shall
00027 not be used in advertising or otherwise to promote the sale, use or other
00028 dealings in this Software without prior written authorization of the
00029 copyright holder.
00030 
00031 **************************************************************************/
00032 
00033 #ifndef TABLE_H
00034 #define TABLE_H
00035 
00036 #include "std_cpp_includes.hh"
00037 #include "except.hh"
00038 #include "utility.hh"
00039 
00040 namespace RVL {
00041 
00042   // ASCII Parameter Table Object.
00043   //    This class implements the HCL associative array (hash table)
00044   //    interface defined in the {\bf AbstractTable} base class.
00045 
00046   //    The principal Table constructor reads the Table's internal data
00047   //    from a "config" file. Each line of the config file is either of
00048   //    the form "Key = Value" or "Object::Key = Value".  The first form
00049   //    initializes the parameter associated to "Key" in the table to the
00050   //    value "Value". The second form facilitates the use of one config
00051   //    file for several different objects. If the "Object" matches one of
00052   //    the object names provided by the user (as argument to the
00053   //    constructor, see below), then the parameter is read/written as
00054   //    before. Otherwise, the line is ignored.  The type of "Value" can
00055   //    be integer, float, double, or string. String values are optionally
00056   //    enclosed by double-quotes.
00057 
00058 
00059   class Table {
00060 
00061   private:
00062 
00063     typedef map<string,string> str_map;
00064     typedef str_map::iterator iterator;
00065     typedef str_map::const_iterator const_iterator;
00066     typedef str_map::value_type value_type;
00067   
00068     string prefixes;
00069     str_map table;
00070 
00071     static bool isPunct(char c);
00072     static string stripWord(const string word);
00073   
00074   public:
00075 
00076     // usual constructor
00077     Table(string fname="",string prefixes="");
00078 
00079     // Copy constructor 
00080     Table(const Table & T);
00081 
00082     // Destructor 
00083     virtual ~Table();
00084 
00085     // Merge entries from a file into the parameter table 
00086     int mergeFile(string fname);
00087 
00088     // Returns size of the table
00089     int getSize() const { return table.size(); }
00090 
00091     // Returns ith key (do we want this one?)
00092     string getKey(unsigned i);
00093 
00094     // Returns ith value (do we want this one?)
00095     string strValue(string key);
00096 
00097     // Methods to access values in the table 
00098     int getValue(string key,int & value) const;
00099     int getValue(string key,double & value) const;
00100     int getValue(string key,float & value) const;
00101     int getValue(string key,string & value,int length=0) const;
00102   
00103     // Methods to insert new pairs (key-value) in the table 
00104     void putValue(string key,int value);
00105     void putValue(string key,float value);
00106     void putValue(string key,double value);
00107     void putValue(string key,const string value);
00108 
00109     // idem (?do we really need those?)
00110     int getArrayValue(string key,int ind,int & value) const;
00111     int getArrayValue(string key,int ind,float & value) const;
00112     int getArrayValue(string key,int ind,double & value) const;
00113     int getArrayValue(string key,int ind,string & value,int length=0) const;
00114 
00115     // idem (?do we really need those?)
00116     void putArrayValue(string key,int ind,int value);
00117     void putArrayValue(string key,int ind,float value);
00118     void putArrayValue(string key,int ind,double value);
00119     void putArrayValue(string key,int ind,const string value);
00120   
00121     ostream & write(ostream & os);
00122   
00123   };
00124 
00125   ostream & operator << (ostream & os,Table & t);
00126 
00127   // convenient helper functions added 12.05 WWS
00128   // deprecated 04.07 WWS
00129   string getStringFromTable(Table const & par, string key);
00130   int getIntFromTable(Table const & par, string key);
00131   float getFloatFromTable(Table const & par, string key);
00132   double getDoubleFromTable(Table const & par, string key);
00133 
00134   template<typename Scalar>
00135   Scalar getValueFromTable(Table const & par, string key);
00136 
00137   template<>
00138   int getValueFromTable<int>(Table const & par, string key);
00139 
00140   template<>
00141   double getValueFromTable<double>(Table const & par, string key);
00142 
00143   template<>
00144   float getValueFromTable<float>(Table const & par, string key);
00145 
00146   template<>
00147   string getValueFromTable<string>(Table const & par, string key);
00148   
00149   template<>
00150   bool getValueFromTable<bool>(Table const & par, string key);
00151   
00152 }
00153 
00154 #endif
00155 
00156 

Generated on 5 Jan 2017 for RvlUmin by  doxygen 1.4.7