NASA - Jet Propulsion Laboratory
    + View the NASA Portal
Search JPL
Jet Propulsion Laboratory Home Earth Solar System Stars & Galaxies Technology
Introduction Background Software Links

paramfile.h

00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  Healpix_cxx is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.jpl.nasa.gov
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*
00028  *  Class for parsing parameter files
00029  *
00030  *  Copyright (C) 2003, 2004, 2005 Max-Planck-Society
00031  *  Authors: Martin Reinecke, Reinhard Hell
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

Generated on Fri Jun 18 16:12:29 2010 for LevelS C++ support library
Privacy / Copyright
FIRST GOV Contact: NASA Home Page Site Manager:
Webmaster:

CL 03-2650