Eclipse SUMO - Simulation of Urban MObility
Parameterised.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2001-2022 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>
25
26#include "Parameterised.h"
27
28
29// ===========================================================================
30// method definitions
31// ===========================================================================
32
34
35
37 myMap(mapArg) {
38}
39
40
42
43
44void
45Parameterised::setParameter(const std::string& key, const std::string& value) {
46 myMap[key] = value;
47}
48
49
50void
51Parameterised::unsetParameter(const std::string& key) {
52 myMap.erase(key);
53}
54
55
56void
58 for (const auto& keyValue : mapArg) {
59 setParameter(keyValue.first, keyValue.second);
60 }
61}
62
63
64bool
65Parameterised::knowsParameter(const std::string& key) const {
66 return myMap.find(key) != myMap.end();
67}
68
69
70const std::string
71Parameterised::getParameter(const std::string& key, const std::string defaultValue) const {
72 const auto i = myMap.find(key);
73 if (i != myMap.end()) {
74 return i->second;
75 }
76 return defaultValue;
77}
78
79
80double
81Parameterised::getDouble(const std::string& key, const double defaultValue) const {
82 const auto i = myMap.find(key);
83 if (i != myMap.end()) {
84 try {
85 return StringUtils::toDouble(i->second);
86 } catch (NumberFormatException&) {
87 WRITE_WARNING("Invalid conversion from string to double (" + i->second + ")");
88 return defaultValue;
89 } catch (EmptyData&) {
90 WRITE_WARNING(TL("Invalid conversion from string to double (empty value)"));
91 return defaultValue;
92 }
93 }
94 return defaultValue;
95}
96
97
98std::vector<double>
99Parameterised::getDoubles(const std::string& key, std::vector<double> defaultValue) const {
100 const auto i = myMap.find(key);
101 if (i != myMap.end()) {
102 try {
103 std::vector<double> result;
104 for (const std::string& s : StringTokenizer(i->second).getVector()) {
105 result.push_back(StringUtils::toDouble(s));
106 }
107 return result;
108 } catch (NumberFormatException&) {
109 WRITE_WARNING("Invalid conversion from string to doubles (" + i->second + ")");
110 return defaultValue;
111 } catch (EmptyData&) {
112 WRITE_WARNING(TL("Invalid conversion from string to doubles (empty value)"));
113 return defaultValue;
114 }
115 }
116 return defaultValue;
117}
118
119void
121 myMap.clear();
122}
123
124
127 return myMap;
128}
129
130
131std::string
132Parameterised::getParametersStr(const std::string kvsep, const std::string sep) const {
133 std::string result;
134 // Generate an string using configurable seperatrs, default: "key1=value1|key2=value2|...|keyN=valueN"
135 bool addSep = false;
136 for (const auto& keyValue : myMap) {
137 if (addSep) {
138 result += sep;
139 }
140 result += keyValue.first + kvsep + keyValue.second;
141 addSep = true;
142 }
143 return result;
144}
145
146
147void
149 // first clear map
150 myMap.clear();
151 // set parameter
152 for (const auto& keyValue : params.getParametersMap()) {
153 setParameter(keyValue.first, keyValue.second);
154 }
155}
156
157
158void
160 // first clear map
161 myMap.clear();
162 // set parameter
163 for (const auto& keyValue : paramsMap) {
164 setParameter(keyValue.first, keyValue.second);
165 }
166}
167
168
169void
170Parameterised::setParametersStr(const std::string& paramsString, const std::string kvsep, const std::string sep) {
171 // clear parameters
172 myMap.clear();
173 // separate value in a vector of string using | as separator
174 std::vector<std::string> parameters = StringTokenizer(paramsString, sep).getVector();
175 // iterate over all values
176 for (const auto& keyValue : parameters) {
177 // obtain key and value and save it in myParameters
178 std::vector<std::string> keyValueStr = StringTokenizer(keyValue, kvsep).getVector();
179 setParameter(keyValueStr.front(), keyValueStr.back());
180 }
181}
182
183
184void
186 // iterate over all parameters and write it
187 for (const auto& keyValue : myMap) {
188 device.openTag(SUMO_TAG_PARAM);
189 device.writeAttr(SUMO_ATTR_KEY, StringUtils::escapeXML(keyValue.first));
190 device.writeAttr(SUMO_ATTR_VALUE, StringUtils::escapeXML(keyValue.second));
191 device.closeTag();
192 }
193}
194
195
196bool
197Parameterised::areParametersValid(const std::string& value, bool report, const std::string kvsep, const std::string sep) {
198 std::vector<std::string> parameters = StringTokenizer(value, sep).getVector();
199 // first check if parsed parameters are valid
200 for (const auto& keyValueStr : parameters) {
201 // check if parameter is valid
202 if (!isParameterValid(keyValueStr, kvsep, sep)) {
203 // report depending of flag
204 if (report) {
205 WRITE_WARNING("Invalid format of parameter (" + keyValueStr + ")");
206 }
207 return false;
208 }
209 }
210 // all ok, then return true
211 return true;
212}
213
214
215bool
216Parameterised::areAttributesValid(const std::string& value, bool report, const std::string kvsep, const std::string sep) {
217 std::vector<std::string> parameters = StringTokenizer(value, sep).getVector();
218 // first check if parsed parameters are valid
219 for (const auto& keyValueStr : parameters) {
220 // check if parameter is valid
221 if (isParameterValid(keyValueStr, kvsep, sep)) {
222 // separate key and value
223 const auto attr = StringTokenizer(value, kvsep).getVector().front();
224 // get first letter
225 const auto letter = StringTokenizer(value, kvsep).getVector().front().front();
226 // check key
227 if (!((letter >= 'a') && (letter <= 'z')) && !((letter >= 'A') && (letter <= 'Z'))) {
228 // report depending of flag
229 if (report) {
230 WRITE_WARNING("Invalid format of atribute '" + attr + "'. Attribute must start with a letter");
231 }
232 return false;
233 }
234 } else {
235 // report depending of flag
236 if (report) {
237 WRITE_WARNING("Invalid format of atribute (" + keyValueStr + ")");
238 }
239 return false;
240 }
241 }
242 // all ok, then return true
243 return true;
244}
245
246// ===========================================================================
247// private
248// ===========================================================================
249
250bool
251Parameterised::isParameterValid(const std::string& value, const std::string& kvsep, const std::string& sep) {
252 if (value.find(sep) != std::string::npos || value.find(kvsep) == std::string::npos) {
253 return false;
254 }
255 // separate key and value
256 std::vector<std::string> keyValueStr = StringTokenizer(value, kvsep).getVector();
257 // Check that keyValue size is exactly 2 (key, value)
258 if (keyValueStr.size() == 2) {
259 // check if key and value contains valid characters
260 if (SUMOXMLDefinitions::isValidParameterKey(keyValueStr.front()) == false) {
261 return false;
262 } else {
263 // key=value valid, then return true
264 return true;
265 }
266 } else {
267 // invalid format
268 return false;
269 }
270}
271
272/****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
#define TL(string)
Definition: MsgHandler.h:282
@ SUMO_TAG_PARAM
parameter associated to a certain key
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_KEY
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:251
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
An upper class for objects with additional parameters.
Definition: Parameterised.h:41
std::vector< double > getDoubles(const std::string &key, std::vector< double > defaultValue=std::vector< double >()) const
Returns the value for a given key converted to a list of doubles.
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.
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
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 inner key/value map in map<string, string> format
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.
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
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.
void setParametersMap(const Parameterised::Map &paramsMap)
set the inner key/value map in map<string, string> format
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
static bool isValidParameterKey(const std::string &value)
whether the given string is a valid key for a parameter
std::vector< std::string > getVector()
return vector of strings
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.