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 <string>
00039 #include <iostream>
00040 #include "simparams.h"
00041 #include "cxxutils.h"
00042
00043 class paramfile
00044 {
00045 private:
00046 std::map<std::string,std::string> params;
00047 bool verbose;
00048
00049 std::string get_valstr(const std::string &key) const
00050 {
00051 std::map<std::string,std::string>::const_iterator loc=params.find(key);
00052 if (loc!=params.end()) return loc->second;
00053 throw Message_error ("Error: Cannot find the key \"" + key + "\".");
00054 }
00055
00056 public:
00057 paramfile (const std::string &filename, bool verbose_=true)
00058 : verbose(verbose_)
00059 { parse_file (filename, params); }
00060
00061 paramfile (const std::map<std::string,std::string> &par)
00062 : params (par), verbose(true)
00063 {}
00064
00065 bool param_present(const std::string &key) const
00066 { return (params.find(key)!=params.end()); }
00067
00068 template<typename T> T find (const std::string &key) const
00069 {
00070 T result;
00071 stringToData(get_valstr(key),result);
00072 if (verbose)
00073 std::cout << "Parser: " << key << " = " << dataToString(result)
00074 << std::endl;
00075 return result;
00076 }
00077 template<typename T> T find
00078 (const std::string &key, const T &deflt)
00079 {
00080 if (param_present(key)) return find<T>(key);
00081 if (verbose)
00082 std::cout << "Parser: " << key << " = " << dataToString(deflt)
00083 << " <default>" << std::endl;
00084 params[key]=dataToString(deflt);
00085 return deflt;
00086 }
00087
00088 template<typename T> void findParam
00089 (const std::string &key, T &value) const
00090 { value = find<T>(key); }
00091
00092 template<typename T> void findHeaderParam(const std::string& key,
00093 T& value, simparams& headerParams, const std::string& headerKey,
00094 const std::string& headerComment) const
00095 {
00096 findParam(key, value);
00097 headerParams.add(key, headerKey, dataToString(value), headerComment);
00098 }
00099 void findSourceParam(const std::string& key, std::string& value,
00100 simparams& headerParams) const
00101 {
00102 findParam(key, value);
00103 headerParams.add_source_file(value);
00104 }
00105 };
00106
00107 #endif