Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Koch
dune-common
Commits
48efdc4f
Commit
48efdc4f
authored
14 years ago
by
Jorrit Fahlke
Browse files
Options
Downloads
Patches
Plain Diff
ConfigParser: Allow get of a FieldVector.
[[Imported from SVN: r6001]]
parent
edb8fd69
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dune/common/configparser.hh
+63
-1
63 additions, 1 deletion
dune/common/configparser.hh
with
63 additions
and
1 deletion
dune/common/configparser.hh
+
63
−
1
View file @
48efdc4f
...
...
@@ -7,10 +7,13 @@
#include
<istream>
#include
<map>
#include
<ostream>
#include
<typeinfo>
#include
<vector>
#include
<string>
#include
<iostream>
#include
<dune/common/fvector.hh>
namespace
Dune
{
/** \brief Parser for hierarchical configuration files
...
...
@@ -61,6 +64,11 @@ namespace Dune {
*/
class
ConfigParser
{
// class providing a single static parse() function, used by the
// generic get() method
template
<
typename
T
>
struct
Parser
;
public:
typedef
std
::
vector
<
std
::
string
>
KeyVector
;
...
...
@@ -228,6 +236,24 @@ namespace Dune {
*/
bool
get
(
const
std
::
string
&
key
,
bool
defaultValue
);
/** \brief get value converted to a certain type
*
* Returns value as type T for given key.
*
* \tparam T type of returned value.
* \param key key name
* \param defaultValue default if key does not exist
* \return value converted to T
*/
template
<
typename
T
>
T
get
(
const
std
::
string
&
key
,
const
T
&
defaultValue
)
{
if
(
hasKey
(
key
))
return
get
<
T
>
(
key
);
else
return
defaultValue
;
}
/** \brief Get value
*
* \tparam T Type of the value
...
...
@@ -237,7 +263,19 @@ namespace Dune {
* \return value as T
*/
template
<
class
T
>
T
get
(
const
std
::
string
&
key
);
T
get
(
const
std
::
string
&
key
)
{
if
(
not
hasKey
(
key
))
DUNE_THROW
(
RangeError
,
"Key '"
<<
key
<<
"' not found in "
"parameter file!"
);
try
{
return
Parser
<
T
>::
parse
((
*
this
)[
key
]);
}
catch
(
const
RangeError
&
)
{
DUNE_THROW
(
RangeError
,
"Cannot parse value
\"
"
<<
(
*
this
)[
key
]
<<
"
\"
for key
\"
"
<<
key
<<
"
\"
"
"as a "
<<
typeid
(
T
).
name
());
}
}
/** \brief get value keys
*
...
...
@@ -265,6 +303,30 @@ namespace Dune {
static
std
::
string
ltrim
(
const
std
::
string
&
s
);
static
std
::
string
rtrim
(
const
std
::
string
&
s
);
};
template
<
typename
T
,
int
n
>
struct
ConfigParser
::
Parser
<
FieldVector
<
T
,
n
>
>
{
static
FieldVector
<
T
,
n
>
parse
(
const
std
::
string
&
str
)
{
FieldVector
<
T
,
n
>
val
;
std
::
istringstream
s
(
str
);
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
s
>>
val
[
i
];
if
(
!
s
)
DUNE_THROW
(
RangeError
,
"Cannot parse value
\"
"
<<
str
<<
"
\"
as a FieldVector<"
<<
typeid
(
T
).
name
()
<<
", "
<<
n
<<
">"
);
}
T
dummy
;
s
>>
dummy
;
// now extraction should have failed, and eof should be set
if
(
not
s
.
fail
()
or
not
s
.
eof
())
DUNE_THROW
(
RangeError
,
"Cannot parse value
\"
"
<<
str
<<
"
\"
"
"as a FieldVector<double, "
<<
n
<<
">"
);
return
val
;
}
};
}
// end namespace dune
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment