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-2020 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  * Option - methods
41  * ----------------------------------------------------------------------- */
42 Option::Option(bool set)
43  : myAmSet(set), myHaveTheDefaultValue(true), myAmWritable(true) {}
44 
45 
47  : myAmSet(s.myAmSet), myHaveTheDefaultValue(s.myHaveTheDefaultValue),
48  myAmWritable(s.myAmWritable) {}
49 
50 
52 
53 
54 Option&
56  if (this == &s) {
57  return *this;
58  }
59  myAmSet = s.myAmSet;
62  return *this;
63 }
64 
65 
66 bool
67 Option::isSet() const {
68  return myAmSet;
69 }
70 
71 
72 double
74  throw InvalidArgument("This is not a double-option");
75 }
76 
77 
78 int
79 Option::getInt() const {
80  throw InvalidArgument("This is not an int-option");
81 }
82 
83 
84 std::string
86  throw InvalidArgument("This is not a string-option");
87 }
88 
89 
90 bool
91 Option::getBool() const {
92  throw InvalidArgument("This is not a bool-option");
93 }
94 
95 
96 const IntVector&
98  throw InvalidArgument("This is not an int vector-option");
99 }
100 
101 const StringVector&
103  throw InvalidArgument("This is not a string vector-option");
104 }
105 
106 bool
108  bool ret = myAmWritable;
109  myHaveTheDefaultValue = false;
110  myAmSet = true;
111  myAmWritable = false;
112  return ret;
113 }
114 
115 
116 void
118  myAmSet = false;
119  myAmWritable = true;
120 }
121 
122 
123 bool
124 Option::isBool() const {
125  return false;
126 }
127 
128 
129 bool
131  return myHaveTheDefaultValue;
132 }
133 
134 
135 bool
137  return false;
138 }
139 
140 
141 bool
143  return myAmWritable;
144 }
145 
146 
147 void
149  myAmWritable = true;
150 }
151 
152 
153 void
155  myHaveTheDefaultValue = true;
156 }
157 
158 
159 const std::string&
161  return myDescription;
162 }
163 
164 
165 void
166 Option::setDescription(const std::string& desc) {
167  myDescription = desc;
168 }
169 
170 
171 const std::string&
173  return myTypeName;
174 }
175 
176 
177 
178 
179 /* -------------------------------------------------------------------------
180  * Option_Integer - methods
181  * ----------------------------------------------------------------------- */
183  : Option(true), myValue(value) {
184  myTypeName = "INT";
185 }
186 
187 
189 
190 
192  : Option(s) {
193  myValue = s.myValue;
194 }
195 
196 
199  if (this == &s) {
200  return *this;
201  }
203  myValue = s.myValue;
204  return *this;
205 }
206 
207 
208 int
210  return myValue;
211 }
212 
213 
214 bool
215 Option_Integer::set(const std::string& v) {
216  try {
218  return markSet();
219  } catch (...) {
220  std::string s = "'" + v + "' is not a valid integer.";
221  throw ProcessError(s);
222  }
223 }
224 
225 
226 std::string
228  std::ostringstream s;
229  s << myValue;
230  return s.str();
231 }
232 
233 
234 
235 /* -------------------------------------------------------------------------
236  * Option_String - methods
237  * ----------------------------------------------------------------------- */
239  : Option() {
240  myTypeName = "STR";
241 }
242 
243 
244 Option_String::Option_String(const std::string& value, std::string typeName)
245  : Option(true), myValue(value) {
246  myTypeName = typeName;
247 }
248 
249 
251 
252 
254  : Option(s) {
255  myValue = s.myValue;
256 }
257 
258 
261  if (this == &s) {
262  return *this;
263  }
265  myValue = s.myValue;
266  return *this;
267 }
268 
269 
270 std::string
272  return myValue;
273 }
274 
275 
276 bool
277 Option_String::set(const std::string& v) {
278  myValue = v;
279  return markSet();
280 }
281 
282 
283 std::string
285  return myValue;
286 }
287 
288 
289 
290 /* -------------------------------------------------------------------------
291  * Option_Float - methods
292  * ----------------------------------------------------------------------- */
294  : Option(true), myValue(value) {
295  myTypeName = "FLOAT";
296 }
297 
298 
300 
301 
303  : Option(s) {
304  myValue = s.myValue;
305 }
306 
307 
310  if (this == &s) {
311  return *this;
312  }
314  myValue = s.myValue;
315  return *this;
316 }
317 
318 
319 double
321  return myValue;
322 }
323 
324 
325 bool
326 Option_Float::set(const std::string& v) {
327  try {
329  return markSet();
330  } catch (...) {
331  throw ProcessError("'" + v + "' is not a valid float.");
332  }
333 }
334 
335 
336 std::string
338  std::ostringstream s;
339  s << myValue;
340  return s.str();
341 }
342 
343 
344 
345 /* -------------------------------------------------------------------------
346  * Option_Bool - methods
347  * ----------------------------------------------------------------------- */
349  : Option(true), myValue(value) {
350  myTypeName = "BOOL";
351 }
352 
353 
355 
356 
358  : Option(s) {
359  myValue = s.myValue;
360 }
361 
362 
365  if (this == &s) {
366  return *this;
367  }
369  myValue = s.myValue;
370  return *this;
371 }
372 
373 
374 bool
376  return myValue;
377 }
378 
379 
380 bool
381 Option_Bool::set(const std::string& v) {
382  try {
384  return markSet();
385  } catch (...) {
386  throw ProcessError("'" + v + "' is not a valid bool.");
387  }
388 }
389 
390 
391 std::string
393  if (myValue) {
394  return "true";
395  }
396  return "false";
397 }
398 
399 
400 bool
402  return true;
403 }
404 
405 
406 
407 /* -------------------------------------------------------------------------
408  * Option_BoolExtended - methods
409  * ----------------------------------------------------------------------- */
411  : Option_Bool(value), myValueString(value ? "true" : "false") {
412 }
413 
414 
416 
417 
419  : Option_Bool(s.myValue) {
421 }
422 
423 
426  if (this == &s) {
427  return *this;
428  }
430  myValue = s.myValue;
432  return *this;
433 }
434 
435 
436 bool
437 Option_BoolExtended::set(const std::string& v) {
438  try {
440  myValueString = "";
441  } catch (...) {
442  myValue = true;
443  myValueString = v;
444  }
445  return markSet();
446 }
447 
448 
449 std::string
451  return myValueString;
452 }
453 
454 
455 /* -------------------------------------------------------------------------
456  * Option_UIntVector - methods
457  * ----------------------------------------------------------------------- */
459  : Option() {
460  myTypeName = "INT[]";
461 }
462 
463 
465  : Option(true), myValue(value) {
466  myTypeName = "INT[]";
467 }
468 
469 
471  : Option(s), myValue(s.myValue) {}
472 
473 
475 
476 
480  myValue = s.myValue;
481  return (*this);
482 }
483 
484 
485 const IntVector&
487  return myValue;
488 }
489 
490 
491 bool
492 Option_IntVector::set(const std::string& v) {
493  myValue.clear();
494  try {
495  if (v.find(';') != std::string::npos) {
496  WRITE_WARNING("Please note that using ';' as list separator is deprecated and not accepted anymore.");
497  }
498  StringTokenizer st(v, ",", true);
499  while (st.hasNext()) {
500  myValue.push_back(StringUtils::toInt(st.next()));
501  }
502  return markSet();
503  } catch (EmptyData&) {
504  throw ProcessError("Empty element occurred in " + v);
505  } catch (...) {
506  throw ProcessError("'" + v + "' is not a valid integer vector.");
507  }
508 }
509 
510 
511 std::string
513  return joinToString(myValue, ',');
514 }
515 
516 
517 /* -------------------------------------------------------------------------
518  * Option_StringVector - methods
519  * ----------------------------------------------------------------------- */
521  myTypeName = "STR[]";
522 }
523 
525  : Option(true), myValue(value) {
526  myTypeName = "STR[]";
527 }
528 
530  : Option(s), myValue(s.myValue) {}
531 
533 
537  myValue = s.myValue;
538  return (*this);
539 }
540 
541 const StringVector&
543  return myValue;
544 }
545 
546 bool
547 Option_StringVector::set(const std::string& v) {
548  myValue.clear();
549  try {
550  if (v.find(';') != std::string::npos) {
551  WRITE_WARNING("Please note that using ';' as list separator is deprecated and not accepted anymore.");
552  }
553  StringTokenizer st(v, ",", true);
554  while (st.hasNext()) {
555  myValue.push_back(StringUtils::prune(st.next()));
556  }
557  return markSet();
558  } catch (EmptyData&) {
559  throw ProcessError("Empty element occurred in " + v);
560  } catch (...) {
561  throw ProcessError("'" + v + "' is not a valid string vector.");
562  }
563 }
564 
565 std::string
567  return joinToString(myValue, ',');
568 }
569 
570 
571 /* -------------------------------------------------------------------------
572  * Option_FileName - methods
573  * ----------------------------------------------------------------------- */
575  myTypeName = "FILE";
576 }
577 
579  : Option_StringVector(value) {
580  myTypeName = "FILE";
581 }
582 
584  : Option_StringVector(s) {}
585 
587 
590  return (*this);
591 }
592 
594  return true;
595 }
596 
597 std::string
600 }
601 
602 std::string Option_FileName::getValueString() const {
604 }
605 
606 
607 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
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:250
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:450
Option_BoolExtended(bool value)
Constructor for an option that can be used without an argument like Option_BoolExtended but which als...
Definition: Option.cpp:410
std::string myValueString
Definition: Option.h:640
~Option_BoolExtended()
Destructor.
Definition: Option.cpp:415
bool set(const std::string &v)
Definition: Option.cpp:437
Option_BoolExtended & operator=(const Option_BoolExtended &s)
Assignment operator.
Definition: Option.cpp:425
Option_Bool & operator=(const Option_Bool &s)
Assignment operator.
Definition: Option.cpp:364
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:375
~Option_Bool()
Destructor.
Definition: Option.cpp:354
virtual std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:392
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition: Option.cpp:401
Option_Bool(bool value)
Constructor for an option with a default value.
Definition: Option.cpp:348
virtual bool set(const std::string &v)
Definition: Option.cpp:381
bool myValue
Definition: Option.h:591
Option_FileName & operator=(const Option_FileName &s)
Assignment operator.
Definition: Option.cpp:588
std::string getString() const
Legacy method that returns the stored filenames as a comma-separated string.
Definition: Option.cpp:598
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:602
virtual ~Option_FileName()
Destructor.
Definition: Option.cpp:586
Option_FileName()
Constructor for an option with no default value.
Definition: Option.cpp:574
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition: Option.cpp:593
double getFloat() const
Returns the stored double value.
Definition: Option.cpp:320
~Option_Float()
Destructor.
Definition: Option.cpp:299
bool set(const std::string &v)
Stores the given value after parsing it into a double.
Definition: Option.cpp:326
double myValue
Definition: Option.h:528
Option_Float & operator=(const Option_Float &s)
Assignment operator.
Definition: Option.cpp:309
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:337
Option_Float(double value)
Constructor for an option with a default value.
Definition: Option.cpp:293
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:486
Option_IntVector & operator=(const Option_IntVector &s)
Assignment operator.
Definition: Option.cpp:478
IntVector myValue
Definition: Option.h:711
Option_IntVector()
Constructor for an option with no default value.
Definition: Option.cpp:458
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:512
bool set(const std::string &v)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:492
virtual ~Option_IntVector()
Destructor.
Definition: Option.cpp:474
An integer-option.
Definition: Option.h:329
Option_Integer(int value)
Constructor for an option with a default value.
Definition: Option.cpp:182
Option_Integer & operator=(const Option_Integer &s)
Assignment operator.
Definition: Option.cpp:198
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:227
~Option_Integer()
Destructor.
Definition: Option.cpp:188
int getInt() const
Returns the stored integer value.
Definition: Option.cpp:209
bool set(const std::string &v)
Stores the given value after parsing it into an integer.
Definition: Option.cpp:215
bool set(const std::string &v)
Stores the given value.
Definition: Option.cpp:277
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:284
virtual ~Option_String()
Destructor.
Definition: Option.cpp:250
std::string myValue
Definition: Option.h:460
Option_String & operator=(const Option_String &s)
Assignment operator.
Definition: Option.cpp:260
std::string getString() const
Returns the stored string value.
Definition: Option.cpp:271
Option_String()
Constructor for an option with no default value.
Definition: Option.cpp:238
const StringVector & getStringVector() const
Returns the stored string vector.
Definition: Option.cpp:542
Option_StringVector & operator=(const Option_StringVector &s)
Assignment operator.
Definition: Option.cpp:535
StringVector myValue
Definition: Option.h:774
Option_StringVector()
Constructor for an option with no default value.
Definition: Option.cpp:520
bool set(const std::string &v)
Stores the given value after parsing it into a vector of strings.
Definition: Option.cpp:547
virtual ~Option_StringVector()
Destructor.
Definition: Option.cpp:532
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:566
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:311
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:142
bool isSet() const
returns the information whether this options holds a valid value
Definition: Option.cpp:67
virtual ~Option()
Definition: Option.cpp:51
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:130
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:85
bool myAmSet
information whether the value is set
Definition: Option.h:308
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:97
void resetWritable()
Resets the option to be writeable.
Definition: Option.cpp:148
const std::string & getDescription() const
Returns the description of what this option does.
Definition: Option.cpp:160
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition: Option.h:303
void unSet()
marks this option as unset
Definition: Option.cpp:117
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition: Option.cpp:136
std::string myDescription
The description what this option does.
Definition: Option.h:317
virtual const StringVector & getStringVector() const
Returns the stored string vector.
Definition: Option.cpp:102
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:166
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:172
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:79
virtual double getFloat() const
Returns the stored double value.
Definition: Option.cpp:73
bool markSet()
Marks the information as set.
Definition: Option.cpp:107
virtual bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:91
Option(bool set=false)
Constructor.
Definition: Option.cpp:42
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:154
bool myAmWritable
information whether the value may be changed
Definition: Option.h:314
virtual Option & operator=(const Option &s)
Assignment operator.
Definition: Option.cpp:55
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition: Option.cpp:124
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 std::string urlEncode(const std::string &url, const std::string encodeWhich="")
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:47
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