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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A class representing a single program option
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <exception>
27 #include <sstream>
28 #include "Option.h"
34 #include <utils/common/ToString.h>
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
40 /* -------------------------------------------------------------------------
41  * Option - methods
42  * ----------------------------------------------------------------------- */
43 Option::Option(bool set)
44  : myAmSet(set), myHaveTheDefaultValue(true), myAmWritable(true) {}
45 
46 
48  : myAmSet(s.myAmSet), myHaveTheDefaultValue(s.myHaveTheDefaultValue),
49  myAmWritable(s.myAmWritable) {}
50 
51 
53 
54 
55 Option&
57  if (this == &s) {
58  return *this;
59  }
60  myAmSet = s.myAmSet;
63  return *this;
64 }
65 
66 
67 bool
68 Option::isSet() const {
69  return myAmSet;
70 }
71 
72 
73 double
75  throw InvalidArgument("This is not a double-option");
76 }
77 
78 
79 int
80 Option::getInt() const {
81  throw InvalidArgument("This is not an int-option");
82 }
83 
84 
85 std::string
87  throw InvalidArgument("This is not a string-option");
88 }
89 
90 
91 bool
92 Option::getBool() const {
93  throw InvalidArgument("This is not a bool-option");
94 }
95 
96 
97 const IntVector&
99  throw InvalidArgument("This is not an int vector-option");
100 }
101 
102 const StringVector&
104  throw InvalidArgument("This is not a string vector-option");
105 }
106 
107 bool
109  bool ret = myAmWritable;
110  myHaveTheDefaultValue = false;
111  myAmSet = true;
112  myAmWritable = false;
113  return ret;
114 }
115 
116 
117 void
119  myAmSet = false;
120  myAmWritable = true;
121 }
122 
123 
124 bool
125 Option::isBool() const {
126  return false;
127 }
128 
129 
130 bool
132  return myHaveTheDefaultValue;
133 }
134 
135 
136 bool
138  return false;
139 }
140 
141 
142 bool
144  return myAmWritable;
145 }
146 
147 
148 void
150  myAmWritable = true;
151 }
152 
153 
154 void
156  myHaveTheDefaultValue = true;
157 }
158 
159 
160 const std::string&
162  return myDescription;
163 }
164 
165 
166 void
167 Option::setDescription(const std::string& desc) {
168  myDescription = desc;
169 }
170 
171 
172 const std::string&
174  return myTypeName;
175 }
176 
177 
178 
179 
180 /* -------------------------------------------------------------------------
181  * Option_Integer - methods
182  * ----------------------------------------------------------------------- */
184  : Option(true), myValue(value) {
185  myTypeName = "INT";
186 }
187 
188 
190 
191 
193  : Option(s) {
194  myValue = s.myValue;
195 }
196 
197 
200  if (this == &s) {
201  return *this;
202  }
204  myValue = s.myValue;
205  return *this;
206 }
207 
208 
209 int
211  return myValue;
212 }
213 
214 
215 bool
216 Option_Integer::set(const std::string& v) {
217  try {
219  return markSet();
220  } catch (...) {
221  std::string s = "'" + v + "' is not a valid integer.";
222  throw ProcessError(s);
223  }
224 }
225 
226 
227 std::string
229  std::ostringstream s;
230  s << myValue;
231  return s.str();
232 }
233 
234 
235 
236 /* -------------------------------------------------------------------------
237  * Option_String - methods
238  * ----------------------------------------------------------------------- */
240  : Option() {
241  myTypeName = "STR";
242 }
243 
244 
245 Option_String::Option_String(const std::string& value, std::string typeName)
246  : Option(true), myValue(value) {
247  myTypeName = typeName;
248 }
249 
250 
252 
253 
255  : Option(s) {
256  myValue = s.myValue;
257 }
258 
259 
262  if (this == &s) {
263  return *this;
264  }
266  myValue = s.myValue;
267  return *this;
268 }
269 
270 
271 std::string
273  return myValue;
274 }
275 
276 
277 bool
278 Option_String::set(const std::string& v) {
279  myValue = v;
280  return markSet();
281 }
282 
283 
284 std::string
286  return myValue;
287 }
288 
289 
290 
291 /* -------------------------------------------------------------------------
292  * Option_Float - methods
293  * ----------------------------------------------------------------------- */
295  : Option(true), myValue(value) {
296  myTypeName = "FLOAT";
297 }
298 
299 
301 
302 
304  : Option(s) {
305  myValue = s.myValue;
306 }
307 
308 
311  if (this == &s) {
312  return *this;
313  }
315  myValue = s.myValue;
316  return *this;
317 }
318 
319 
320 double
322  return myValue;
323 }
324 
325 
326 bool
327 Option_Float::set(const std::string& v) {
328  try {
330  return markSet();
331  } catch (...) {
332  throw ProcessError("'" + v + "' is not a valid float.");
333  }
334 }
335 
336 
337 std::string
339  std::ostringstream s;
340  s << myValue;
341  return s.str();
342 }
343 
344 
345 
346 /* -------------------------------------------------------------------------
347  * Option_Bool - methods
348  * ----------------------------------------------------------------------- */
350  : Option(true), myValue(value) {
351  myTypeName = "BOOL";
352 }
353 
354 
356 
357 
359  : Option(s) {
360  myValue = s.myValue;
361 }
362 
363 
366  if (this == &s) {
367  return *this;
368  }
370  myValue = s.myValue;
371  return *this;
372 }
373 
374 
375 bool
377  return myValue;
378 }
379 
380 
381 bool
382 Option_Bool::set(const std::string& v) {
383  try {
385  return markSet();
386  } catch (...) {
387  throw ProcessError("'" + v + "' is not a valid bool.");
388  }
389 }
390 
391 
392 std::string
394  if (myValue) {
395  return "true";
396  }
397  return "false";
398 }
399 
400 
401 bool
403  return true;
404 }
405 
406 
407 
408 /* -------------------------------------------------------------------------
409  * Option_BoolExtended - methods
410  * ----------------------------------------------------------------------- */
412  : Option_Bool(value), myValueString(value ? "true" : "false") {
413 }
414 
415 
417 
418 
420  : Option_Bool(s.myValue) {
422 }
423 
424 
427  if (this == &s) {
428  return *this;
429  }
431  myValue = s.myValue;
433  return *this;
434 }
435 
436 
437 bool
438 Option_BoolExtended::set(const std::string& v) {
439  try {
441  myValueString = "";
442  } catch (...) {
443  myValue = true;
444  myValueString = v;
445  }
446  return markSet();
447 }
448 
449 
450 std::string
452  return myValueString;
453 }
454 
455 
456 /* -------------------------------------------------------------------------
457  * Option_UIntVector - methods
458  * ----------------------------------------------------------------------- */
460  : Option() {
461  myTypeName = "INT[]";
462 }
463 
464 
466  : Option(true), myValue(value) {
467  myTypeName = "INT[]";
468 }
469 
470 
472  : Option(s), myValue(s.myValue) {}
473 
474 
476 
477 
481  myValue = s.myValue;
482  return (*this);
483 }
484 
485 
486 const IntVector&
488  return myValue;
489 }
490 
491 
492 bool
493 Option_IntVector::set(const std::string& v) {
494  myValue.clear();
495  try {
496  if (v.find(';') != std::string::npos) {
497  WRITE_WARNING("Please note that using ';' as list separator is deprecated and not accepted anymore.");
498  }
499  StringTokenizer st(v, ",", true);
500  while (st.hasNext()) {
501  myValue.push_back(StringUtils::toInt(st.next()));
502  }
503  return markSet();
504  } catch (EmptyData&) {
505  throw ProcessError("Empty element occurred in " + v);
506  } catch (...) {
507  throw ProcessError("'" + v + "' is not a valid integer vector.");
508  }
509 }
510 
511 
512 std::string
514  return joinToString(myValue, ',');
515 }
516 
517 
518 /* -------------------------------------------------------------------------
519  * Option_StringVector - methods
520  * ----------------------------------------------------------------------- */
522  myTypeName = "STR[]";
523 }
524 
526  : Option(true), myValue(value) {
527  myTypeName = "STR[]";
528 }
529 
531  : Option(s), myValue(s.myValue) {}
532 
534 
538  myValue = s.myValue;
539  return (*this);
540 }
541 
542 const StringVector&
544  return myValue;
545 }
546 
547 bool
548 Option_StringVector::set(const std::string& v) {
549  myValue.clear();
550  try {
551  if (v.find(';') != std::string::npos) {
552  WRITE_WARNING("Please note that using ';' as list separator is deprecated and not accepted anymore.");
553  }
554  StringTokenizer st(v, ",", true);
555  while (st.hasNext()) {
556  myValue.push_back(StringUtils::prune(st.next()));
557  }
558  return markSet();
559  } catch (EmptyData&) {
560  throw ProcessError("Empty element occurred in " + v);
561  } catch (...) {
562  throw ProcessError("'" + v + "' is not a valid string vector.");
563  }
564 }
565 
566 std::string
568  return joinToString(myValue, ',');
569 }
570 
571 
572 /* -------------------------------------------------------------------------
573  * Option_FileName - methods
574  * ----------------------------------------------------------------------- */
576  myTypeName = "FILE";
577 }
578 
580  : Option_StringVector(value) {
581  myTypeName = "FILE";
582 }
583 
585  : Option_StringVector(s) {}
586 
588 
591  return (*this);
592 }
593 
595  return true;
596 }
597 
598 std::string
601 }
602 
603 std::string Option_FileName::getValueString() const {
605 }
606 
607 
608 /****************************************************************************/
609 
Option_StringVector::getStringVector
const StringVector & getStringVector() const
Returns the stored string vector.
Definition: Option.cpp:543
Option_StringVector::operator=
Option_StringVector & operator=(const Option_StringVector &s)
Assignment operator.
Definition: Option.cpp:536
Option_FileName::getString
std::string getString() const
Legacy method that returns the stored filenames as a comma-separated string.
Definition: Option.cpp:599
Option::myAmSet
bool myAmSet
information whether the value is set
Definition: Option.h:310
Option::~Option
virtual ~Option()
Definition: Option.cpp:52
ToString.h
Option_BoolExtended::~Option_BoolExtended
~Option_BoolExtended()
Destructor.
Definition: Option.cpp:416
Option_String::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:285
Option_BoolExtended
Definition: Option.h:602
StringUtils::toBool
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter
Definition: StringUtils.cpp:374
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
Option
A class representing a single program option.
Definition: Option.h:75
Option_Bool
Definition: Option.h:538
Option_StringVector::myValue
StringVector myValue
Definition: Option.h:776
Option_Integer::set
bool set(const std::string &v)
Stores the given value after parsing it into an integer.
Definition: Option.cpp:216
Option_Bool::isBool
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition: Option.cpp:402
Option::isWriteable
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:143
Option_Integer::operator=
Option_Integer & operator=(const Option_Integer &s)
Assignment operator.
Definition: Option.cpp:199
Option_BoolExtended::operator=
Option_BoolExtended & operator=(const Option_BoolExtended &s)
Assignment operator.
Definition: Option.cpp:426
Option_Float::operator=
Option_Float & operator=(const Option_Float &s)
Assignment operator.
Definition: Option.cpp:310
Option::resetDefault
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:155
StringTokenizer::hasNext
bool hasNext()
returns the information whether further substrings exist
Definition: StringTokenizer.cpp:94
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
Option_IntVector::Option_IntVector
Option_IntVector()
Constructor for an option with no default value.
Definition: Option.cpp:459
Option_Bool::operator=
Option_Bool & operator=(const Option_Bool &s)
Assignment operator.
Definition: Option.cpp:365
Option::isFileName
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition: Option.cpp:137
Option_Bool::set
virtual bool set(const std::string &v)
Definition: Option.cpp:382
MsgHandler.h
Option_Float::myValue
double myValue
Definition: Option.h:530
Option_StringVector::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:567
Option_StringVector::~Option_StringVector
virtual ~Option_StringVector()
Destructor.
Definition: Option.cpp:533
Option_IntVector::myValue
IntVector myValue
Definition: Option.h:713
Option_FileName::isFileName
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition: Option.cpp:594
EmptyData
Definition: UtilExceptions.h:68
StringTokenizer::next
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
Definition: StringTokenizer.cpp:99
Option::isBool
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition: Option.cpp:125
Option::setDescription
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:167
Option::isDefault
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:131
Option_IntVector::set
bool set(const std::string &v)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:493
Option::myDescription
std::string myDescription
The description what this option does.
Definition: Option.h:319
IntVector
std::vector< int > IntVector
Definition of a vector of ints.
Definition: Option.h:40
Option_Integer::getInt
int getInt() const
Returns the stored integer value.
Definition: Option.cpp:210
Option::getTypeName
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:173
Option::Option
Option(bool set=false)
Constructor.
Definition: Option.cpp:43
Option_Integer::Option_Integer
Option_Integer(int value)
Constructor for an option with a default value.
Definition: Option.cpp:183
Option_BoolExtended::Option_BoolExtended
Option_BoolExtended(bool value)
Constructor for an option that can be used without an argument like Option_BoolExtended but which als...
Definition: Option.cpp:411
Option_Float::getFloat
double getFloat() const
Returns the stored double value.
Definition: Option.cpp:321
StringUtils::prune
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:48
StringVector
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition: Option.h:45
Option_Bool::getValueString
virtual std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:393
Option_FileName::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:603
Option_String::getString
std::string getString() const
Returns the stored string value.
Definition: Option.cpp:272
Option_String::operator=
Option_String & operator=(const Option_String &s)
Assignment operator.
Definition: Option.cpp:261
Option_BoolExtended::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:451
StringTokenizer
Definition: StringTokenizer.h:61
Option_Integer::~Option_Integer
~Option_Integer()
Destructor.
Definition: Option.cpp:189
Option::getDescription
const std::string & getDescription() const
Returns the description of what this option does.
Definition: Option.cpp:161
Option_Float::Option_Float
Option_Float(double value)
Constructor for an option with a default value.
Definition: Option.cpp:294
ProcessError
Definition: UtilExceptions.h:39
Option_StringVector::set
bool set(const std::string &v)
Stores the given value after parsing it into a vector of strings.
Definition: Option.cpp:548
Option::isSet
bool isSet() const
returns the information whether this options holds a valid value
Definition: Option.cpp:68
Option_IntVector::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:513
Option_String::Option_String
Option_String()
Constructor for an option with no default value.
Definition: Option.cpp:239
Option_String
Definition: Option.h:399
UtilExceptions.h
Option_Float::set
bool set(const std::string &v)
Stores the given value after parsing it into a double.
Definition: Option.cpp:327
Option_FileName::Option_FileName
Option_FileName()
Constructor for an option with no default value.
Definition: Option.cpp:575
Option::getFloat
virtual double getFloat() const
Returns the stored double value.
Definition: Option.cpp:74
Option_Bool::~Option_Bool
~Option_Bool()
Destructor.
Definition: Option.cpp:355
Option_Float::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:338
StringUtils.h
StringUtils::urlEncode
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
Definition: StringUtils.cpp:206
StringUtils::toInt
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:278
Option_String::set
bool set(const std::string &v)
Stores the given value.
Definition: Option.cpp:278
Option_StringVector
Definition: Option.h:720
Option.h
Option::resetWritable
void resetWritable()
Resets the option to be writeable.
Definition: Option.cpp:149
Option_Integer::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:228
Option_IntVector::~Option_IntVector
virtual ~Option_IntVector()
Destructor.
Definition: Option.cpp:475
Option::unSet
void unSet()
marks this option as unset
Definition: Option.cpp:118
InvalidArgument
Definition: UtilExceptions.h:56
Option_Float
Definition: Option.h:470
Option_IntVector::getIntVector
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:487
Option_FileName::~Option_FileName
virtual ~Option_FileName()
Destructor.
Definition: Option.cpp:587
Option_Bool::getBool
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:376
Option::getString
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:86
Option_StringVector::Option_StringVector
Option_StringVector()
Constructor for an option with no default value.
Definition: Option.cpp:521
Option_Float::~Option_Float
~Option_Float()
Destructor.
Definition: Option.cpp:300
joinToString
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:246
Option_Bool::Option_Bool
Option_Bool(bool value)
Constructor for an option with a default value.
Definition: Option.cpp:349
Option_Bool::myValue
bool myValue
Definition: Option.h:593
Option::getIntVector
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:98
Option_BoolExtended::myValueString
std::string myValueString
Definition: Option.h:642
config.h
Option_FileName
Definition: Option.h:783
Option_BoolExtended::set
bool set(const std::string &v)
Definition: Option.cpp:438
Option::markSet
bool markSet()
Marks the information as set.
Definition: Option.cpp:108
Option_IntVector
Definition: Option.h:650
StringTokenizer.h
Option::getBool
virtual bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:92
Option::operator=
virtual Option & operator=(const Option &s)
Assignment operator.
Definition: Option.cpp:56
Option::getStringVector
virtual const StringVector & getStringVector() const
Returns the stored string vector.
Definition: Option.cpp:103
Option::myTypeName
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition: Option.h:305
Option::getInt
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:80
Option_Integer::myValue
int myValue
Definition: Option.h:391
Option_String::~Option_String
virtual ~Option_String()
Destructor.
Definition: Option.cpp:251
Option_Integer
An integer-option.
Definition: Option.h:331
Option_String::myValue
std::string myValue
Definition: Option.h:462
Option::myAmWritable
bool myAmWritable
information whether the value may be changed
Definition: Option.h:316
Option_IntVector::operator=
Option_IntVector & operator=(const Option_IntVector &s)
Assignment operator.
Definition: Option.cpp:479
Option_FileName::operator=
Option_FileName & operator=(const Option_FileName &s)
Assignment operator.
Definition: Option.cpp:589
Option::myHaveTheDefaultValue
bool myHaveTheDefaultValue
information whether the value is the default value (is then set)
Definition: Option.h:313