LevelS C++ support library  3.82
paramfile.h
Go to the documentation of this file.
1 /*
2  * This file is part of libcxxsupport.
3  *
4  * libcxxsupport is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * libcxxsupport is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with libcxxsupport; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22  * (DLR).
23  */
24 
25 /*! \file paramfile.h
26  * Class for parsing parameter files
27  *
28  * Copyright (C) 2003-2014 Max-Planck-Society
29  * Authors: Martin Reinecke
30  */
31 
32 #ifndef PLANCK_PARAMFILE_H
33 #define PLANCK_PARAMFILE_H
34 
35 #include <map>
36 #include <set>
37 #include <string>
38 #include "datatypes.h"
39 #include "string_utils.h"
40 
41 /*! Class for storing and querying key/value pairs. The name is historical;
42  the parameters can actually be obtained from othersources as well
43  (e.g. the command line). */
44 class paramfile
45  {
46  private:
47  typedef std::map<std::string,std::string> params_type;
48  params_type params;
49  mutable std::set<std::string> read_params;
50  bool verbose;
51 
52  std::string get_valstr(const std::string &key) const;
53  bool param_unread (const std::string &key) const;
54  void findhelper (const std::string &key, const std::string &value, NDT type,
55  bool deflt) const;
56  void setParamString (const std::string &key, const std::string &value);
57 
58  public:
59  paramfile() : verbose(true) {}
60  /*! Constructs a paramfile object from the contents of \a filename.
61  If \a verbose_==true, diagnostic output is generated when calling
62  methods on this object, otherwise not. */
63  paramfile (const std::string &filename, bool verbose_=true);
64  /*! Constructs a paramfile object from the contents of \a par.
65  If \a verbose_==true, diagnostic output is generated when calling
66  methods on this object, otherwise not. */
67  paramfile (const params_type &par, bool verbose_=true);
68  ~paramfile();
69 
70  /*! Allows adjusting the verbosity. */
71  void setVerbosity (bool verbose_)
72  { verbose = verbose_; }
73 
74  /*! Returns the verbosity setting of the object. */
75  bool getVerbosity () const
76  { return verbose; }
77 
78  /*! Returns \c true, if a paremeter called \a key is stored in the object,
79  else \c false. */
80  bool param_present(const std::string &key) const;
81 
82  /*! Returns the value stored for the parameter name \a key, after converting
83  it to the requested type. If \a key is not present, an exception is
84  thrown. */
85  template<typename T> T find (const std::string &key) const;
86  /*! Returns the value stored for the parameter name \a key, after converting
87  it to the requested type. If \a key is not present, \a deflt is returned
88  instead, and is also entered into the parameter set. */
89  template<typename T> T find
90  (const std::string &key, const T &deflt);
91 
92  /*! Returns the entire set of currently stored parameters. */
93  const params_type &getParams() const
94  { return params; }
95 
96  /*! Sets the parameter with the name \a key to \a value. */
97  template<typename T> void setParam (const std::string &key, const T &value)
98  { setParamString(key,dataToString(value)); }
99  };
100 
101 /*! Tries to build a \a paramfile object from the contents of a command line.
102  If \a argc==2 and \a argv[1] does not contain the character "=", the
103  function tries to input parameters from the file \a argv[1]. Otherwise
104  the function interprets each command line argument as a "key=value"
105  statement. */
106 paramfile getParamsFromCmdline (int argc, const char **argv,
107  bool verbose=true);
108 
109 #endif
T find(const std::string &key) const
paramfile getParamsFromCmdline(int argc, const char **argv, bool verbose=true)
Definition: paramfile.cc:155
NDT
Definition: datatypes.h:249
bool param_present(const std::string &key) const
Definition: paramfile.cc:66
string dataToString(const T &x)
Definition: string_utils.cc:52
bool getVerbosity() const
Definition: paramfile.h:75
void setVerbosity(bool verbose_)
Definition: paramfile.h:71
void setParam(const std::string &key, const T &value)
Definition: paramfile.h:97
const params_type & getParams() const
Definition: paramfile.h:93

Generated on Thu Jul 28 2022 17:32:06 for LevelS C++ support library