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