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