VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
ConfigurationFile.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef BASICS_CONFIGURATIONFILE_H
35#define BASICS_CONFIGURATIONFILE_H
36
37#include <logger/Logger.h>
38
39#include <map>
40#include <sstream>
41#include <string>
42#include <vector>
43
45
74
75namespace vf::basics
76{
77
78template <class T>
79T convert_to(const std::string& value)
80{
81 std::istringstream stream(value);
84 if (stream.fail())
85 throw UbException(UB_EXARGS, " cannot convert \"" + value + "\" to type <" +
86 static_cast<std::string>(typeid(typedValue).name()) + ">");
87
88 return typedValue;
89}
90
91template <>
92bool convert_to<bool>(const std::string& value);
93
95{
96public:
98 void clear();
99
101 void load(const std::string& File);
102
104 bool contains(const std::string& key) const;
105
107 template <class T>
108 std::vector<T> getVector(const std::string& key) const;
109
111 template <class T>
112 T getValue(const std::string& key) const;
113
115 template <class T>
116 T getValue(const std::string& key, T defaultValue) const;
117
119 std::map<std::string, std::string> data;
120
121private:
124 std::string getValue(const std::string& key) const;
125
127 static std::string trim(const std::string& str);
128
130 // template <class T>
131 // T convert_to(const std::string& value) const;
132
133 void split(std::vector<std::string>& lst, const std::string& input, const std::string& separators,
134 bool remove_empty = true) const;
135};
136
138template <class T>
139std::vector<T> ConfigurationFile::getVector(const std::string& key) const
140{
141 std::string string_value = getValue(key);
142 std::vector<T> values;
143 std::vector<std::string> string_vector;
144 split(string_vector, string_value, "\t\n\r;, ");
145 for (std::vector<std::string>::iterator it = string_vector.begin(); it != string_vector.end(); ++it) {
146 if (!(*it).empty()) {
147 values.push_back(convert_to<T>(*it));
148 }
149 }
150 return values;
151}
152
154template <class T>
155T ConfigurationFile::getValue(const std::string& key) const
156{
157 std::string value = getValue(key);
158
159 return convert_to<T>(value);
160}
161
162template <class T>
163T ConfigurationFile::getValue(const std::string& key, T defaultValue) const
164{
165 if (contains(key)) {
166 return getValue<T>(key);
167 }
168 return defaultValue;
169}
170
171ConfigurationFile loadConfig(int argc, char* argv[], std::string configPath = "./config.txt");
172
173} // namespace vf::basics
174
175#endif
176
std::vector< T > getVector(const std::string &key) const
get vector with key
std::map< std::string, std::string > data
the container is public to test this class
bool contains(const std::string &key) const
check if value associated with given key exists
void load(const std::string &File)
load a configuration file
T getValue(const std::string &key) const
get value with key
std::shared_ptr< T > SPtr
#define UB_EXARGS
Definition UbException.h:73
Simple configuration file.
ConfigurationFile loadConfig(int argc, char *argv[], std::string configPath)
bool convert_to< bool >(const std::string &value)
T convert_to(const std::string &value)