ParameterGroup.hpp
Go to the documentation of this file.
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>
46 
47 namespace Opm {
48  namespace parameter {
72  // for parameter ``param1'' lying at the root and ``param2'' in the group
110  public:
111  struct NotFoundException : public std::exception {};
112  struct WrongTypeException : public std::exception {};
113  struct ConversionFailedException : public std::exception {};
114 
115  template<typename T>
116  struct RequirementFailedException : public std::exception {};
117 
118  ParameterGroup();
119  ParameterGroup(const std::string& path, const ParameterGroup* parent,
120  const bool enable_output);
121 
122  // From ParameterMapItem
123  virtual ~ParameterGroup();
124  virtual std::string getTag() const;
125 
142  template <typename StringArray>
143  ParameterGroup(int argc, StringArray argv, const bool verify_syntax = true,
144  const bool enabled_output=true);
145 
152  bool has(const std::string& name) const;
153 
163  template<typename T>
164  T get(const std::string& name) const;
165 
166  template<typename T, class Requirement>
167  T get(const std::string& name, const Requirement&) const;
168 
184  template<typename T>
185  T getDefault(const std::string& name,
186  const T& default_value) const;
187 
188  template<typename T, class Requirement>
189  T getDefault(const std::string& name,
190  const T& default_value,
191  const Requirement& r) const;
192 
198  ParameterGroup getGroup(const std::string& name) const;
199 
202  void disableOutput();
203 
206  void enableOutput();
207 
213  bool isOutputEnabled() const;
214 
215 
220  void readXML(const std::string& xml_filename);
221 
226  // The '/' separates ParameterGroups and Parameters
235  void readParam(const std::string& param_filename);
236 
241  void writeParam(const std::string& param_filename) const;
242 
246  void writeParamToStream(std::ostream& stream) const;
247 
248 
250  template<typename T>
251  void get(const char* name, T& value, const T& default_value) const {
252  value = this->getDefault<T>(name, default_value);
253  }
254 
256  template<typename T>
257  void get(const char* name, T& value) const {
258  value = this->get<T>(name);
259  }
260 
262  bool anyUnused() const;
263 
265  void displayUsage(bool used_params = false) const;
266 
268  std::string path() const;
269 
271  void insert(const std::string& name,
272  const std::shared_ptr<ParameterMapItem>& data);
273 
275  void insertParameter(const std::string& name, const std::string& value);
276 
278  const std::vector<std::string>& unhandledArguments() const;
279 
280  private:
281  typedef std::shared_ptr<ParameterMapItem> data_type;
282  typedef std::pair<std::string, data_type> pair_type;
283  typedef std::map<std::string, data_type> map_type;
284 
285  std::string path_;
286  map_type map_;
287  const ParameterGroup* parent_;
288  bool output_is_enabled_;
289  std::vector<std::string> unhandled_arguments_;
290 
291  template<typename T, class Requirement>
292  T translate(const pair_type& data, const Requirement& chk) const;
293  template <typename StringArray>
294  void parseCommandLineArguments(int argc, StringArray argv, bool verify_syntax);
295  void recursiveSetIsOutputEnabled(bool output_is_enabled);
296 
297  // helper routines to do textual I/O
298  template <typename T>
299  static std::string to_string(const T& val);
300  static std::pair<std::string, std::string>
301  filename_split(const std::string& filename);
302  };
303  } // namespace parameter
304 } // namespace Opm
305 
307 
308 #endif // OPM_PARAMETERGROUP_HEADER
Definition: AnisotropicEikonal.hpp:43
Definition: ParameterGroup.hpp:109
bool isOutputEnabled() const
Returs true if and only if output from get, getDefault and getGroup is enabled.
void disableOutput()
Disables the output from get, getDefault and getGroup. By default, such output is enabled...
void readXML(const std::string &xml_filename)
Reads the contents of the xml file specified by xml_filename into this ParameterGroup.
void displayUsage(bool used_params=false) const
Shows which parameters which are used or unused.
void enableOutput()
Enables the output from get, getDefault and getGroup. By default, such output is enabled.
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:226
Definition: ParameterGroup.hpp:111
bool anyUnused() const
Return true if any parameters are unused.
Definition: ParameterMapItem.hpp:48
ParameterGroup getGroup(const std::string &name) const
This method returns the parameter group given by name, i.e. it is an alias of get().
void insert(const std::string &name, const std::shared_ptr< ParameterMapItem > &data)
Insert a new item into the group.
void writeParam(const std::string &param_filename) const
Writes this ParameterGroup into a param file specified by param_filename.
void readParam(const std::string &param_filename)
Reads the contents of the param file specified by param_filename into this ParameterGroup.
bool has(const std::string &name) const
This method checks if there is something with name name in the parameter gropup.
std::string path() const
Returns the path of the parameter group.
Definition: ParameterGroup.hpp:112
const std::vector< std::string > & unhandledArguments() const
Unhandled arguments from command line parsing.
virtual std::string getTag() const
This function returns a string describing the ParameterMapItem.
void insertParameter(const std::string &name, const std::string &value)
Insert a new parameter item into the group.
void writeParamToStream(std::ostream &stream) const
Writes this ParameterGroup to a stream.