paramfile.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef PLANCK_PARAMFILE_H
00035 #define PLANCK_PARAMFILE_H
00036
00037 #include <map>
00038 #include <set>
00039 #include <string>
00040 #include <iostream>
00041 #include "simparams.h"
00042 #include "cxxutils.h"
00043
00044 class paramfile
00045 {
00046 private:
00047 typedef std::map<std::string,std::string> params_type;
00048 params_type params;
00049 mutable std::set<std::string> read_params;
00050 bool verbose;
00051
00052 std::string get_valstr(const std::string &key) const
00053 {
00054 params_type::const_iterator loc=params.find(key);
00055 if (loc!=params.end()) return loc->second;
00056 throw Message_error ("Error: Cannot find the key \"" + key + "\".");
00057 }
00058
00059 public:
00060 paramfile (const std::string &filename, bool verbose_=true)
00061 : verbose(verbose_)
00062 { parse_file (filename, params); }
00063
00064 paramfile (const params_type &par)
00065 : params (par), verbose(true)
00066 {}
00067
00068 ~paramfile ()
00069 {
00070 if (verbose)
00071 for (params_type::const_iterator loc=params.begin();
00072 loc!=params.end(); ++loc)
00073 if (read_params.find(loc->first)==read_params.end())
00074 std::cout << "Parser warning: unused parameter "
00075 << loc->first << std::endl;
00076 }
00077
00078 bool param_present(const std::string &key) const
00079 { return (params.find(key)!=params.end()); }
00080
00081 template<typename T> T find (const std::string &key) const
00082 {
00083 T result;
00084 stringToData(get_valstr(key),result);
00085 if (verbose)
00086 std::cout << "Parser: " << key << " = " << dataToString(result)
00087 << std::endl;
00088 read_params.insert(key);
00089 return result;
00090 }
00091 template<typename T> T find
00092 (const std::string &key, const T &deflt)
00093 {
00094 if (param_present(key)) return find<T>(key);
00095 if (verbose)
00096 std::cout << "Parser: " << key << " = " << dataToString(deflt)
00097 << " <default>" << std::endl;
00098 params[key]=dataToString(deflt);
00099 read_params.insert(key);
00100 return deflt;
00101 }
00102
00103 const params_type &getParams() const
00104 { return params; }
00105
00106 template<typename T> void findParam
00107 (const std::string &key, T &value) const
00108 { value = find<T>(key); }
00109
00110 template<typename T> void findHeaderParam(const std::string& key,
00111 T& value, simparams& headerParams, const std::string& headerKey,
00112 const std::string& headerComment) const
00113 {
00114 findParam(key, value);
00115 headerParams.add(key, headerKey, dataToString(value), headerComment);
00116 }
00117 void findSourceParam(const std::string& key, std::string& value,
00118 simparams& headerParams) const
00119 {
00120 findParam(key, value);
00121 headerParams.add_source_file(value);
00122 }
00123 };
00124
00125 #endif