All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
ParameterGroup.hpp
1 //===========================================================================
2 //
3 // File: ParameterGroup.hpp
4 //
5 // Created: Tue Jun 2 19:11:11 2009
6 //
7 // Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
8 // Atgeirr F Rasmussen <atgeirr@sintef.no>
9 //
10 // $Date$
11 //
12 // $Revision$
13 //
14 //===========================================================================
15 
16 /*
17  Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
18  Copyright 2009, 2010 Statoil ASA.
19 
20  This file is part of the Open Porous Media project (OPM).
21 
22  OPM is free software: you can redistribute it and/or modify
23  it under the terms of the GNU General Public License as published by
24  the Free Software Foundation, either version 3 of the License, or
25  (at your option) any later version.
26 
27  OPM is distributed in the hope that it will be useful,
28  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  GNU General Public License for more details.
31 
32  You should have received a copy of the GNU General Public License
33  along with OPM. If not, see <http://www.gnu.org/licenses/>.
34 */
35 
36 #ifndef OPM_PARAMETERGROUP_HEADER
37 #define OPM_PARAMETERGROUP_HEADER
38 
39 #include <exception>
40 #include <map>
41 #include <string>
42 #include <vector>
43 #include <memory>
44 #include <opm/core/utility/parameters/ParameterMapItem.hpp>
45 #include <opm/core/utility/parameters/ParameterRequirement.hpp>
46 
47 namespace Opm {
70  // for parameter ``param1'' lying at the root and ``param2'' in the group
82  public:
83  struct NotFoundException : public std::exception {};
84  struct WrongTypeException : public std::exception {};
85  struct ConversionFailedException : public std::exception {};
86 
87  template<typename T>
88  struct RequirementFailedException : public std::exception {};
89 
91  ParameterGroup(const std::string& path, const ParameterGroup* parent,
92  const bool enable_output);
93 
94  // From ParameterMapItem
95  virtual ~ParameterGroup();
96  virtual std::string getTag() const;
97 
114  template <typename StringArray>
115  ParameterGroup(int argc, StringArray argv, const bool verify_syntax = true,
116  const bool enabled_output=true);
117 
124  bool has(const std::string& name) const;
125 
135  template<typename T>
136  T get(const std::string& name) const;
137 
138  template<typename T, class Requirement>
139  T get(const std::string& name, const Requirement&) const;
140 
156  template<typename T>
157  T getDefault(const std::string& name,
158  const T& default_value) const;
159 
160  template<typename T, class Requirement>
161  T getDefault(const std::string& name,
162  const T& default_value,
163  const Requirement& r) const;
164 
170  ParameterGroup getGroup(const std::string& name) const;
171 
174  void disableOutput();
175 
178  void enableOutput();
179 
185  bool isOutputEnabled() const;
186 
191  // The '/' separates ParameterGroups and Parameters
200  void readParam(const std::string& param_filename);
201 
206  void writeParam(const std::string& param_filename) const;
207 
211  void writeParamToStream(std::ostream& stream) const;
212 
213 
215  template<typename T>
216  void get(const char* name, T& value, const T& default_value) const {
217  value = this->getDefault<T>(name, default_value);
218  }
219 
221  template<typename T>
222  void get(const char* name, T& value) const {
223  value = this->get<T>(name);
224  }
225 
227  bool anyUnused() const;
228 
230  void displayUsage(bool used_params = false) const;
231 
233  std::string path() const;
234 
236  void insert(const std::string& name,
237  const std::shared_ptr<ParameterMapItem>& data);
238 
240  void insertParameter(const std::string& name, const std::string& value);
241 
243  const std::vector<std::string>& unhandledArguments() const;
244 
245  private:
246  typedef std::shared_ptr<ParameterMapItem> data_type;
247  typedef std::pair<std::string, data_type> pair_type;
248  typedef std::map<std::string, data_type> map_type;
249 
250  std::string path_;
251  map_type map_;
252  const ParameterGroup* parent_;
253  bool output_is_enabled_;
254  std::vector<std::string> unhandled_arguments_;
255 
256  template<typename T, class Requirement>
257  T translate(const pair_type& data, const Requirement& chk) const;
258  template <typename StringArray>
259  void parseCommandLineArguments(int argc, StringArray argv, bool verify_syntax);
260  void recursiveSetIsOutputEnabled(bool output_is_enabled);
261 
262  // helper routines to do textual I/O
263  template <typename T>
264  static std::string to_string(const T& val);
265  static std::pair<std::string, std::string>
266  filename_split(const std::string& filename);
267  };
268 } // namespace Opm
269 
270 #include <opm/core/utility/parameters/ParameterGroup_impl.hpp>
271 
272 #endif // OPM_PARAMETERGROUP_HEADER
bool anyUnused() const
Return true if any parameters are unused.
Definition: ParameterGroup.cpp:265
T getDefault(const std::string &name, const T &default_value) const
This method is used to read a parameter from the parameter group.
Definition: ParameterGroup_impl.hpp:214
void enableOutput()
Enables the output from get, getDefault and getGroup.
Definition: ParameterGroup.cpp:307
std::string path() const
Returns the path of the parameter group.
Definition: ParameterGroup.cpp:82
Definition: ParameterGroup.hpp:83
void insertParameter(const std::string &name, const std::string &value)
Insert a new parameter item into the group.
Definition: ParameterGroup.cpp:200
Definition: ParameterGroup.hpp:88
The parameter handlig system is structured as a tree, where each node inhertis from ParameterMapItem...
Definition: ParameterMapItem.hpp:47
void writeParam(const std::string &param_filename) const
Writes this ParameterGroup into a param file specified by param_filename.
Definition: ParameterGroup.cpp:138
void insert(const std::string &name, const std::shared_ptr< ParameterMapItem > &data)
Insert a new item into the group.
Definition: ParameterGroup.cpp:168
void readParam(const std::string &param_filename)
Reads the contents of the param file specified by param_filename into this ParameterGroup.
Definition: ParameterGroup.cpp:106
bool has(const std::string &name) const
This method checks if there is something with name name in the parameter gropup.
Definition: ParameterGroup.cpp:68
bool isOutputEnabled() const
Returs true if and only if output from get, getDefault and getGroup is enabled.
Definition: ParameterGroup.cpp:311
Definition: ParameterGroup.hpp:85
void displayUsage(bool used_params=false) const
Shows which parameters which are used or unused.
Definition: ParameterGroup.cpp:285
const std::vector< std::string > & unhandledArguments() const
Unhandled arguments from command line parsing.
Definition: ParameterGroup.cpp:326
Definition: ParameterGroup.hpp:84
ParameterGroup is a class that is used to provide run-time parameters.
Definition: ParameterGroup.hpp:81
void disableOutput()
Disables the output from get, getDefault and getGroup.
Definition: ParameterGroup.cpp:303
ParameterGroup getGroup(const std::string &name) const
This method returns the parameter group given by name, i.e.
Definition: ParameterGroup.cpp:94
virtual std::string getTag() const
This function returns a string describing the ParameterMapItem.
Definition: ParameterGroup.cpp:63
void writeParamToStream(std::ostream &stream) const
Writes this ParameterGroup to a stream.
Definition: ParameterGroup.cpp:148