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