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-2024 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
64void
65Parameterised::mergeParameters(const Parameterised::Map& mapArg, const std::string separator, bool uniqueValues) {
66 for (const auto& keyValue : mapArg) {
67 if (hasParameter(keyValue.first)) {
68 bool append = true;
69 if (uniqueValues) {
70 if (getParameter(keyValue.first) == keyValue.second) {
71 append = false;
72 }
73 }
74 if (append) {
75 setParameter(keyValue.first, getParameter(keyValue.first) + separator + keyValue.second);
76 }
77 } else {
78 setParameter(keyValue.first, keyValue.second);
79 }
80 }
81}
82
83bool
84Parameterised::hasParameter(const std::string& key) const {
85 return myMap.find(key) != myMap.end();
86}
87
88
89const std::string
90Parameterised::getParameter(const std::string& key, const std::string defaultValue) const {
91 const auto i = myMap.find(key);
92 if (i != myMap.end()) {
93 return i->second;
94 }
95 return defaultValue;
96}
97
98
99double
100Parameterised::getDouble(const std::string& key, const double defaultValue) const {
101 const auto i = myMap.find(key);
102 if (i != myMap.end()) {
103 try {
104 return StringUtils::toDouble(i->second);
105 } catch (NumberFormatException&) {
106 WRITE_WARNINGF(TL("Invalid conversion from string to double (%)"), i->second);
107 return defaultValue;
108 } catch (EmptyData&) {
109 WRITE_WARNING(TL("Invalid conversion from string to double (empty value)"));
110 return defaultValue;
111 }
112 }
113 return defaultValue;
114}
115
116
117void
121
122
125 return myMap;
126}
127
128
129std::string
130Parameterised::getParametersStr(const std::string kvsep, const std::string sep) const {
131 std::string result;
132 // Generate an string using configurable seperatrs, default: "key1=value1|key2=value2|...|keyN=valueN"
133 bool addSep = false;
134 for (const auto& keyValue : myMap) {
135 if (addSep) {
136 result += sep;
137 }
138 result += keyValue.first + kvsep + keyValue.second;
139 addSep = true;
140 }
141 return result;
142}
143
144
145void
149
150
151void
152Parameterised::setParametersStr(const std::string& paramsString, const std::string kvsep, const std::string sep) {
153 // clear parameters
154 myMap.clear();
155 // separate value in a vector of string using | as separator
156 std::vector<std::string> parameters = StringTokenizer(paramsString, sep).getVector();
157 // iterate over all values
158 for (const auto& keyValue : parameters) {
159 // obtain key and value and save it in myParameters
160 std::vector<std::string> keyValueStr = StringTokenizer(keyValue, kvsep).getVector();
161 setParameter(keyValueStr.front(), keyValueStr.back());
162 }
163}
164
165
166void
168 // iterate over all parameters and write it
169 for (const auto& keyValue : myMap) {
170 device.openTag(SUMO_TAG_PARAM);
171 device.writeAttr(SUMO_ATTR_KEY, StringUtils::escapeXML(keyValue.first));
172 device.writeAttr(SUMO_ATTR_VALUE, StringUtils::escapeXML(keyValue.second));
173 device.closeTag();
174 }
175}
176
177
178bool
179Parameterised::areParametersValid(const std::string& value, bool report, const std::string kvsep, const std::string sep) {
180 std::vector<std::string> parameters = StringTokenizer(value, sep).getVector();
181 // first check if parsed parameters are valid
182 for (const auto& keyValueStr : parameters) {
183 // check if parameter is valid
184 if (!isParameterValid(keyValueStr, kvsep, sep)) {
185 // report depending of flag
186 if (report) {
187 WRITE_WARNINGF(TL("Invalid format of parameter (%)"), keyValueStr);
188 }
189 return false;
190 }
191 }
192 // all ok, then return true
193 return true;
194}
195
196
197bool
198Parameterised::areAttributesValid(const std::string& value, bool report, const std::string kvsep, const std::string sep) {
199 std::vector<std::string> parameters = StringTokenizer(value, sep).getVector();
200 // first check if parsed parameters are valid
201 for (const auto& keyValueStr : parameters) {
202 // check if parameter is valid
203 if (isParameterValid(keyValueStr, kvsep, sep)) {
204 // separate key and value
205 const auto attr = StringTokenizer(value, kvsep).getVector().front();
206 // get first letter
207 const auto letter = StringTokenizer(value, kvsep).getVector().front().front();
208 // check key
209 if (!((letter >= 'a') && (letter <= 'z')) && !((letter >= 'A') && (letter <= 'Z'))) {
210 // report depending of flag
211 if (report) {
212 WRITE_WARNINGF(TL("Invalid format of attribute '%'. Attribute must start with a letter"), attr);
213 }
214 return false;
215 }
216 } else {
217 // report depending of flag
218 if (report) {
219 WRITE_WARNINGF(TL("Invalid format of attribute (%)"), keyValueStr);
220 }
221 return false;
222 }
223 }
224 // all ok, then return true
225 return true;
226}
227
228// ===========================================================================
229// private
230// ===========================================================================
231
232bool
233Parameterised::isParameterValid(const std::string& value, const std::string& kvsep, const std::string& sep) {
234 if (value.find(sep) != std::string::npos || value.find(kvsep) == std::string::npos) {
235 return false;
236 }
237 // separate key and value
238 std::vector<std::string> keyValueStr = StringTokenizer(value, kvsep).getVector();
239 // Check that keyValue size is exactly 2 (key, value)
240 if (keyValueStr.size() == 2) {
241 // check if key and value contains valid characters
242 if (SUMOXMLDefinitions::isValidParameterKey(keyValueStr.front()) == false) {
243 return false;
244 } else {
245 // key=value valid, then return true
246 return true;
247 }
248 } else {
249 // invalid format
250 return false;
251 }
252}
253
254/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:296
#define WRITE_WARNING(msg)
Definition MsgHandler.h:295
#define TL(string)
Definition MsgHandler.h:315
@ 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.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
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.
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 inner 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.
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.
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.