Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
Parameterised.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// A super class for objects with additional parameters
19/****************************************************************************/
20#include <config.h>
24
25#include "Parameterised.h"
26
27
28// ===========================================================================
29// method definitions
30// ===========================================================================
31
33
34
36 myMap(mapArg) {
37}
38
39
41
42
43void
44Parameterised::setParameter(const std::string& key, const std::string& value) {
45 myMap[key] = value;
46}
47
48
49void
50Parameterised::unsetParameter(const std::string& key) {
51 myMap.erase(key);
52}
53
54
55void
57 for (const auto& keyValue : mapArg) {
58 setParameter(keyValue.first, keyValue.second);
59 }
60}
61
62
63void
64Parameterised::mergeParameters(const Parameterised::Map& mapArg, const std::string separator, bool uniqueValues) {
65 for (const auto& keyValue : mapArg) {
66 if (hasParameter(keyValue.first)) {
67 bool append = true;
68 if (uniqueValues) {
69 if (getParameter(keyValue.first) == keyValue.second) {
70 append = false;
71 }
72 }
73 if (append) {
74 setParameter(keyValue.first, getParameter(keyValue.first) + separator + keyValue.second);
75 }
76 } else {
77 setParameter(keyValue.first, keyValue.second);
78 }
79 }
80}
81
82bool
83Parameterised::hasParameter(const std::string& key) const {
84 return myMap.find(key) != myMap.end();
85}
86
87
88const std::string
89Parameterised::getParameter(const std::string& key, const std::string defaultValue) const {
90 const auto i = myMap.find(key);
91 if (i != myMap.end()) {
92 return i->second;
93 }
94 return defaultValue;
95}
96
97
98double
99Parameterised::getDouble(const std::string& key, const double defaultValue) const {
100 const auto i = myMap.find(key);
101 if (i != myMap.end()) {
102 try {
103 return StringUtils::toDouble(i->second);
104 } catch (NumberFormatException&) {
105 WRITE_WARNINGF(TL("Invalid conversion from string to double (%)"), i->second);
106 return defaultValue;
107 } catch (EmptyData&) {
108 WRITE_WARNING(TL("Invalid conversion from string to double (empty value)"));
109 return defaultValue;
110 }
111 }
112 return defaultValue;
113}
114
115
116void
120
121
124 return myMap;
125}
126
127
128std::string
129Parameterised::getParametersStr(const std::string kvsep, const std::string sep) const {
130 std::string result;
131 // Generate an string using configurable seperatrs, default: "key1=value1|key2=value2|...|keyN=valueN"
132 bool addSep = false;
133 for (const auto& keyValue : myMap) {
134 if (addSep) {
135 result += sep;
136 }
137 result += keyValue.first + kvsep + keyValue.second;
138 addSep = true;
139 }
140 return result;
141}
142
143
144void
148
149
150void
151Parameterised::setParameters(const std::vector<std::pair<std::string, std::string> >& params) {
152 myMap.clear();
153 for (const auto& keyValue : params) {
154 myMap[keyValue.first] = keyValue.second;
155 }
156}
157
158
159void
160Parameterised::setParametersStr(const std::string& paramsString, const std::string kvsep, const std::string sep) {
161 // clear parameters
162 myMap.clear();
163 // separate value in a vector of string using | as separator
164 std::vector<std::string> parameters = StringTokenizer(paramsString, sep).getVector();
165 // iterate over all values
166 for (const auto& keyValue : parameters) {
167 // obtain key and value and save it in myParameters
168 std::vector<std::string> keyValueStr = StringTokenizer(keyValue, kvsep).getVector();
169 setParameter(keyValueStr.front(), keyValueStr.back());
170 }
171}
172
173
174bool
175Parameterised::areParametersValid(const std::string& value, bool report, const std::string kvsep, const std::string sep) {
176 std::vector<std::string> parameters = StringTokenizer(value, sep).getVector();
177 // first check if parsed parameters are valid
178 for (const auto& keyValueStr : parameters) {
179 // check if parameter is valid
180 if (!isParameterValid(keyValueStr, kvsep, sep)) {
181 // report depending of flag
182 if (report) {
183 WRITE_WARNINGF(TL("Invalid format of parameter (%)"), keyValueStr);
184 }
185 return false;
186 }
187 }
188 // all ok, then return true
189 return true;
190}
191
192
193bool
194Parameterised::areAttributesValid(const std::string& value, bool report, const std::string kvsep, const std::string sep) {
195 std::vector<std::string> parameters = StringTokenizer(value, sep).getVector();
196 // first check if parsed parameters are valid
197 for (const auto& keyValueStr : parameters) {
198 // check if parameter is valid
199 if (isParameterValid(keyValueStr, kvsep, sep)) {
200 // separate key and value
201 const auto attr = StringTokenizer(value, kvsep).getVector().front();
202 // get first letter
203 const auto letter = StringTokenizer(value, kvsep).getVector().front().front();
204 // check key
205 if (!((letter >= 'a') && (letter <= 'z')) && !((letter >= 'A') && (letter <= 'Z'))) {
206 // report depending of flag
207 if (report) {
208 WRITE_WARNINGF(TL("Invalid format of attribute '%'. Attribute must start with a letter"), attr);
209 }
210 return false;
211 }
212 } else {
213 // report depending of flag
214 if (report) {
215 WRITE_WARNINGF(TL("Invalid format of attribute (%)"), keyValueStr);
216 }
217 return false;
218 }
219 }
220 // all ok, then return true
221 return true;
222}
223
224// ===========================================================================
225// private
226// ===========================================================================
227
228bool
229Parameterised::isParameterValid(const std::string& value, const std::string& kvsep, const std::string& sep) {
230 if (value.find(sep) != std::string::npos || value.find(kvsep) == std::string::npos) {
231 return false;
232 }
233 // separate key and value
234 std::vector<std::string> keyValueStr = StringTokenizer(value, kvsep).getVector();
235 // Check that keyValue size is exactly 2 (key, value)
236 if (keyValueStr.size() == 2) {
237 // check if key and value contains valid characters
238 if (StringUtils::isValidParameterKey(keyValueStr.front()) == false) {
239 return false;
240 } else {
241 // key=value valid, then return true
242 return true;
243 }
244 } else {
245 // invalid format
246 return false;
247 }
248}
249
250/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
#define WRITE_WARNING(msg)
Definition MsgHandler.h:298
#define TL(string)
Definition MsgHandler.h:316
An upper class for objects with additional parameters.
static bool areAttributesValid(const std::string &value, bool report=false, const std::string kvsep="=", const std::string sep="|")
check if given string can be parsed to an attributes map "key1=value1|key2=value2|....
static bool isParameterValid(const std::string &value, const std::string &kvsep, const std::string &sep)
check if given string can be parsed to a parameter of type "key=value"
Parameterised::Map myMap
The key->value map.
static bool areParametersValid(const std::string &value, bool report=false, const std::string kvsep="=", const std::string sep="|")
check if given string can be parsed to a parameters map "key1=value1|key2=value2|....
void unsetParameter(const std::string &key)
Removes a parameter.
bool hasParameter(const std::string &key) const
Returns whether the parameter is set.
std::map< std::string, std::string > Map
parameters map
double getDouble(const std::string &key, const double defaultValue) const
Returns the value for a given key converted to a double.
Parameterised()
Default constructor.
void setParameters(const Parameterised &params)
set the given key/value map in map<string, string> format
void mergeParameters(const Parameterised::Map &mapArg, const std::string separator=" ", bool uniqueValues=true)
Adds or appends all given parameters from the map.
void setParametersStr(const std::string &paramsString, const std::string kvsep="=", const std::string sep="|")
set the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN"
virtual ~Parameterised()
Destructor.
void clearParameter()
Clears the parameter map.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
const Parameterised::Map & getParametersMap() const
Returns the inner key/value map.
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
std::string getParametersStr(const std::string kvsep="=", const std::string sep="|") const
Returns the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN".
void updateParameters(const Parameterised::Map &mapArg)
Adds or updates all given parameters from the map.
std::vector< std::string > getVector()
return vector of strings
static bool isValidParameterKey(const std::string &value)
whether the given string is a valid key for a parameter
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter