SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
60  : myAmSet(s.myAmSet), myHaveTheDefaultValue(s.myHaveTheDefaultValue),
61  myAmWritable(s.myAmWritable) {}
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(true), myValue(value) {
193  myTypeName = "INT";
194 }
195 
196 
198 
199 
201  : Option(s) {
202  myValue = s.myValue;
203 }
204 
205 
208  if (this == &s) {
209  return *this;
210  }
212  myValue = s.myValue;
213  return *this;
214 }
215 
216 
217 int
219  return myValue;
220 }
221 
222 
223 bool
224 Option_Integer::set(const std::string& v) {
225  try {
226  myValue = TplConvert::_2int(v.c_str());
227  return markSet();
228  } catch (...) {
229  std::string s = "'" + v + "' is not a valid integer.";
230  throw ProcessError(s);
231  }
232 }
233 
234 
235 std::string
237  std::ostringstream s;
238  s << myValue;
239  return s.str();
240 }
241 
242 
243 
244 /* -------------------------------------------------------------------------
245  * Option_String - methods
246  * ----------------------------------------------------------------------- */
248  : Option() {
249  myTypeName = "STR";
250 }
251 
252 
253 Option_String::Option_String(const std::string& value, std::string typeName)
254  : Option(true), myValue(value) {
255  myTypeName = typeName;
256 }
257 
258 
260 
261 
263  : Option(s) {
264  myValue = s.myValue;
265 }
266 
267 
270  if (this == &s) {
271  return *this;
272  }
274  myValue = s.myValue;
275  return *this;
276 }
277 
278 
279 std::string
281  return myValue;
282 }
283 
284 
285 bool
286 Option_String::set(const std::string& v) {
287  myValue = v;
288  return markSet();
289 }
290 
291 
292 std::string
294  return myValue;
295 }
296 
297 
298 
299 /* -------------------------------------------------------------------------
300  * Option_Float - methods
301  * ----------------------------------------------------------------------- */
303  : Option(true), myValue(value) {
304  myTypeName = "FLOAT";
305 }
306 
307 
309 
310 
312  : Option(s) {
313  myValue = s.myValue;
314 }
315 
316 
319  if (this == &s) {
320  return *this;
321  }
323  myValue = s.myValue;
324  return *this;
325 }
326 
327 
328 SUMOReal
330  return myValue;
331 }
332 
333 
334 bool
335 Option_Float::set(const std::string& v) {
336  try {
337  myValue = TplConvert::_2SUMOReal(v.c_str());
338  return markSet();
339  } catch (...) {
340  throw ProcessError("'" + v + "' is not a valid float.");
341  }
342 }
343 
344 
345 std::string
347  std::ostringstream s;
348  s << myValue;
349  return s.str();
350 }
351 
352 
353 
354 /* -------------------------------------------------------------------------
355  * Option_Bool - methods
356  * ----------------------------------------------------------------------- */
358  : Option(true), myValue(value) {
359  myTypeName = "BOOL";
360 }
361 
362 
364 
365 
367  : Option(s) {
368  myValue = s.myValue;
369 }
370 
371 
374  if (this == &s) {
375  return *this;
376  }
378  myValue = s.myValue;
379  return *this;
380 }
381 
382 
383 bool
385  return myValue;
386 }
387 
388 
389 bool
390 Option_Bool::set(const std::string& v) {
391  try {
392  myValue = TplConvert::_2bool(v.c_str());
393  return markSet();
394  } catch (...) {
395  throw ProcessError("'" + v + "' is not a valid bool.");
396  }
397 }
398 
399 
400 std::string
402  if (myValue) {
403  return "true";
404  }
405  return "false";
406 }
407 
408 
409 bool
411  return true;
412 }
413 
414 
415 
416 /* -------------------------------------------------------------------------
417  * Option_FileName - methods
418  * ----------------------------------------------------------------------- */
420  : Option_String() {
421  myTypeName = "FILE";
422 }
423 
424 
425 Option_FileName::Option_FileName(const std::string& value)
426  : Option_String(value) {
427  myTypeName = "FILE";
428 }
429 
430 
432  : Option_String(s) {}
433 
434 
436 
437 
441  return (*this);
442 }
443 
444 
445 bool
447  return true;
448 }
449 
450 
451 std::string
453  return StringUtils::urlEncode(myValue, " ;%");
454 }
455 
456 
457 
458 /* -------------------------------------------------------------------------
459  * Option_UIntVector - methods
460  * ----------------------------------------------------------------------- */
462  : Option() {
463  myTypeName = "INT[]";
464 }
465 
466 
468  : Option(true), myValue(value) {
469  myTypeName = "INT[]";
470 }
471 
472 
474  : Option(s), myValue(s.myValue) {}
475 
476 
478 
479 
483  myValue = s.myValue;
484  return (*this);
485 }
486 
487 
488 const IntVector&
490  return myValue;
491 }
492 
493 
494 bool
495 Option_IntVector::set(const std::string& v) {
496  myValue.clear();
497  try {
498  if (v.find(';') != std::string::npos) {
499  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
500  }
501  StringTokenizer st(v, ";,", true);
502  while (st.hasNext()) {
503  myValue.push_back(TplConvert::_2int(st.next().c_str()));
504  }
505  return markSet();
506  } catch (EmptyData&) {
507  throw ProcessError("Empty element occured in " + v);
508  } catch (...) {
509  throw ProcessError("'" + v + "' is not a valid integer vector.");
510  }
511 }
512 
513 
514 std::string
516  return joinToString(myValue, ',');
517 }
518 
519 
520 
521 /****************************************************************************/
522 
~Option_Bool()
Destructor.
Definition: Option.cpp:363
bool set(const std::string &v)
Stores the given value after parsing it into an integer.
Definition: Option.cpp:224
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:481
bool myAmWritable
information whether the value may be changed
Definition: Option.h:298
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:290
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:364
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:346
~Option_Float()
Destructor.
Definition: Option.cpp:308
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:444
Option_String & operator=(const Option_String &s)
Assignment operator.
Definition: Option.cpp:269
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:446
bool set(const std::string &v)
Stores the given value after parsing it into a SUMOReal.
Definition: Option.cpp:335
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:329
bool myAmSet
information whether the value is set
Definition: Option.h:292
std::string getString() const
Returns the stored string value.
Definition: Option.cpp:280
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:98
bool myValue
Definition: Option.h:575
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:477
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:515
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:452
Option_FileName()
Constructor for an option with no default value.
Definition: Option.cpp:419
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:293
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:163
virtual ~Option_String()
Destructor.
Definition: Option.cpp:259
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition: Option.cpp:410
virtual Option & operator=(const Option &s)
Assignment operator.
Definition: Option.cpp:68
std::vector< int > IntVector
Definition of a vector of 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_String()
Constructor for an option with no default value.
Definition: Option.cpp:247
int getInt() const
Returns the stored integer value.
Definition: Option.cpp:218
bool set(const std::string &v)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:495
Option_Integer(int value)
Constructor for an option with a default value.
Definition: Option.cpp:191
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:373
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:236
~Option_Integer()
Destructor.
Definition: Option.cpp:197
IntVector myValue
Definition: Option.h:696
bool set(const std::string &v)
Stores the given value.
Definition: Option.cpp:286
A class representing a single program option.
Definition: Option.h:79
Option_Float(SUMOReal value)
Constructor for an option with a default value.
Definition: Option.cpp:302
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:390
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:149
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:461
std::string myDescription
The description what this option does.
Definition: Option.h:301
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:384
virtual ~Option_FileName()
Destructor.
Definition: Option.cpp:435
Option_FileName & operator=(const Option_FileName &s)
Assignment operator.
Definition: Option.cpp:439
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:401
Option_Float & operator=(const Option_Float &s)
Assignment operator.
Definition: Option.cpp:318
SUMOReal myValue
Definition: Option.h:512
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:188
Option_Bool(bool value)
Constructor for an option with a default value.
Definition: Option.cpp:357
#define SUMOReal
Definition: config.h:214
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:489
Option_Integer & operator=(const Option_Integer &s)
Assignment operator.
Definition: Option.cpp:207