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
47namespace 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
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
171
175
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
234
237 const std::shared_ptr<ParameterMapItem>& data);
238
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
271
272#endif // OPM_PARAMETERGROUP_HEADER
const char *const name
Definition: cJSON.h:258
const char *const string
Definition: cJSON.h:170
Definition: ParameterGroup.hpp:81
std::string path() const
Returns the path of the parameter group.
void insert(const std::string &name, const std::shared_ptr< ParameterMapItem > &data)
Insert a new item into the group.
void get(const char *name, T &value, const T &default_value) const
vki param interface - deprecated
Definition: ParameterGroup.hpp:216
bool anyUnused() const
Return true if any parameters are unused.
ParameterGroup getGroup(const std::string &name) const
This method returns the parameter group given by name, i.e. it is an alias of get<ParameterGroup>().
const std::vector< std::string > & unhandledArguments() const
Unhandled arguments from command line parsing.
void writeParam(const std::string &param_filename) const
Writes this ParameterGroup into a param file specified by param_filename.
void disableOutput()
Disables the output from get, getDefault and getGroup. By default, such output is enabled.
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.
void enableOutput()
Enables the output from get, getDefault and getGroup. By default, such output is enabled.
virtual ~ParameterGroup()
ParameterGroup(const std::string &path, const ParameterGroup *parent, const bool enable_output)
void get(const char *name, T &value) const
vki param interface - deprecated
Definition: ParameterGroup.hpp:222
void displayUsage(bool used_params=false) const
Shows which parameters which are used or unused.
bool has(const std::string &name) const
This method checks if there is something with name name in the parameter gropup.
bool isOutputEnabled() const
Returs true if and only if output from get, getDefault and getGroup 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:214
void readParam(const std::string &param_filename)
Reads the contents of the param file specified by param_filename into this ParameterGroup.
T get(const std::string &name) const
This method is used to read a parameter from the parameter group.
Definition: ParameterGroup_impl.hpp:170
virtual std::string getTag() const
This function returns a string describing the ParameterMapItem.
Definition: A.hpp:4
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955
static std::string data()
Definition: exprtk.hpp:40022
Definition: ParameterGroup.hpp:85
Definition: ParameterGroup.hpp:83
Definition: ParameterGroup.hpp:88
Definition: ParameterGroup.hpp:84
Definition: ParameterMapItem.hpp:47