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