SUMO - Simulation of Urban MObility
Option.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A class representing a single program option
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <exception>
35 #include <sstream>
36 #include "Option.h"
42 #include <utils/common/ToString.h>
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
48 /* -------------------------------------------------------------------------
49  * Option - methods
50  * ----------------------------------------------------------------------- */
51 Option::Option(bool set)
52  : myAmSet(set), myHaveTheDefaultValue(true), myAmWritable(true) {}
53 
54 
58 
59 
61 
62 
63 Option&
65  if (this == &s) {
66  return *this;
67  }
68  myAmSet = s.myAmSet;
71  return *this;
72 }
73 
74 
75 bool
76 Option::isSet() const {
77  return myAmSet;
78 }
79 
80 
81 double
83  throw InvalidArgument("This is not a double-option");
84 }
85 
86 
87 int
88 Option::getInt() const {
89  throw InvalidArgument("This is not an int-option");
90 }
91 
92 
93 std::string
95  throw InvalidArgument("This is not a string-option");
96 }
97 
98 
99 bool
101  throw InvalidArgument("This is not a bool-option");
102 }
103 
104 
105 const IntVector&
107  throw InvalidArgument("This is not an int vector-option");
108 }
109 
110 
111 bool
113  bool ret = myAmWritable;
114  myHaveTheDefaultValue = false;
115  myAmSet = true;
116  myAmWritable = false;
117  return ret;
118 }
119 
120 
121 void
123  myAmSet = false;
124  myAmWritable = true;
125 }
126 
127 
128 bool
129 Option::isBool() const {
130  return false;
131 }
132 
133 
134 bool
136  return myHaveTheDefaultValue;
137 }
138 
139 
140 bool
142  return false;
143 }
144 
145 
146 bool
148  return myAmWritable;
149 }
150 
151 
152 void
154  myAmWritable = true;
155 }
156 
157 
158 void
160  myHaveTheDefaultValue = true;
161 }
162 
163 
164 const std::string&
166  return myDescription;
167 }
168 
169 
170 void
171 Option::setDescription(const std::string& desc) {
172  myDescription = desc;
173 }
174 
175 
176 const std::string&
178  return myTypeName;
179 }
180 
181 
182 
183 
184 /* -------------------------------------------------------------------------
185  * Option_Integer - methods
186  * ----------------------------------------------------------------------- */
188  : Option(true), myValue(value) {
189  myTypeName = "INT";
190 }
191 
192 
194 
195 
197  : Option(s) {
198  myValue = s.myValue;
199 }
200 
201 
204  if (this == &s) {
205  return *this;
206  }
208  myValue = s.myValue;
209  return *this;
210 }
211 
212 
213 int
215  return myValue;
216 }
217 
218 
219 bool
220 Option_Integer::set(const std::string& v) {
221  try {
222  myValue = TplConvert::_2int(v.c_str());
223  return markSet();
224  } catch (...) {
225  std::string s = "'" + v + "' is not a valid integer.";
226  throw ProcessError(s);
227  }
228 }
229 
230 
231 std::string
233  std::ostringstream s;
234  s << myValue;
235  return s.str();
236 }
237 
238 
239 
240 /* -------------------------------------------------------------------------
241  * Option_String - methods
242  * ----------------------------------------------------------------------- */
244  : Option() {
245  myTypeName = "STR";
246 }
247 
248 
249 Option_String::Option_String(const std::string& value, std::string typeName)
250  : Option(true), myValue(value) {
251  myTypeName = typeName;
252 }
253 
254 
256 
257 
259  : Option(s) {
260  myValue = s.myValue;
261 }
262 
263 
266  if (this == &s) {
267  return *this;
268  }
270  myValue = s.myValue;
271  return *this;
272 }
273 
274 
275 std::string
277  return myValue;
278 }
279 
280 
281 bool
282 Option_String::set(const std::string& v) {
283  myValue = v;
284  return markSet();
285 }
286 
287 
288 std::string
290  return myValue;
291 }
292 
293 
294 
295 /* -------------------------------------------------------------------------
296  * Option_Float - methods
297  * ----------------------------------------------------------------------- */
299  : Option(true), myValue(value) {
300  myTypeName = "FLOAT";
301 }
302 
303 
305 
306 
308  : Option(s) {
309  myValue = s.myValue;
310 }
311 
312 
315  if (this == &s) {
316  return *this;
317  }
319  myValue = s.myValue;
320  return *this;
321 }
322 
323 
324 double
326  return myValue;
327 }
328 
329 
330 bool
331 Option_Float::set(const std::string& v) {
332  try {
333  myValue = TplConvert::_2double(v.c_str());
334  return markSet();
335  } catch (...) {
336  throw ProcessError("'" + v + "' is not a valid float.");
337  }
338 }
339 
340 
341 std::string
343  std::ostringstream s;
344  s << myValue;
345  return s.str();
346 }
347 
348 
349 
350 /* -------------------------------------------------------------------------
351  * Option_Bool - methods
352  * ----------------------------------------------------------------------- */
354  : Option(true), myValue(value) {
355  myTypeName = "BOOL";
356 }
357 
358 
360 
361 
363  : Option(s) {
364  myValue = s.myValue;
365 }
366 
367 
370  if (this == &s) {
371  return *this;
372  }
374  myValue = s.myValue;
375  return *this;
376 }
377 
378 
379 bool
381  return myValue;
382 }
383 
384 
385 bool
386 Option_Bool::set(const std::string& v) {
387  try {
388  myValue = TplConvert::_2bool(v.c_str());
389  return markSet();
390  } catch (...) {
391  throw ProcessError("'" + v + "' is not a valid bool.");
392  }
393 }
394 
395 
396 std::string
398  if (myValue) {
399  return "true";
400  }
401  return "false";
402 }
403 
404 
405 bool
407  return true;
408 }
409 
410 
411 
412 /* -------------------------------------------------------------------------
413  * Option_FileName - methods
414  * ----------------------------------------------------------------------- */
416  : Option_String() {
417  myTypeName = "FILE";
418 }
419 
420 
421 Option_FileName::Option_FileName(const std::string& value)
422  : Option_String(value) {
423  myTypeName = "FILE";
424 }
425 
426 
428  : Option_String(s) {}
429 
430 
432 
433 
437  return (*this);
438 }
439 
440 
441 bool
443  return true;
444 }
445 
446 
447 std::string
449  return StringUtils::urlEncode(myValue, " ;%");
450 }
451 
452 
453 
454 /* -------------------------------------------------------------------------
455  * Option_UIntVector - methods
456  * ----------------------------------------------------------------------- */
458  : Option() {
459  myTypeName = "INT[]";
460 }
461 
462 
464  : Option(true), myValue(value) {
465  myTypeName = "INT[]";
466 }
467 
468 
470  : Option(s), myValue(s.myValue) {}
471 
472 
474 
475 
479  myValue = s.myValue;
480  return (*this);
481 }
482 
483 
484 const IntVector&
486  return myValue;
487 }
488 
489 
490 bool
491 Option_IntVector::set(const std::string& v) {
492  myValue.clear();
493  try {
494  if (v.find(';') != std::string::npos) {
495  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
496  }
497  StringTokenizer st(v, ";,", true);
498  while (st.hasNext()) {
499  myValue.push_back(TplConvert::_2int(st.next().c_str()));
500  }
501  return markSet();
502  } catch (EmptyData&) {
503  throw ProcessError("Empty element occured in " + v);
504  } catch (...) {
505  throw ProcessError("'" + v + "' is not a valid integer vector.");
506  }
507 }
508 
509 
510 std::string
512  return joinToString(myValue, ',');
513 }
514 
515 
516 
517 /****************************************************************************/
518 
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:511
virtual double getFloat() const
Returns the stored double value.
Definition: Option.cpp:82
~Option_Bool()
Destructor.
Definition: Option.cpp:359
double getFloat() const
Returns the stored double value.
Definition: Option.cpp:325
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:106
bool set(const std::string &v)
Stores the given value after parsing it into an integer.
Definition: Option.cpp:220
bool markSet()
Marks the information as set.
Definition: Option.cpp:112
std::string next()
Option_IntVector & operator=(const Option_IntVector &s)
Assignment operator.
Definition: Option.cpp:477
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:485
bool myAmWritable
information whether the value may be changed
Definition: Option.h:298
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:371
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:94
~Option_Float()
Destructor.
Definition: Option.cpp:304
virtual ~Option()
Definition: Option.cpp:60
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition: Option.cpp:442
std::string myValue
Definition: Option.h:444
Option_String & operator=(const Option_String &s)
Assignment operator.
Definition: Option.cpp:265
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:448
bool set(const std::string &v)
Stores the given value after parsing it into a double.
Definition: Option.cpp:331
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:171
bool myAmSet
information whether the value is set
Definition: Option.h:292
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:147
bool myValue
Definition: Option.h:575
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:88
void unSet()
marks this option as unset
Definition: Option.cpp:122
Option(bool set=false)
Constructor.
Definition: Option.cpp:51
virtual ~Option_IntVector()
Destructor.
Definition: Option.cpp:473
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
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:415
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:159
virtual ~Option_String()
Destructor.
Definition: Option.cpp:255
Option_Float(double value)
Constructor for an option with a default value.
Definition: Option.cpp:298
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:397
virtual Option & operator=(const Option &s)
Assignment operator.
Definition: Option.cpp:64
std::vector< int > IntVector
Definition of a vector of ints.
Definition: Option.h:48
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition: Option.h:287
int getInt() const
Returns the stored integer value.
Definition: Option.cpp:214
Option_String()
Constructor for an option with no default value.
Definition: Option.cpp:243
bool set(const std::string &v)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:491
Option_Integer(int value)
Constructor for an option with a default value.
Definition: Option.cpp:187
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:232
Option_Bool & operator=(const Option_Bool &s)
Assignment operator.
Definition: Option.cpp:369
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition: Option.cpp:141
double myValue
Definition: Option.h:512
std::string getString() const
Returns the stored string value.
Definition: Option.cpp:276
~Option_Integer()
Destructor.
Definition: Option.cpp:193
IntVector myValue
Definition: Option.h:696
bool set(const std::string &v)
Stores the given value.
Definition: Option.cpp:282
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition: Option.cpp:129
A class representing a single program option.
Definition: Option.h:79
virtual bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:100
bool set(const std::string &v)
Definition: Option.cpp:386
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:342
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:149
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:135
An integer-option.
Definition: Option.h:313
const std::string & getDescription() const
Returns the description of what this option does.
Definition: Option.cpp:165
void resetWritable()
Resets the option to be writeable.
Definition: Option.cpp:153
Option_IntVector()
Constructor for an option with no default value.
Definition: Option.cpp:457
std::string myDescription
The description what this option does.
Definition: Option.h:301
virtual ~Option_FileName()
Destructor.
Definition: Option.cpp:431
bool isSet() const
returns the information whether this options holds a valid value
Definition: Option.cpp:76
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:177
Option_FileName & operator=(const Option_FileName &s)
Assignment operator.
Definition: Option.cpp:435
Option_Float & operator=(const Option_Float &s)
Assignment operator.
Definition: Option.cpp:314
bool myHaveTheDefaultValue
information whether the value is the default value (is then set)
Definition: Option.h:295
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:289
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:380
Option_Bool(bool value)
Constructor for an option with a default value.
Definition: Option.cpp:353
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:228
Option_Integer & operator=(const Option_Integer &s)
Assignment operator.
Definition: Option.cpp:203
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition: Option.cpp:406