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