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-2018 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 
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_FileName - methods
411  * ----------------------------------------------------------------------- */
413  : Option_String() {
414  myTypeName = "FILE";
415 }
416 
417 
418 Option_FileName::Option_FileName(const std::string& value)
419  : Option_String(value) {
420  myTypeName = "FILE";
421 }
422 
423 
425  : Option_String(s) {}
426 
427 
429 
430 
434  return (*this);
435 }
436 
437 
438 bool
440  return true;
441 }
442 
443 
444 std::string
446  return StringUtils::urlEncode(myValue, " ;%");
447 }
448 
449 
450 
451 /* -------------------------------------------------------------------------
452  * Option_UIntVector - methods
453  * ----------------------------------------------------------------------- */
455  : Option() {
456  myTypeName = "INT[]";
457 }
458 
459 
461  : Option(true), myValue(value) {
462  myTypeName = "INT[]";
463 }
464 
465 
467  : Option(s), myValue(s.myValue) {}
468 
469 
471 
472 
476  myValue = s.myValue;
477  return (*this);
478 }
479 
480 
481 const IntVector&
483  return myValue;
484 }
485 
486 
487 bool
488 Option_IntVector::set(const std::string& v) {
489  myValue.clear();
490  try {
491  if (v.find(';') != std::string::npos) {
492  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
493  }
494  StringTokenizer st(v, ";,", true);
495  while (st.hasNext()) {
496  myValue.push_back(StringUtils::toInt(st.next()));
497  }
498  return markSet();
499  } catch (EmptyData&) {
500  throw ProcessError("Empty element occurred in " + v);
501  } catch (...) {
502  throw ProcessError("'" + v + "' is not a valid integer vector.");
503  }
504 }
505 
506 
507 std::string
509  return joinToString(myValue, ',');
510 }
511 
512 
513 /* -------------------------------------------------------------------------
514  * Option_UFloatVector - methods
515  * ----------------------------------------------------------------------- */
517  : Option() {
518  myTypeName = "FLOAT[]";
519 }
520 
521 
523  : Option(true), myValue(value) {
524  myTypeName = "FLOAT[]";
525 }
526 
527 
529  : Option(s), myValue(s.myValue) {}
530 
531 
533 
534 
538  myValue = s.myValue;
539  return (*this);
540 }
541 
542 
543 const FloatVector&
545  return myValue;
546 }
547 
548 
549 bool
550 Option_FloatVector::set(const std::string& v) {
551  myValue.clear();
552  try {
553  if (v.find(';') != std::string::npos) {
554  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
555  }
556  StringTokenizer st(v, ";,", true);
557  while (st.hasNext()) {
558  myValue.push_back(StringUtils::toDouble(st.next()));
559  }
560  return markSet();
561  } catch (EmptyData&) {
562  throw ProcessError("Empty element occurred in " + v);
563  } catch (...) {
564  throw ProcessError("'" + v + "' is not a valid float vector.");
565  }
566 }
567 
568 
569 std::string
571  return joinToString(myValue, ',');
572 }
573 
574 
575 /****************************************************************************/
576 
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:508
virtual double getFloat() const
Returns the stored double value.
Definition: Option.cpp:75
~Option_Bool()
Destructor.
Definition: Option.cpp:356
double getFloat() const
Returns the stored double value.
Definition: Option.cpp:322
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:99
bool set(const std::string &v)
Stores the given value after parsing it into an integer.
Definition: Option.cpp:217
bool markSet()
Marks the information as set.
Definition: Option.cpp:109
virtual const FloatVector & getFloatVector() const
Returns the stored float vector.
Definition: Option.cpp:104
std::vector< double > FloatVector
Definition of a vector of doubles.
Definition: Option.h:46
std::string next()
Option_IntVector & operator=(const Option_IntVector &s)
Assignment operator.
Definition: Option.cpp:474
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:482
bool myAmWritable
information whether the value may be changed
Definition: Option.h:318
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:87
~Option_Float()
Destructor.
Definition: Option.cpp:301
virtual ~Option()
Definition: Option.cpp:53
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition: Option.cpp:439
std::string myValue
Definition: Option.h:464
Option_String & operator=(const Option_String &s)
Assignment operator.
Definition: Option.cpp:262
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:445
bool set(const std::string &v)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:550
bool set(const std::string &v)
Stores the given value after parsing it into a double.
Definition: Option.cpp:328
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:168
bool myAmSet
information whether the value is set
Definition: Option.h:312
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:144
bool myValue
Definition: Option.h:595
virtual ~Option_FloatVector()
Destructor.
Definition: Option.cpp:532
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:81
void unSet()
marks this option as unset
Definition: Option.cpp:119
Option(bool set=false)
Constructor.
Definition: Option.cpp:44
virtual ~Option_IntVector()
Destructor.
Definition: Option.cpp:470
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
Option_FileName()
Constructor for an option with no default value.
Definition: Option.cpp:412
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:156
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter ...
virtual ~Option_String()
Destructor.
Definition: Option.cpp:252
Option_Float(double value)
Constructor for an option with a default value.
Definition: Option.cpp:295
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:394
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:570
virtual Option & operator=(const Option &s)
Assignment operator.
Definition: Option.cpp:57
std::vector< int > IntVector
Definition of a vector of ints.
Definition: Option.h:41
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition: Option.h:307
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
int getInt() const
Returns the stored integer value.
Definition: Option.cpp:211
Option_String()
Constructor for an option with no default value.
Definition: Option.cpp:240
bool set(const std::string &v)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:488
Option_Integer(int value)
Constructor for an option with a default value.
Definition: Option.cpp:184
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:229
Option_Bool & operator=(const Option_Bool &s)
Assignment operator.
Definition: Option.cpp:366
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition: Option.cpp:138
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
double myValue
Definition: Option.h:532
std::string getString() const
Returns the stored string value.
Definition: Option.cpp:273
~Option_Integer()
Destructor.
Definition: Option.cpp:190
IntVector myValue
Definition: Option.h:716
Option_FloatVector & operator=(const Option_FloatVector &s)
Assignment operator.
Definition: Option.cpp:536
bool set(const std::string &v)
Stores the given value.
Definition: Option.cpp:279
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition: Option.cpp:126
A class representing a single program option.
Definition: Option.h:77
Option_FloatVector()
Constructor for an option with no default value.
Definition: Option.cpp:516
virtual bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:93
bool set(const std::string &v)
Definition: Option.cpp:383
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:339
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:132
An integer-option.
Definition: Option.h:333
const std::string & getDescription() const
Returns the description of what this option does.
Definition: Option.cpp:162
const FloatVector & getFloatVector() const
Returns the stored float vector.
Definition: Option.cpp:544
void resetWritable()
Resets the option to be writeable.
Definition: Option.cpp:150
Option_IntVector()
Constructor for an option with no default value.
Definition: Option.cpp:454
std::string myDescription
The description what this option does.
Definition: Option.h:321
virtual ~Option_FileName()
Destructor.
Definition: Option.cpp:428
bool isSet() const
returns the information whether this options holds a valid value
Definition: Option.cpp:69
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:174
Option_FileName & operator=(const Option_FileName &s)
Assignment operator.
Definition: Option.cpp:432
Option_Float & operator=(const Option_Float &s)
Assignment operator.
Definition: Option.cpp:311
bool myHaveTheDefaultValue
information whether the value is the default value (is then set)
Definition: Option.h:315
FloatVector myValue
Definition: Option.h:786
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:286
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:377
Option_Bool(bool value)
Constructor for an option with a default value.
Definition: Option.cpp:350
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:237
Option_Integer & operator=(const Option_Integer &s)
Assignment operator.
Definition: Option.cpp:200
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition: Option.cpp:403