
00001 /*!@file Component/ParamMap.H I/O operations for parameter files */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters@klab.caltech.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Component/ParamMap.H $ 00035 // $Id: ParamMap.H 7095 2006-09-01 18:07:02Z rjpeters $ 00036 // 00037 00038 #ifndef PARAMMAP_H_DEFINED 00039 #define PARAMMAP_H_DEFINED 00040 00041 #include "rutz/shared_ptr.h" 00042 00043 #include <iosfwd> 00044 #include <vector> 00045 00046 //! Allows reading and writing of parameters to and from files or streams 00047 /*! ParamMap allows reading and writing of arbitrarily nested data structes 00048 to and from files or other streams. */ 00049 class ParamMap 00050 { 00051 ParamMap(const ParamMap&); // not allowed 00052 ParamMap& operator=(const ParamMap&); // not allowed 00053 00054 public: 00055 //! Convenience function to create a new ParamMap from a .pmap file. 00056 static rutz::shared_ptr<ParamMap> loadPmapFile(const std::string& fname); 00057 00058 //! Compatibility function to create a new ParamMap from a .conf file. 00059 /*! See readConfig for details on the format of a .conf file. Basically, 00060 it is similar to the .pmap format, except that comments must be 00061 bracketed by whitespace-separated '#' characters on both sides, and 00062 that there is no nesting of data structures. */ 00063 static rutz::shared_ptr<ParamMap> loadConfFile(const std::string& fname); 00064 00065 //! Return codes when getting params from the map. 00066 enum ReturnCode 00067 { 00068 MISSING, //!< the param was not found 00069 UNCHANGED, //!< the param was found, but the value was unchanged 00070 CHANGED, //!< the param was found and its value was changed 00071 }; 00072 00073 //! Default construct with an empty map. 00074 ParamMap(); 00075 00076 //! Construct and load() from \a fname. 00077 ParamMap(const std::string& fname); 00078 00079 //! Destructor. 00080 ~ParamMap(); 00081 00082 //! A class for iterating over the ParamMap's keys (i.e. param names). 00083 class key_iterator; 00084 00085 //! Get an iterator to the beginning of the keys sequence. 00086 key_iterator keys_begin() const; 00087 00088 //! Get an iterator to one-past-the-end of the keys sequence. 00089 key_iterator keys_end() const; 00090 00091 //! Load in a parameter map from the given input stream. 00092 void load(std::istream& istrm); 00093 00094 //! Convenience overload of the other load(). 00095 /*! In this case we just build an istream internally from the given 00096 filename, and then load() from that.*/ 00097 void load(const std::string& fname); 00098 00099 //! Write a formatted parameter map to the given output stream. 00100 /*! The indentlev parameter is only used internally by ParamMap, and can 00101 be safely ignored by external callers. */ 00102 void format(std::ostream& ostrm, int indentlev = 0) const; 00103 00104 //! Convenience overload of the other format(). 00105 /*! In this case we just build an ostream internally from the given 00106 filename, and then format() from that.*/ 00107 void format(const std::string& fname) const; 00108 00109 //! Query whether the parameter map has a particular named parameter. 00110 bool hasParam(const std::string& paramname) const; 00111 00112 //! Query whether the parameter map is a leaf or not 00113 bool isLeaf(const std::string& paramname) const; 00114 00115 // ###################################################################### 00116 /*! @name Functions to extract named values from the ParamMap 00117 00118 There are several kinds of functions that differ in how they handle 00119 named parameters that are not found in the ParamMap. The first kind 00120 look for a named parameter, and trigger and error if that parameter 00121 is not found. The second kind take a default value as an additional 00122 argument, and return that value if the named parameter is not 00123 found. The third kind take a destination by reference, and leave that 00124 destination unchanged if the parameter is not found, and additional 00125 return a ReturnCode specifying with the parameter was missing, 00126 found-and-changed, or found-and-unchanged. */ 00127 //@{ 00128 00129 //! Get a named parameter as a nested parameter map. 00130 /*! Triggers an error if there is no parameter with the given name. */ 00131 rutz::shared_ptr<ParamMap> getSubpmap(const std::string& paramname) const; 00132 00133 //! Get a named parameter as a string value. 00134 /*! Triggers an error if there is no parameter with the given name. */ 00135 std::string getStringParam(const std::string& paramname) const; 00136 00137 //! Get a named parameter as a 'double' value. 00138 /*! Triggers an error if there is no parameter with the given name. */ 00139 double getDoubleParam(const std::string& paramname) const; 00140 00141 //! Get a named parameter as an 'int' value. 00142 /*! Triggers an error if there is no parameter with the given name. */ 00143 int getIntParam(const std::string& paramname) const; 00144 00145 //! Get a named parameter as a string value, or return the default value. 00146 /*! The default value will be returned if the named parameter is not 00147 found.*/ 00148 std::string getStringParam(const std::string& paramname, const std::string& defval) const; 00149 00150 //! Get a named parameter as a 'double' value, or return the default value. 00151 /*! The default value will be returned if the named parameter is not 00152 found.*/ 00153 double getDoubleParam(const std::string& paramname, const double defval) const; 00154 00155 //! Get a named parameter as an 'int' value, or return the default value. 00156 /*! The default value will be returned if the named parameter is not 00157 found.*/ 00158 int getIntParam(const std::string& paramname, const int defval) const; 00159 00160 //! Returns the submap for the given name. 00161 /*! If a submap for the given name exists, a rutz::shared_ptr to it 00162 is returned. If no submap for the given name has yet been 00163 created, one is created, and a rutz::shared_ptr to it is 00164 returned. This function differs from getSubpmap(), which only 00165 returns existing submaps. */ 00166 rutz::shared_ptr<ParamMap> lookupSubpmap(const std::string& paramname); 00167 00168 //! Get the named parameter, or do nothing if the parameter is missing. 00169 /*! The ReturnCode describes what happened when looking up the param. */ 00170 ReturnCode queryStringParam(const std::string& paramname, std::string& result) const; 00171 00172 //! Get the named parameter, or do nothing if the parameter is missing. 00173 /*! The ReturnCode describes what happened when looking up the param. */ 00174 ReturnCode queryDoubleParam(const std::string& paramname, double& result) const; 00175 00176 //! Get the named parameter, or do nothing if the parameter is missing. 00177 /*! The ReturnCode describes what happened when looking up the param. */ 00178 ReturnCode queryIntParam(const std::string& paramname, int& result) const; 00179 00180 //@} 00181 00182 // ###################################################################### 00183 /*! @name Functions to place named values into the ParamMap */ 00184 00185 //! Add a nested parameter map with a given name. 00186 void putSubpmap(const std::string& paramname, const rutz::shared_ptr<ParamMap>& val); 00187 00188 //! Add a string value with a given name. 00189 void putStringParam(const std::string& paramname, const std::string& val); 00190 00191 //! Add a double-precision floating point value with a given name. 00192 void putDoubleParam(const std::string& paramname, double val); 00193 00194 //! Add an integral value with a given name. 00195 void putIntParam(const std::string& paramname, int val); 00196 00197 //! Replace a submap 00198 void replaceSubpmap(const std::string& paramname, const rutz::shared_ptr<ParamMap>& val); 00199 00200 //! Replace string-param 00201 void replaceStringParam(const std::string& paramname, const std::string& val); 00202 00203 //! Replace double-param 00204 void replaceDoubleParam(const std::string& paramname, double val); 00205 00206 //! Replace int-param 00207 void replaceIntParam(const std::string& paramname, int val); 00208 00209 //! Remove all params from the ParamMap 00210 void clear(); 00211 00212 //@} 00213 00214 //! For testing/debugging only. 00215 /*! Prints the raw contents of this parameter map to standard output. */ 00216 void print(const std::string& name) const; 00217 00218 private: 00219 struct Impl; 00220 Impl* const rep; 00221 }; 00222 00223 class ParamMap::key_iterator 00224 { 00225 public: 00226 //! Destructor. 00227 ~key_iterator(); 00228 //! Copy constructor. 00229 key_iterator(const key_iterator& other); 00230 //! Assignment operator. 00231 key_iterator& operator=(const key_iterator&); 00232 00233 //! Dereference to get the key (i.e., parameter name). 00234 const std::string& operator*() const; 00235 00236 //! Pre-increment operator. 00237 key_iterator& operator++(); 00238 00239 //! Equality comparison. 00240 bool operator==(const key_iterator& other) const; 00241 00242 //! Inequality comparison. 00243 bool operator!=(const key_iterator& other) const; 00244 00245 private: 00246 void swap(key_iterator& other); 00247 00248 //! Default construct. 00249 key_iterator(); 00250 00251 friend class ParamMap; 00252 00253 struct IterRep; 00254 IterRep* const rep; 00255 }; 00256 00257 // ###################################################################### 00258 /* So things look consistent in everyone's emacs... */ 00259 /* Local Variables: */ 00260 /* indent-tabs-mode: nil */ 00261 /* End: */ 00262 00263 #endif // !PARAMMAP_H_DEFINED
1.4.4