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-2015 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 const std::string&
164  return myDescription;
165 }
166 
167 
168 void
169 Option::setDescription(const std::string& desc) {
170  myDescription = desc;
171 }
172 
173 
174 const std::string&
176  return myTypeName;
177 }
178 
179 
180 
181 
182 /* -------------------------------------------------------------------------
183  * Option_Integer - methods
184  * ----------------------------------------------------------------------- */
186  : Option() {
187  myTypeName = "INT";
188 }
189 
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() {
304  myTypeName = "FLOAT";
305 }
306 
307 
309  : Option(true), myValue(value) {
310  myTypeName = "FLOAT";
311 }
312 
313 
315 
316 
318  : Option(s) {
319  myValue = s.myValue;
320 }
321 
322 
325  if (this == &s) {
326  return *this;
327  }
329  myValue = s.myValue;
330  return *this;
331 }
332 
333 
334 SUMOReal
336  return myValue;
337 }
338 
339 
340 bool
341 Option_Float::set(const std::string& v) {
342  try {
343  myValue = TplConvert::_2SUMOReal(v.c_str());
344  return markSet();
345  } catch (...) {
346  throw ProcessError("'" + v + "' is not a valid float.");
347  }
348 }
349 
350 
351 std::string
353  std::ostringstream s;
354  s << myValue;
355  return s.str();
356 }
357 
358 
359 
360 /* -------------------------------------------------------------------------
361  * Option_Bool - methods
362  * ----------------------------------------------------------------------- */
364  : Option() {
365  myTypeName = "BOOL";
366 }
367 
368 
370  : Option(true), myValue(value) {
371  myTypeName = "BOOL";
372 }
373 
374 
376 
377 
379  : Option(s) {
380  myValue = s.myValue;
381 }
382 
383 
386  if (this == &s) {
387  return *this;
388  }
390  myValue = s.myValue;
391  return *this;
392 }
393 
394 
395 bool
397  return myValue;
398 }
399 
400 
401 bool
402 Option_Bool::set(const std::string& v) {
403  try {
404  myValue = TplConvert::_2bool(v.c_str());
405  return markSet();
406  } catch (...) {
407  throw ProcessError("'" + v + "' is not a valid bool.");
408  }
409 }
410 
411 
412 std::string
414  if (myValue) {
415  return "true";
416  }
417  return "false";
418 }
419 
420 
421 bool
423  return true;
424 }
425 
426 
427 
428 /* -------------------------------------------------------------------------
429  * Option_FileName - methods
430  * ----------------------------------------------------------------------- */
432  : Option_String() {
433  myTypeName = "FILE";
434 }
435 
436 
437 Option_FileName::Option_FileName(const std::string& value)
438  : Option_String(value) {
439  myTypeName = "FILE";
440 }
441 
442 
444  : Option_String(s) {}
445 
446 
448 
449 
453  return (*this);
454 }
455 
456 
457 bool
459  return true;
460 }
461 
462 
463 std::string
465  return StringUtils::urlEncode(myValue, " ;%");
466 }
467 
468 
469 
470 /* -------------------------------------------------------------------------
471  * Option_UIntVector - methods
472  * ----------------------------------------------------------------------- */
474  : Option() {
475  myTypeName = "INT[]";
476 }
477 
478 
480  : Option(true), myValue(value) {
481  myTypeName = "INT[]";
482 }
483 
484 
486  : Option(s), myValue(s.myValue) {}
487 
488 
490 
491 
495  myValue = s.myValue;
496  return (*this);
497 }
498 
499 
500 const IntVector&
502  return myValue;
503 }
504 
505 
506 bool
507 Option_IntVector::set(const std::string& v) {
508  myValue.clear();
509  try {
510  if (v.find(';') != std::string::npos) {
511  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
512  }
513  StringTokenizer st(v, ";,", true);
514  while (st.hasNext()) {
515  myValue.push_back(TplConvert::_2int(st.next().c_str()));
516  }
517  return markSet();
518  } catch (EmptyData&) {
519  throw ProcessError("Empty element occured in " + v);
520  } catch (...) {
521  throw ProcessError("'" + v + "' is not a valid integer vector.");
522  }
523 }
524 
525 
526 std::string
528  return joinToString(myValue, ',');
529 }
530 
531 
532 
533 /****************************************************************************/
534 
Option_Float()
Constructor for an option with no default value.
Definition: Option.cpp:302
~Option_Bool()
Destructor.
Definition: Option.cpp:375
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:493
bool myAmWritable
information whether the value may be changed
Definition: Option.h:294
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:352
~Option_Float()
Destructor.
Definition: Option.cpp:314
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:447
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:458
bool set(const std::string &v)
Stores the given value after parsing it into a SUMOReal.
Definition: Option.cpp:341
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:169
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:175
SUMOReal getFloat() const
Returns the stored SUMOReal value.
Definition: Option.cpp:335
bool myAmSet
information whether the value is set
Definition: Option.h:288
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:592
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:489
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:527
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:464
Option_FileName()
Constructor for an option with no default value.
Definition: Option.cpp:431
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:293
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:422
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:163
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition: Option.h:283
Option_Integer()
Constructor for an option with no default value.
Definition: Option.cpp:185
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:507
Option_Bool()
Constructor for an option with no default value.
Definition: Option.cpp:363
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:385
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:713
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
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:402
static int _2int(const E *const data)
Definition: TplConvert.h:114
An integer-option.
Definition: Option.h:309
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:473
std::string myDescription
The description what this option does.
Definition: Option.h:297
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:396
virtual ~Option_FileName()
Destructor.
Definition: Option.cpp:447
Option_FileName & operator=(const Option_FileName &s)
Assignment operator.
Definition: Option.cpp:451
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:413
Option_Float & operator=(const Option_Float &s)
Assignment operator.
Definition: Option.cpp:324
SUMOReal myValue
Definition: Option.h:522
bool myHaveTheDefaultValue
information whether the value is the default value (is then set)
Definition: Option.h:291
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:159
#define SUMOReal
Definition: config.h:214
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:501
Option_Integer & operator=(const Option_Integer &s)
Assignment operator.
Definition: Option.cpp:207