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-2016 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 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 /* -------------------------------------------------------------------------
53  * Option - methods
54  * ----------------------------------------------------------------------- */
55 Option::Option(bool set)
56  : myAmSet(set), myHaveTheDefaultValue(true), myAmWritable(true) {}
57 
58 
62 
63 
65 
66 
67 Option&
69  if (this == &s) {
70  return *this;
71  }
72  myAmSet = s.myAmSet;
75  return *this;
76 }
77 
78 
79 bool
80 Option::isSet() const {
81  return myAmSet;
82 }
83 
84 
87  throw InvalidArgument("This is not a SUMOReal-option");
88 }
89 
90 
91 int
92 Option::getInt() const {
93  throw InvalidArgument("This is not an int-option");
94 }
95 
96 
97 std::string
99  throw InvalidArgument("This is not a string-option");
100 }
101 
102 
103 bool
105  throw InvalidArgument("This is not a bool-option");
106 }
107 
108 
109 const IntVector&
111  throw InvalidArgument("This is not an int vector-option");
112 }
113 
114 
115 bool
117  bool ret = myAmWritable;
118  myHaveTheDefaultValue = false;
119  myAmSet = true;
120  myAmWritable = false;
121  return ret;
122 }
123 
124 
125 void
127  myAmSet = false;
128  myAmWritable = true;
129 }
130 
131 
132 bool
133 Option::isBool() const {
134  return false;
135 }
136 
137 
138 bool
140  return myHaveTheDefaultValue;
141 }
142 
143 
144 bool
146  return false;
147 }
148 
149 
150 bool
152  return myAmWritable;
153 }
154 
155 
156 void
158  myAmWritable = true;
159 }
160 
161 
162 void
164  myHaveTheDefaultValue = true;
165 }
166 
167 
168 const std::string&
170  return myDescription;
171 }
172 
173 
174 void
175 Option::setDescription(const std::string& desc) {
176  myDescription = desc;
177 }
178 
179 
180 const std::string&
182  return myTypeName;
183 }
184 
185 
186 
187 
188 /* -------------------------------------------------------------------------
189  * Option_Integer - methods
190  * ----------------------------------------------------------------------- */
192  : Option() {
193  myTypeName = "INT";
194 }
195 
196 
198  : Option(true), myValue(value) {
199  myTypeName = "INT";
200 }
201 
202 
204 
205 
207  : Option(s) {
208  myValue = s.myValue;
209 }
210 
211 
214  if (this == &s) {
215  return *this;
216  }
218  myValue = s.myValue;
219  return *this;
220 }
221 
222 
223 int
225  return myValue;
226 }
227 
228 
229 bool
230 Option_Integer::set(const std::string& v) {
231  try {
232  myValue = TplConvert::_2int(v.c_str());
233  return markSet();
234  } catch (...) {
235  std::string s = "'" + v + "' is not a valid integer.";
236  throw ProcessError(s);
237  }
238 }
239 
240 
241 std::string
243  std::ostringstream s;
244  s << myValue;
245  return s.str();
246 }
247 
248 
249 
250 /* -------------------------------------------------------------------------
251  * Option_String - methods
252  * ----------------------------------------------------------------------- */
254  : Option() {
255  myTypeName = "STR";
256 }
257 
258 
259 Option_String::Option_String(const std::string& value, std::string typeName)
260  : Option(true), myValue(value) {
261  myTypeName = typeName;
262 }
263 
264 
266 
267 
269  : Option(s) {
270  myValue = s.myValue;
271 }
272 
273 
276  if (this == &s) {
277  return *this;
278  }
280  myValue = s.myValue;
281  return *this;
282 }
283 
284 
285 std::string
287  return myValue;
288 }
289 
290 
291 bool
292 Option_String::set(const std::string& v) {
293  myValue = v;
294  return markSet();
295 }
296 
297 
298 std::string
300  return myValue;
301 }
302 
303 
304 
305 /* -------------------------------------------------------------------------
306  * Option_Float - methods
307  * ----------------------------------------------------------------------- */
309  : Option() {
310  myTypeName = "FLOAT";
311 }
312 
313 
315  : Option(true), myValue(value) {
316  myTypeName = "FLOAT";
317 }
318 
319 
321 
322 
324  : Option(s) {
325  myValue = s.myValue;
326 }
327 
328 
331  if (this == &s) {
332  return *this;
333  }
335  myValue = s.myValue;
336  return *this;
337 }
338 
339 
340 SUMOReal
342  return myValue;
343 }
344 
345 
346 bool
347 Option_Float::set(const std::string& v) {
348  try {
349  myValue = TplConvert::_2SUMOReal(v.c_str());
350  return markSet();
351  } catch (...) {
352  throw ProcessError("'" + v + "' is not a valid float.");
353  }
354 }
355 
356 
357 std::string
359  std::ostringstream s;
360  s << myValue;
361  return s.str();
362 }
363 
364 
365 
366 /* -------------------------------------------------------------------------
367  * Option_Bool - methods
368  * ----------------------------------------------------------------------- */
370  : Option() {
371  myTypeName = "BOOL";
372 }
373 
374 
376  : Option(true), myValue(value) {
377  myTypeName = "BOOL";
378 }
379 
380 
382 
383 
385  : Option(s) {
386  myValue = s.myValue;
387 }
388 
389 
392  if (this == &s) {
393  return *this;
394  }
396  myValue = s.myValue;
397  return *this;
398 }
399 
400 
401 bool
403  return myValue;
404 }
405 
406 
407 bool
408 Option_Bool::set(const std::string& v) {
409  try {
410  myValue = TplConvert::_2bool(v.c_str());
411  return markSet();
412  } catch (...) {
413  throw ProcessError("'" + v + "' is not a valid bool.");
414  }
415 }
416 
417 
418 std::string
420  if (myValue) {
421  return "true";
422  }
423  return "false";
424 }
425 
426 
427 bool
429  return true;
430 }
431 
432 
433 
434 /* -------------------------------------------------------------------------
435  * Option_FileName - methods
436  * ----------------------------------------------------------------------- */
438  : Option_String() {
439  myTypeName = "FILE";
440 }
441 
442 
443 Option_FileName::Option_FileName(const std::string& value)
444  : Option_String(value) {
445  myTypeName = "FILE";
446 }
447 
448 
450  : Option_String(s) {}
451 
452 
454 
455 
459  return (*this);
460 }
461 
462 
463 bool
465  return true;
466 }
467 
468 
469 std::string
471  return StringUtils::urlEncode(myValue, " ;%");
472 }
473 
474 
475 
476 /* -------------------------------------------------------------------------
477  * Option_UIntVector - methods
478  * ----------------------------------------------------------------------- */
480  : Option() {
481  myTypeName = "INT[]";
482 }
483 
484 
486  : Option(true), myValue(value) {
487  myTypeName = "INT[]";
488 }
489 
490 
492  : Option(s), myValue(s.myValue) {}
493 
494 
496 
497 
501  myValue = s.myValue;
502  return (*this);
503 }
504 
505 
506 const IntVector&
508  return myValue;
509 }
510 
511 
512 bool
513 Option_IntVector::set(const std::string& v) {
514  myValue.clear();
515  try {
516  if (v.find(';') != std::string::npos) {
517  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
518  }
519  StringTokenizer st(v, ";,", true);
520  while (st.hasNext()) {
521  myValue.push_back(TplConvert::_2int(st.next().c_str()));
522  }
523  return markSet();
524  } catch (EmptyData&) {
525  throw ProcessError("Empty element occured in " + v);
526  } catch (...) {
527  throw ProcessError("'" + v + "' is not a valid integer vector.");
528  }
529 }
530 
531 
532 std::string
534  return joinToString(myValue, ',');
535 }
536 
537 
538 
539 /****************************************************************************/
540 
Option_Float()
Constructor for an option with no default value.
Definition: Option.cpp:308
~Option_Bool()
Destructor.
Definition: Option.cpp:381
bool set(const std::string &v)
Stores the given value after parsing it into an integer.
Definition: Option.cpp:230
bool markSet()
Marks the information as set.
Definition: Option.cpp:116
bool isSet() const
returns the information whether this options holds a valid value
Definition: Option.cpp:80
std::string next()
Option_IntVector & operator=(const Option_IntVector &s)
Assignment operator.
Definition: Option.cpp:499
bool myAmWritable
information whether the value may be changed
Definition: Option.h:298
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:242
static bool _2bool(const E *const data)
Definition: TplConvert.h:311
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition: Option.cpp:133
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:358
~Option_Float()
Destructor.
Definition: Option.cpp:320
virtual ~Option()
Definition: Option.cpp:64
virtual bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:104
std::string myValue
Definition: Option.h:451
Option_String & operator=(const Option_String &s)
Assignment operator.
Definition: Option.cpp:275
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:110
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition: Option.cpp:464
bool set(const std::string &v)
Stores the given value after parsing it into a SUMOReal.
Definition: Option.cpp:347
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:175
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:181
SUMOReal getFloat() const
Returns the stored SUMOReal value.
Definition: Option.cpp:341
bool myAmSet
information whether the value is set
Definition: Option.h:292
std::string getString() const
Returns the stored string value.
Definition: Option.cpp:286
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:98
bool myValue
Definition: Option.h:596
void unSet()
marks this option as unset
Definition: Option.cpp:126
Option(bool set=false)
Constructor.
Definition: Option.cpp:55
virtual ~Option_IntVector()
Destructor.
Definition: Option.cpp:495
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:533
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:470
Option_FileName()
Constructor for an option with no default value.
Definition: Option.cpp:437
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:299
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:163
virtual ~Option_String()
Destructor.
Definition: Option.cpp:265
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition: Option.cpp:428
virtual Option & operator=(const Option &s)
Assignment operator.
Definition: Option.cpp:68
std::vector< int > IntVector
Definition of a vector of unsigned ints.
Definition: Option.h:48
const std::string & getDescription() const
Returns the description of what this option does.
Definition: Option.cpp:169
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition: Option.h:287
Option_Integer()
Constructor for an option with no default value.
Definition: Option.cpp:191
Option_String()
Constructor for an option with no default value.
Definition: Option.cpp:253
int getInt() const
Returns the stored integer value.
Definition: Option.cpp:224
bool set(const std::string &v)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:513
Option_Bool()
Constructor for an option with no default value.
Definition: Option.cpp:369
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:151
Option_Bool & operator=(const Option_Bool &s)
Assignment operator.
Definition: Option.cpp:391
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:242
~Option_Integer()
Destructor.
Definition: Option.cpp:203
IntVector myValue
Definition: Option.h:717
bool set(const std::string &v)
Stores the given value.
Definition: Option.cpp:292
A class representing a single program option.
Definition: Option.h:79
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:139
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:92
virtual SUMOReal getFloat() const
Returns the stored SUMOReal value.
Definition: Option.cpp:86
bool set(const std::string &v)
Definition: Option.cpp:408
static int _2int(const E *const data)
Definition: TplConvert.h:114
An integer-option.
Definition: Option.h:313
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition: Option.cpp:145
void resetWritable()
Resets the option to be writeable.
Definition: Option.cpp:157
Option_IntVector()
Constructor for an option with no default value.
Definition: Option.cpp:479
std::string myDescription
The description what this option does.
Definition: Option.h:301
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:402
virtual ~Option_FileName()
Destructor.
Definition: Option.cpp:453
Option_FileName & operator=(const Option_FileName &s)
Assignment operator.
Definition: Option.cpp:457
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:419
Option_Float & operator=(const Option_Float &s)
Assignment operator.
Definition: Option.cpp:330
SUMOReal myValue
Definition: Option.h:526
bool myHaveTheDefaultValue
information whether the value is the default value (is then set)
Definition: Option.h:295
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:160
#define SUMOReal
Definition: config.h:213
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:507
Option_Integer & operator=(const Option_Integer &s)
Assignment operator.
Definition: Option.cpp:213