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-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/****************************************************************************/
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
198 if (myValueString.empty()) {
199 myAmSet = false;
200 }
201}
202
203
204const std::string&
206 return myDescription;
207}
208
209
210void
211Option::setDescription(const std::string& desc) {
212 myDescription = desc;
213}
214
215
216bool
218 return myRequired;
219}
220
221
222void
224 myRequired = true;
225}
226
227bool
229 return myPositional;
230}
231
232void
236
237
238bool
240 return myEditable;
241}
242
243
244void Option::setEditable(const bool value) {
245 myEditable = value;
246}
247
248
249const std::string&
251 return myListSeparator;
252}
253
254void
255Option::setListSeparator(const std::string& listSep) {
256 myListSeparator = listSep;
257}
258
259const std::string&
261 return mySubTopic;
262}
263
264
265void
266Option::setSubtopic(const std::string& subtopic) {
267 mySubTopic = subtopic;
268}
269
270
271const std::string&
273 return myTypeName;
274}
275
276// -------------------------------------------------------------------------
277// Option_Integer - methods
278// -------------------------------------------------------------------------
279
281 Option(true),
282 myValue(value) {
283 myTypeName = "INT";
284 myValueString = toString(value);
285}
286
287
288int
290 return myValue;
291}
292
293
294bool
295Option_Integer::set(const std::string& v, const std::string& orig, const bool /* append */) {
296 try {
298 return markSet(orig);
299 } catch (...) {
300 std::string s = "'" + v + "' is not a valid integer.";
301 throw ProcessError(s);
302 }
303}
304
305
306bool
308 return true;
309}
310
311// -------------------------------------------------------------------------
312// Option_String - methods
313// -------------------------------------------------------------------------
314
316 Option() {
317 myTypeName = "STR";
318}
319
320
321Option_String::Option_String(const std::string& value, std::string typeName) :
322 Option(true),
323 myValue(value) {
324 myTypeName = typeName;
325 myValueString = value;
326}
327
328
329std::string
331 return myValue;
332}
333
334
335bool
336Option_String::set(const std::string& v, const std::string& orig, const bool /* append */) {
337 myValue = v;
338 return markSet(orig);
339}
340
341// -------------------------------------------------------------------------
342// Option_Float - methods
343// -------------------------------------------------------------------------
344
346 Option(true),
347 myValue(value) {
348 myTypeName = "FLOAT";
349 std::ostringstream oss;
350 oss << value;
351 myValueString = oss.str();
352}
353
354
355double
357 return myValue;
358}
359
360
361bool
362Option_Float::set(const std::string& v, const std::string& orig, const bool /* append */) {
363 try {
365 return markSet(orig);
366 } catch (...) {
367 throw ProcessError(TLF("'%' is not a valid float.", v));
368 }
369}
370
371
372bool
374 return true;
375}
376
377// -------------------------------------------------------------------------
378// Option_Bool - methods
379// -------------------------------------------------------------------------
380
382 Option(true),
383 myValue(value) {
384 myTypeName = "BOOL";
385 myValueString = value ? "true" : "false";
386}
387
388
389bool
391 return myValue;
392}
393
394
395bool
396Option_Bool::set(const std::string& v, const std::string& orig, const bool /* append */) {
397 try {
399 return markSet(orig);
400 } catch (...) {
401 throw ProcessError(TLF("'%' is not a valid bool.", v));
402 }
403}
404
405
406bool
408 return true;
409}
410
411// -------------------------------------------------------------------------
412// Option_BoolExtended - methods
413// -------------------------------------------------------------------------
414
418
419
420bool
421Option_BoolExtended::set(const std::string& v, const std::string& orig, const bool /* append */) {
422 try {
424 return markSet("");
425 } catch (...) {
426 myValue = true;
427 }
428 return markSet(orig);
429}
430
431// -------------------------------------------------------------------------
432// Option_IntVector - methods
433// -------------------------------------------------------------------------
434
439
440
442 : Option(true), myValue(value) {
443 myTypeName = "INT[]";
444 myValueString = joinToString(value, ",");
445}
446
447
448const IntVector&
450 return myValue;
451}
452
453
454bool
455Option_IntVector::set(const std::string& v, const std::string& orig, const bool append) {
456 if (!append) {
457 myValue.clear();
458 }
459 try {
460 if (v.find(';') != std::string::npos) {
461 WRITE_WARNING(TL("Please note that using ';' as list separator is deprecated and not accepted anymore."));
462 }
463 StringTokenizer st(v, ",", true);
464 while (st.hasNext()) {
465 myValue.push_back(StringUtils::toInt(st.next()));
466 }
467 return markSet(orig);
468 } catch (EmptyData&) {
469 throw ProcessError("Empty element occurred in " + v);
470 } catch (...) {
471 throw ProcessError(TLF("'%' is not a valid integer vector.", v));
472 }
473}
474
475// -------------------------------------------------------------------------
476// Option_StringVector - methods
477// -------------------------------------------------------------------------
478
483
484
486 Option(true), myValue(value) {
487 myTypeName = "STR[]";
488 myValueString = joinToString(value, ",");
489}
490
491
492const StringVector&
496
497
498bool
499Option_StringVector::set(const std::string& v, const std::string& orig, const bool append) {
500 if (!append) {
501 myValue.clear();
502 }
503 StringTokenizer st(v, ",");
504 while (st.hasNext()) {
505 myValue.push_back(StringUtils::prune(st.next()));
506 }
507 return markSet(append && getValueString() != "" ? getValueString() + "," + orig : orig);
508}
509
510// -------------------------------------------------------------------------
511// Option_FileName - methods
512// -------------------------------------------------------------------------
513
518
519
521 Option_StringVector(value) {
522 myTypeName = "FILE";
523}
524
525
526bool
528 return true;
529}
530
531
532std::string
534 return joinToString(getStringVector(), ",");
535}
536
537// -------------------------------------------------------------------------
538// Option_Network - methods
539// -------------------------------------------------------------------------
540
541Option_Network::Option_Network(const std::string& value) :
542 Option_String(value, "NETWORK") {
543}
544
545
547 return true;
548}
549
550// -------------------------------------------------------------------------
551// Option_Additional - methods
552// -------------------------------------------------------------------------
553
554Option_Additional::Option_Additional(const std::string& value) :
555 Option_String(value, "ADDITIONAL") {
556}
557
558
559bool
561 return true;
562}
563
564// -------------------------------------------------------------------------
565// Option_Route - methods
566// -------------------------------------------------------------------------
567
568Option_Route::Option_Route(const std::string& value) :
569 Option_String(value, "ROUTE") {
570}
571
572
573bool
575 return true;
576}
577
578// -------------------------------------------------------------------------
579// Option_Data - methods
580// -------------------------------------------------------------------------
581
582Option_Data::Option_Data(const std::string& value) :
583 Option_String(value, "DATA") {
584}
585
586
587bool
589 return true;
590}
591
592// -------------------------------------------------------------------------
593// Option_Data - methods
594// -------------------------------------------------------------------------
595
596Option_SumoConfig::Option_SumoConfig(const std::string& value) :
597 Option_String(value, "SUMOCONFIG") {
598}
599
600
601bool
603 return true;
604}
605
606// -------------------------------------------------------------------------
607// Option_Data - methods
608// -------------------------------------------------------------------------
609
610Option_Edge::Option_Edge(const std::string& value) :
611 Option_String(value, "EDGE") {
612}
613
614
615bool
617 return true;
618}
619
620// -------------------------------------------------------------------------
621// Option_Data - methods
622// -------------------------------------------------------------------------
623
624Option_EdgeVector::Option_EdgeVector(const std::string& value) :
625 Option_String(value, "EDGE[]") {
626}
627
628
629bool
631 return true;
632}
633
634/****************************************************************************/
#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:322
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
Option_Additional(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:554
bool isAdditional() const
Returns true, the information whether this option is a file name.
Definition Option.cpp:560
Option_BoolExtended(bool value)
Constructor for an option that can be used without an argument like Option_BoolExtended but which als...
Definition Option.cpp:415
bool set(const std::string &v, const std::string &orig, const bool append)
sets the given value (converts it to bool)
Definition Option.cpp:421
bool getBool() const
Returns the stored boolean value.
Definition Option.cpp:390
bool set(const std::string &v, const std::string &orig, const bool append)
sets the given value (converts it to bool)
Definition Option.cpp:396
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition Option.cpp:407
Option_Bool(bool value)
Constructor for an option with a default value.
Definition Option.cpp:381
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:582
bool isData() const
Returns true, the information whether this option is a data file.
Definition Option.cpp:588
bool isEdge() const
Returns true, the information whether this option is a list of edges.
Definition Option.cpp:616
Option_Edge(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:610
bool isEdgeVector() const
Returns true, the information whether this option is a list of edges.
Definition Option.cpp:630
Option_EdgeVector(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:624
std::string getString() const
Legacy method that returns the stored filenames as a comma-separated string.
Definition Option.cpp:533
Option_FileName()
Constructor for an option with no default value.
Definition Option.cpp:514
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition Option.cpp:527
bool isFloat() const
Returns the information whether the option is a float option.
Definition Option.cpp:373
double getFloat() const
Returns the stored double value.
Definition Option.cpp:356
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:362
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:345
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition Option.cpp:449
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:455
Option_IntVector()
Constructor for an option with no default value.
Definition Option.cpp:435
bool isInteger() const
Returns the information whether the option is a int option.
Definition Option.cpp:307
Option_Integer(int value)
Constructor for an option with a default value.
Definition Option.cpp:280
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:289
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:295
Option_Network(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:541
bool isNetwork() const
Returns true, the information whether this option is a file name.
Definition Option.cpp:546
bool isRoute() const
Returns true, the information whether this option is a file name.
Definition Option.cpp:574
Option_Route(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:568
bool set(const std::string &v, const std::string &orig, const bool append)
Stores the given value.
Definition Option.cpp:336
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:330
Option_String()
Constructor for an option with no default value.
Definition Option.cpp:315
const StringVector & getStringVector() const
Returns the stored string vector.
Definition Option.cpp:493
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:479
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:499
Option_SumoConfig(const std::string &value)
Constructor for an option with a default value.
Definition Option.cpp:596
bool isSumoConfig() const
Returns true, the information whether this option is a sumo config name.
Definition Option.cpp:602
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:223
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:205
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:255
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:250
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:211
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition Option.cpp:272
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:228
void setPositional()
mark option as positional
Definition Option.cpp:233
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:260
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:239
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:244
bool isRequired() const
check if option is required
Definition Option.cpp:217
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:266
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