Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
Option.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-2025 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// -------------------------------------------------------------------------
41// Option - methods
42// -------------------------------------------------------------------------
43
44Option::Option(bool set) :
45 myAmSet(set) {
46}
47
48
50
51
52bool
54 return myAmSet;
55}
56
57
58double
60 throw InvalidArgument("This is not a double-option");
61}
62
63
64int
66 throw InvalidArgument("This is not an int-option");
67}
68
69
70std::string
72 throw InvalidArgument("This is not a string-option");
73}
74
75
76bool
78 throw InvalidArgument("This is not a bool-option");
79}
80
81
82const IntVector&
84 throw InvalidArgument("This is not an int vector-option");
85}
86
87
88const StringVector&
90 throw InvalidArgument("This is not a string vector-option");
91}
92
93
94bool
95Option::markSet(const std::string& orig) {
96 bool ret = myAmWritable;
98 myAmSet = true;
99 myAmWritable = false;
100 myValueString = orig;
101 return ret;
102}
103
104
105const std::string&
107 return myValueString;
108}
109
110
111bool
114}
115
116
117bool
119 return false;
120}
121
122
123bool
125 return false;
126}
127
128
129bool
131 return false;
132}
133
134
135bool
137 return false;
138}
139
140
141bool
143 return false;
144}
145
146
147bool
149 return false;
150}
151
152
153bool
155 return false;
156}
157
158
159bool
161 return false;
162}
163
164
165bool
167 return false;
168}
169
170
171bool
173 return false;
174}
175
176
177bool
179 return false;
180}
181
182
183bool
185 return myAmWritable;
186}
187
188
189void
193
194
195void
199
200
201const std::string&
203 return myDescription;
204}
205
206
207void
208Option::setDescription(const std::string& desc) {
209 myDescription = desc;
210}
211
212
213bool
215 return myRequired;
216}
217
218
219void
221 myRequired = true;
222}
223
224bool
226 return myPositional;
227}
228
229void
233
234
235bool
237 return myEditable;
238}
239
240
241void Option::setEditable(const bool value) {
242 myEditable = value;
243}
244
245
246const std::string&
248 return myListSeparator;
249}
250
251void
252Option::setListSeparator(const std::string& listSep) {
253 myListSeparator = listSep;
254}
255
256const std::string&
258 return mySubTopic;
259}
260
261
262void
263Option::setSubtopic(const std::string& subtopic) {
264 mySubTopic = subtopic;
265}
266
267
268const std::string&
270 return myTypeName;
271}
272
273// -------------------------------------------------------------------------
274// Option_Integer - methods
275// -------------------------------------------------------------------------
276
278 Option(true),
279 myValue(value) {
280 myTypeName = "INT";
281 myValueString = toString(value);
282}
283
284
285int
287 return myValue;
288}
289
290
291bool
292Option_Integer::set(const std::string& v, const std::string& orig, const bool /* append */) {
293 try {
295 return markSet(orig);
296 } catch (...) {
297 std::string s = "'" + v + "' is not a valid integer.";
298 throw ProcessError(s);
299 }
300}
301
302
303bool
305 return true;
306}
307
308// -------------------------------------------------------------------------
309// Option_String - methods
310// -------------------------------------------------------------------------
311
313 Option() {
314 myTypeName = "STR";
315}
316
317
318Option_String::Option_String(const std::string& value, std::string typeName) :
319 Option(true),
320 myValue(value) {
321 myTypeName = typeName;
322 myValueString = value;
323}
324
325
326std::string
328 return myValue;
329}
330
331
332bool
333Option_String::set(const std::string& v, const std::string& orig, const bool /* append */) {
334 myValue = v;
335 return markSet(orig);
336}
337
338// -------------------------------------------------------------------------
339// Option_Float - methods
340// -------------------------------------------------------------------------
341
343 Option(true),
344 myValue(value) {
345 myTypeName = "FLOAT";
346 std::ostringstream oss;
347 oss << value;
348 myValueString = oss.str();
349}
350
351
352double
354 return myValue;
355}
356
357
358bool
359Option_Float::set(const std::string& v, const std::string& orig, const bool /* append */) {
360 try {
362 return markSet(orig);
363 } catch (...) {
364 throw ProcessError(TLF("'%' is not a valid float.", v));
365 }
366}
367
368
369bool
371 return true;
372}
373
374// -------------------------------------------------------------------------
375// Option_Bool - methods
376// -------------------------------------------------------------------------
377
379 Option(true),
380 myValue(value) {
381 myTypeName = "BOOL";
382 myValueString = value ? "true" : "false";
383}
384
385
386bool
388 return myValue;
389}
390
391
392bool
393Option_Bool::set(const std::string& v, const std::string& orig, const bool /* append */) {
394 try {
396 return markSet(orig);
397 } catch (...) {
398 throw ProcessError(TLF("'%' is not a valid bool.", v));
399 }
400}
401
402
403bool
405 return true;
406}
407
408// -------------------------------------------------------------------------
409// Option_BoolExtended - methods
410// -------------------------------------------------------------------------
411
415
416
417bool
418Option_BoolExtended::set(const std::string& v, const std::string& orig, const bool /* append */) {
419 try {
421 return markSet("");
422 } catch (...) {
423 myValue = true;
424 }
425 return markSet(orig);
426}
427
428// -------------------------------------------------------------------------
429// Option_IntVector - methods
430// -------------------------------------------------------------------------
431
436
437
439 : Option(true), myValue(value) {
440 myTypeName = "INT[]";
441 myValueString = joinToString(value, ",");
442}
443
444
445const IntVector&
447 return myValue;
448}
449
450
451bool
452Option_IntVector::set(const std::string& v, const std::string& orig, const bool append) {
453 if (!append) {
454 myValue.clear();
455 }
456 try {
457 if (v.find(';') != std::string::npos) {
458 WRITE_WARNING(TL("Please note that using ';' as list separator is deprecated and not accepted anymore."));
459 }
460 StringTokenizer st(v, ",", true);
461 while (st.hasNext()) {
462 myValue.push_back(StringUtils::toInt(st.next()));
463 }
464 return markSet(orig);
465 } catch (EmptyData&) {
466 throw ProcessError("Empty element occurred in " + v);
467 } catch (...) {
468 throw ProcessError(TLF("'%' is not a valid integer vector.", v));
469 }
470}
471
472// -------------------------------------------------------------------------
473// Option_StringVector - methods
474// -------------------------------------------------------------------------
475
480
481
483 Option(true), myValue(value) {
484 myTypeName = "STR[]";
485 myValueString = joinToString(value, ",");
486}
487
488
489const StringVector&
493
494
495bool
496Option_StringVector::set(const std::string& v, const std::string& orig, const bool append) {
497 if (!append) {
498 myValue.clear();
499 }
500 StringTokenizer st(v, ",");
501 while (st.hasNext()) {
502 myValue.push_back(StringUtils::prune(st.next()));
503 }
504 return markSet(append && getValueString() != "" ? getValueString() + "," + orig : orig);
505}
506
507// -------------------------------------------------------------------------
508// Option_FileName - methods
509// -------------------------------------------------------------------------
510
515
516
518 Option_StringVector(value) {
519 myTypeName = "FILE";
520}
521
522
523bool
525 return true;
526}
527
528
529std::string
531 return joinToString(getStringVector(), ",");
532}
533
534// -------------------------------------------------------------------------
535// Option_Network - methods
536// -------------------------------------------------------------------------
537
538Option_Network::Option_Network(const std::string& value) :
539 Option_String(value, "NETWORK") {
540}
541
542
544 return true;
545}
546
547// -------------------------------------------------------------------------
548// Option_Additional - methods
549// -------------------------------------------------------------------------
550
551Option_Additional::Option_Additional(const std::string& value) :
552 Option_String(value, "ADDITIONAL") {
553}
554
555
556bool
558 return true;
559}
560
561// -------------------------------------------------------------------------
562// Option_Route - methods
563// -------------------------------------------------------------------------
564
565Option_Route::Option_Route(const std::string& value) :
566 Option_String(value, "ROUTE") {
567}
568
569
570bool
572 return true;
573}
574
575// -------------------------------------------------------------------------
576// Option_Data - methods
577// -------------------------------------------------------------------------
578
579Option_Data::Option_Data(const std::string& value) :
580 Option_String(value, "DATA") {
581}
582
583
584bool
586 return true;
587}
588
589// -------------------------------------------------------------------------
590// Option_Data - methods
591// -------------------------------------------------------------------------
592
593Option_SumoConfig::Option_SumoConfig(const std::string& value) :
594 Option_String(value, "SUMOCONFIG") {
595}
596
597
598bool
600 return true;
601}
602
603// -------------------------------------------------------------------------
604// Option_Data - methods
605// -------------------------------------------------------------------------
606
607Option_Edge::Option_Edge(const std::string& value) :
608 Option_String(value, "EDGE") {
609}
610
611
612bool
614 return true;
615}
616
617// -------------------------------------------------------------------------
618// Option_Data - methods
619// -------------------------------------------------------------------------
620
621Option_EdgeVector::Option_EdgeVector(const std::string& value) :
622 Option_String(value, "EDGE[]") {
623}
624
625
626bool
628 return true;
629}
630
631/****************************************************************************/
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition Option.h:42
std::vector< int > IntVector
Definition of a vector of ints.
Definition Option.h:37
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:289
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
Option_Additional(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:551
bool isAdditional() const
Returns true, the information whether this option is a file name.
Definition Option.cpp:557
Option_BoolExtended(bool value)
Constructor for an option that can be used without an argument like Option_BoolExtended but which als...
Definition Option.cpp:412
bool set(const std::string &v, const std::string &orig, const bool append)
sets the given value (converts it to bool)
Definition Option.cpp:418
bool getBool() const
Returns the stored boolean value.
Definition Option.cpp:387
bool set(const std::string &v, const std::string &orig, const bool append)
sets the given value (converts it to bool)
Definition Option.cpp:393
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition Option.cpp:404
Option_Bool(bool value)
Constructor for an option with a default value.
Definition Option.cpp:378
bool myValue
the value, valid only when the base-classes "myAmSet"-member is true
Definition Option.h:591
Option_Data(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:579
bool isData() const
Returns true, the information whether this option is a data file.
Definition Option.cpp:585
bool isEdge() const
Returns true, the information whether this option is a list of edges.
Definition Option.cpp:613
Option_Edge(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:607
bool isEdgeVector() const
Returns true, the information whether this option is a list of edges.
Definition Option.cpp:627
Option_EdgeVector(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:621
std::string getString() const
Legacy method that returns the stored filenames as a comma-separated string.
Definition Option.cpp:530
Option_FileName()
Constructor for an option with no default value.
Definition Option.cpp:511
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition Option.cpp:524
bool isFloat() const
Returns the information whether the option is a float option.
Definition Option.cpp:370
double getFloat() const
Returns the stored double value.
Definition Option.cpp:353
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:359
double myValue
the value, valid only when the base-classes "myAmSet"-member is true
Definition Option.h:551
Option_Float(double value)
Constructor for an option with a default value.
Definition Option.cpp:342
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition Option.cpp:446
IntVector myValue
the value, valid only when the base-classes "myAmSet"-member is true
Definition Option.h:659
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:452
Option_IntVector()
Constructor for an option with no default value.
Definition Option.cpp:432
bool isInteger() const
Returns the information whether the option is a int option.
Definition Option.cpp:304
Option_Integer(int value)
Constructor for an option with a default value.
Definition Option.cpp:277
int myValue
the value, valid only when the base-classes "myAmSet"-member is true
Definition Option.h:451
int getInt() const
Returns the stored integer value.
Definition Option.cpp:286
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:292
Option_Network(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:538
bool isNetwork() const
Returns true, the information whether this option is a file name.
Definition Option.cpp:543
bool isRoute() const
Returns true, the information whether this option is a file name.
Definition Option.cpp:571
Option_Route(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:565
bool set(const std::string &v, const std::string &orig, const bool append)
Stores the given value.
Definition Option.cpp:333
std::string myValue
the value, valid only when the base-classes "myAmSet"-member is true
Definition Option.h:498
std::string getString() const
Returns the stored string value.
Definition Option.cpp:327
Option_String()
Constructor for an option with no default value.
Definition Option.cpp:312
const StringVector & getStringVector() const
Returns the stored string vector.
Definition Option.cpp:490
StringVector myValue
the value, valid only when the base-classes "myAmSet"-member is true
Definition Option.h:706
Option_StringVector()
Constructor for an option with no default value.
Definition Option.cpp:476
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:496
Option_SumoConfig(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:593
bool isSumoConfig() const
Returns true, the information whether this option is a sumo config name.
Definition Option.cpp:599
A class representing a single program option.
Definition Option.h:74
bool myHaveTheDefaultValue
information whether the value is the default value (is then set)
Definition Option.h:377
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition Option.cpp:184
virtual bool isSumoConfig() const
Returns the information whether this option is a sumo config file.
Definition Option.cpp:166
virtual bool isNetwork() const
Returns the information whether this option is a network file.
Definition Option.cpp:142
bool isSet() const
returns the information whether this options holds a valid value
Definition Option.cpp:53
virtual ~Option()
destructor
Definition Option.cpp:49
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition Option.cpp:112
void setRequired()
mark option as required
Definition Option.cpp:220
virtual std::string getString() const
Returns the stored string value.
Definition Option.cpp:71
bool myAmSet
information whether the value is set
Definition Option.h:374
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition Option.cpp:83
void resetWritable()
Resets the option to be writeable.
Definition Option.cpp:190
const std::string & getDescription() const
Returns the description of what this option does.
Definition Option.cpp:202
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition Option.h:367
virtual bool isInteger() const
Returns the information whether the option is a int option.
Definition Option.cpp:118
std::string myListSeparator
the list separator for this option (needed for python tools)
Definition Option.h:395
std::string mySubTopic
The subtopic to which this option belongs.
Definition Option.h:398
void setListSeparator(const std::string &listSep)
set list separator
Definition Option.cpp:252
virtual bool isAdditional() const
Returns the information whether this option is an additional file.
Definition Option.cpp:148
virtual bool isData() const
Returns the information whether this option is a data file.
Definition Option.cpp:160
bool myEditable
this option can be edited using option dialog
Definition Option.h:392
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition Option.cpp:136
const std::string & getListSeparator() const
retrieve list separator
Definition Option.cpp:247
std::string myDescription
The description what this option does.
Definition Option.h:383
virtual const StringVector & getStringVector() const
Returns the stored string vector.
Definition Option.cpp:89
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition Option.cpp:208
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition Option.cpp:269
virtual int getInt() const
Returns the stored integer value.
Definition Option.cpp:65
virtual double getFloat() const
Returns the stored double value.
Definition Option.cpp:59
virtual bool isEdgeVector() const
Returns the information whether this option is a vector of edges.
Definition Option.cpp:178
virtual bool getBool() const
Returns the stored boolean value.
Definition Option.cpp:77
virtual bool isRoute() const
Returns the information whether this option is a route file.
Definition Option.cpp:154
bool isPositional() const
check if option is positional
Definition Option.cpp:225
void setPositional()
mark option as positional
Definition Option.cpp:230
bool myPositional
this option is positional (needed for python tools)
Definition Option.h:389
Option(bool set=false)
Constructor.
Definition Option.cpp:44
void resetDefault()
Resets the option to be on its default value.
Definition Option.cpp:196
const std::string & getSubTopic() const
Returns the subtopic to which this option belongs.
Definition Option.cpp:257
virtual bool isFloat() const
Returns the information whether the option is a float option.
Definition Option.cpp:124
bool markSet(const std::string &orig)
Marks the information as set.
Definition Option.cpp:95
bool myRequired
this option is required (needed for python tools)
Definition Option.h:386
bool myAmWritable
information whether the value may be changed
Definition Option.h:380
std::string myValueString
The original set string.
Definition Option.h:370
bool isEditable() const
check if this option is editable
Definition Option.cpp:236
virtual bool isEdge() const
Returns the information whether this option is an edge.
Definition Option.cpp:172
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition Option.cpp:130
void setEditable(const bool value)
set editable
Definition Option.cpp:241
bool isRequired() const
check if option is required
Definition Option.cpp:214
const std::string & getValueString() const
Returns the string-representation of the value.
Definition Option.cpp:106
void setSubtopic(const std::string &subtopic)
Sets the subtopic to which this option belongs.
Definition Option.cpp:263
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.
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