Eclipse SUMO - Simulation of Urban MObility
Option.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/****************************************************************************/
20// A class representing a single program option
21/****************************************************************************/
22#include <config.h>
23
24#include <string>
25#include <exception>
26#include <sstream>
27#include "Option.h"
34
35
36// ===========================================================================
37// method definitions
38// ===========================================================================
39/* -------------------------------------------------------------------------
40 * Option - methods
41 * ----------------------------------------------------------------------- */
43 : myAmSet(set), myHaveTheDefaultValue(true), myAmWritable(true) {}
44
45
47
48
49bool
51 return myAmSet;
52}
53
54
55double
57 throw InvalidArgument("This is not a double-option");
58}
59
60
61int
63 throw InvalidArgument("This is not an int-option");
64}
65
66
67std::string
69 throw InvalidArgument("This is not a string-option");
70}
71
72
73bool
75 throw InvalidArgument("This is not a bool-option");
76}
77
78
79const IntVector&
81 throw InvalidArgument("This is not an int vector-option");
82}
83
84
85const StringVector&
87 throw InvalidArgument("This is not a string vector-option");
88}
89
90
91bool
92Option::markSet(const std::string& orig) {
93 bool ret = myAmWritable;
95 myAmSet = true;
96 myAmWritable = false;
97 myValueString = orig;
98 return ret;
99}
100
101
102bool
104 return false;
105}
106
107
108bool
111}
112
113
114bool
116 return false;
117}
118
119
120bool
122 return myAmWritable;
123}
124
125
126void
128 myAmWritable = true;
129}
130
131
132void
135}
136
137
138const std::string&
140 return myDescription;
141}
142
143
144void
145Option::setDescription(const std::string& desc) {
146 myDescription = desc;
147}
148
149
150const std::string&
152 return myTypeName;
153}
154
155
156/* -------------------------------------------------------------------------
157 * Option_Integer - methods
158 * ----------------------------------------------------------------------- */
160 : Option(true), myValue(value) {
161 myTypeName = "INT";
162 myValueString = toString(value);
163}
164
165
166int
168 return myValue;
169}
170
171
172bool
173Option_Integer::set(const std::string& v, const std::string& orig, const bool /* append */) {
174 try {
176 return markSet(orig);
177 } catch (...) {
178 std::string s = "'" + v + "' is not a valid integer.";
179 throw ProcessError(s);
180 }
181}
182
183
184/* -------------------------------------------------------------------------
185 * Option_String - methods
186 * ----------------------------------------------------------------------- */
188 : Option() {
189 myTypeName = "STR";
190}
191
192
193Option_String::Option_String(const std::string& value, std::string typeName)
194 : Option(true), myValue(value) {
195 myTypeName = typeName;
196 myValueString = value;
197}
198
199
200std::string
202 return myValue;
203}
204
205
206bool
207Option_String::set(const std::string& v, const std::string& orig, const bool /* append */) {
208 myValue = v;
209 return markSet(orig);
210}
211
212
213/* -------------------------------------------------------------------------
214 * Option_Float - methods
215 * ----------------------------------------------------------------------- */
217 : Option(true), myValue(value) {
218 myTypeName = "FLOAT";
219 std::ostringstream oss;
220 oss << value;
221 myValueString = oss.str();
222}
223
224
225double
227 return myValue;
228}
229
230
231bool
232Option_Float::set(const std::string& v, const std::string& orig, const bool /* append */) {
233 try {
235 return markSet(orig);
236 } catch (...) {
237 throw ProcessError("'" + v + "' is not a valid float.");
238 }
239}
240
241
242/* -------------------------------------------------------------------------
243 * Option_Bool - methods
244 * ----------------------------------------------------------------------- */
246 : Option(true), myValue(value) {
247 myTypeName = "BOOL";
248 myValueString = value ? "true" : "false";
249}
250
251
252bool
254 return myValue;
255}
256
257
258bool
259Option_Bool::set(const std::string& v, const std::string& orig, const bool /* append */) {
260 try {
262 return markSet(orig);
263 } catch (...) {
264 throw ProcessError("'" + v + "' is not a valid bool.");
265 }
266}
267
268
269bool
271 return true;
272}
273
274
275/* -------------------------------------------------------------------------
276 * Option_BoolExtended - methods
277 * ----------------------------------------------------------------------- */
279 : Option_Bool(value) {
280}
281
282
283bool
284Option_BoolExtended::set(const std::string& v, const std::string& orig, const bool /* append */) {
285 try {
287 return markSet("");
288 } catch (...) {
289 myValue = true;
290 }
291 return markSet(orig);
292}
293
294
295/* -------------------------------------------------------------------------
296 * Option_IntVector - methods
297 * ----------------------------------------------------------------------- */
299 : Option() {
300 myTypeName = "INT[]";
301}
302
303
305 : Option(true), myValue(value) {
306 myTypeName = "INT[]";
307 myValueString = joinToString(value, ",");
308}
309
310
311const IntVector&
313 return myValue;
314}
315
316
317bool
318Option_IntVector::set(const std::string& v, const std::string& orig, const bool append) {
319 if (!append) {
320 myValue.clear();
321 }
322 try {
323 if (v.find(';') != std::string::npos) {
324 WRITE_WARNING(TL("Please note that using ';' as list separator is deprecated and not accepted anymore."));
325 }
326 StringTokenizer st(v, ",", true);
327 while (st.hasNext()) {
328 myValue.push_back(StringUtils::toInt(st.next()));
329 }
330 return markSet(orig);
331 } catch (EmptyData&) {
332 throw ProcessError("Empty element occurred in " + v);
333 } catch (...) {
334 throw ProcessError("'" + v + "' is not a valid integer vector.");
335 }
336}
337
338
339/* -------------------------------------------------------------------------
340 * Option_StringVector - methods
341 * ----------------------------------------------------------------------- */
343 myTypeName = "STR[]";
344}
345
346
348 : Option(true), myValue(value) {
349 myTypeName = "STR[]";
350 myValueString = joinToString(value, ",");
351}
352
353
354const StringVector&
356 return myValue;
357}
358
359
360bool
361Option_StringVector::set(const std::string& v, const std::string& orig, const bool append) {
362 if (!append) {
363 myValue.clear();
364 }
365 StringTokenizer st(v, ",");
366 while (st.hasNext()) {
367 myValue.push_back(StringUtils::prune(st.next()));
368 }
369 return markSet(append && getValueString() != "" ? getValueString() + "," + orig : orig);
370}
371
372
373/* -------------------------------------------------------------------------
374 * Option_FileName - methods
375 * ----------------------------------------------------------------------- */
377 myTypeName = "FILE";
378}
379
380
382 : Option_StringVector(value) {
383 myTypeName = "FILE";
384}
385
386
388 return true;
389}
390
391
392std::string
394 return joinToString(getStringVector(), ",");
395}
396
397
398/****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
#define TL(string)
Definition: MsgHandler.h:282
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition: Option.h:43
std::vector< int > IntVector
Definition of a vector of ints.
Definition: Option.h:38
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:282
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
Option_BoolExtended(bool value)
Constructor for an option that can be used without an argument like Option_BoolExtended but which als...
Definition: Option.cpp:278
bool set(const std::string &v, const std::string &orig, const bool append)
Definition: Option.cpp:284
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:253
bool set(const std::string &v, const std::string &orig, const bool append)
Definition: Option.cpp:259
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition: Option.cpp:270
Option_Bool(bool value)
Constructor for an option with a default value.
Definition: Option.cpp:245
bool myValue
Definition: Option.h:448
std::string getString() const
Legacy method that returns the stored filenames as a comma-separated string.
Definition: Option.cpp:393
Option_FileName()
Constructor for an option with no default value.
Definition: Option.cpp:376
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition: Option.cpp:387
double getFloat() const
Returns the stored double value.
Definition: Option.cpp:226
bool set(const std::string &v, const std::string &orig, const bool append)
Stores the given value after parsing it into a double.
Definition: Option.cpp:232
double myValue
Definition: Option.h:411
Option_Float(double value)
Constructor for an option with a default value.
Definition: Option.cpp:216
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:312
IntVector myValue
Definition: Option.h:512
bool set(const std::string &v, const std::string &orig, const bool append)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:318
Option_IntVector()
Constructor for an option with no default value.
Definition: Option.cpp:298
Option_Integer(int value)
Constructor for an option with a default value.
Definition: Option.cpp:159
int getInt() const
Returns the stored integer value.
Definition: Option.cpp:167
bool set(const std::string &v, const std::string &orig, const bool append)
Stores the given value after parsing it into an integer.
Definition: Option.cpp:173
bool set(const std::string &v, const std::string &orig, const bool append)
Stores the given value.
Definition: Option.cpp:207
std::string myValue
Definition: Option.h:368
std::string getString() const
Returns the stored string value.
Definition: Option.cpp:201
Option_String()
Constructor for an option with no default value.
Definition: Option.cpp:187
const StringVector & getStringVector() const
Returns the stored string vector.
Definition: Option.cpp:355
StringVector myValue
Definition: Option.h:557
Option_StringVector()
Constructor for an option with no default value.
Definition: Option.cpp:342
bool set(const std::string &v, const std::string &orig, const bool append)
Stores the given value after parsing it into a vector of strings.
Definition: Option.cpp:361
A class representing a single program option.
Definition: Option.h:73
bool myHaveTheDefaultValue
information whether the value is the default value (is then set)
Definition: Option.h:271
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:121
bool isSet() const
returns the information whether this options holds a valid value
Definition: Option.cpp:50
virtual ~Option()
Definition: Option.cpp:46
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:109
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:68
bool myAmSet
information whether the value is set
Definition: Option.h:268
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:80
void resetWritable()
Resets the option to be writeable.
Definition: Option.cpp:127
const std::string & getDescription() const
Returns the description of what this option does.
Definition: Option.cpp:139
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition: Option.h:261
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition: Option.cpp:115
std::string myDescription
The description what this option does.
Definition: Option.h:277
virtual const StringVector & getStringVector() const
Returns the stored string vector.
Definition: Option.cpp:86
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:145
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:151
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:62
virtual double getFloat() const
Returns the stored double value.
Definition: Option.cpp:56
virtual bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:74
Option(bool set=false)
Constructor.
Definition: Option.cpp:42
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:133
bool markSet(const std::string &orig)
Marks the information as set.
Definition: Option.cpp:92
bool myAmWritable
information whether the value may be changed
Definition: Option.h:274
std::string myValueString
The original set string.
Definition: Option.h:264
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition: Option.cpp:103
const std::string & getValueString() const
Returns the string-representation of the value.
Definition: Option.h:167
bool hasNext()
returns the information whether further substrings exist
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
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 prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:55
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter