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.sourceforge.net/
12 // Copyright (C) 2001-2012 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 <algorithm>
34 #include <string>
35 #include <exception>
36 #include <sstream>
37 #include "Option.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 bool
125 Option::isBool() const {
126  return false;
127 }
128 
129 
130 bool
132  return myHaveTheDefaultValue;
133 }
134 
135 
136 bool
138  return false;
139 }
140 
141 
142 bool
144  return myAmWritable;
145 }
146 
147 
148 void
150  myAmWritable = true;
151 }
152 
153 
154 const std::string&
156  return myDescription;
157 }
158 
159 
160 void
161 Option::setDescription(const std::string& desc) {
162  myDescription = desc;
163 }
164 
165 
166 const std::string&
168  return myTypeName;
169 }
170 
171 
172 
173 
174 /* -------------------------------------------------------------------------
175  * Option_Integer - methods
176  * ----------------------------------------------------------------------- */
178  : Option() {
179  myTypeName = "INT";
180 }
181 
182 
184  : Option(true), myValue(value) {
185  myTypeName = "INT";
186 }
187 
188 
190 
191 
193  : Option(s) {
194  myValue = s.myValue;
195 }
196 
197 
200  if (this == &s) {
201  return *this;
202  }
204  myValue = s.myValue;
205  return *this;
206 }
207 
208 
209 int
211  return myValue;
212 }
213 
214 
215 bool
216 Option_Integer::set(const std::string& v) {
217  try {
218  myValue = TplConvert<char>::_2int(v.c_str());
219  return markSet();
220  } catch (...) {
221  std::string s = "'" + v + "' is not a valid integer (should be).";
222  throw InvalidArgument(s);
223  }
224 }
225 
226 
227 std::string
229  std::ostringstream s;
230  s << myValue;
231  return s.str();
232 }
233 
234 
235 
236 /* -------------------------------------------------------------------------
237  * Option_String - methods
238  * ----------------------------------------------------------------------- */
240  : Option() {
241  myTypeName = "STR";
242 }
243 
244 
245 Option_String::Option_String(const std::string& value, std::string typeName)
246  : Option(true), myValue(value) {
247  myTypeName = typeName;
248 }
249 
250 
252 
253 
255  : Option(s) {
256  myValue = s.myValue;
257 }
258 
259 
262  if (this == &s) {
263  return *this;
264  }
266  myValue = s.myValue;
267  return *this;
268 }
269 
270 
271 std::string
273  return myValue;
274 }
275 
276 
277 bool
278 Option_String::set(const std::string& v) {
279  myValue = v;
280  return markSet();
281 }
282 
283 
284 std::string
286  return myValue;
287 }
288 
289 
290 
291 /* -------------------------------------------------------------------------
292  * Option_Float - methods
293  * ----------------------------------------------------------------------- */
295  : Option() {
296  myTypeName = "FLOAT";
297 }
298 
299 
301  : Option(true), myValue(value) {
302  myTypeName = "FLOAT";
303 }
304 
305 
307 
308 
310  : Option(s) {
311  myValue = s.myValue;
312 }
313 
314 
317  if (this == &s) {
318  return *this;
319  }
321  myValue = s.myValue;
322  return *this;
323 }
324 
325 
326 SUMOReal
328  return myValue;
329 }
330 
331 
332 bool
333 Option_Float::set(const std::string& v) {
334  try {
336  return markSet();
337  } catch (...) {
338  std::string s = "'" + v + "' is not a valid float (should be).";
339  throw InvalidArgument(s);
340  }
341 }
342 
343 
344 std::string
346  std::ostringstream s;
347  s << myValue;
348  return s.str();
349 }
350 
351 
352 
353 /* -------------------------------------------------------------------------
354  * Option_Bool - methods
355  * ----------------------------------------------------------------------- */
357  : Option() {
358  myTypeName = "BOOL";
359 }
360 
361 
363  : Option(true), myValue(value) {
364  myTypeName = "BOOL";
365 }
366 
367 
369 
370 
372  : Option(s) {
373  myValue = s.myValue;
374 }
375 
376 
379  if (this == &s) {
380  return *this;
381  }
383  myValue = s.myValue;
384  return *this;
385 }
386 
387 
388 bool
390  return myValue;
391 }
392 
393 
394 bool
395 Option_Bool::set(const std::string& v) {
396  std::string value = v;
397  std::transform(value.begin(), value.end(), value.begin(), tolower);
398  if (value == "1" || value == "yes" || value == "true" || value == "on" || value == "x") {
399  myValue = true;
400  } else if (value == "0" || value == "no" || value == "false" || value == "off") {
401  myValue = false;
402  } else {
403  throw ProcessError("Invalid boolean value for option.");
404  }
405  return markSet();
406 }
407 
408 
409 std::string
411  if (myValue) {
412  return "true";
413  }
414  return "false";
415 }
416 
417 
418 bool
420  return true;
421 }
422 
423 
424 
425 /* -------------------------------------------------------------------------
426  * Option_FileName - methods
427  * ----------------------------------------------------------------------- */
429  : Option_String() {
430  myTypeName = "FILE";
431 }
432 
433 
434 Option_FileName::Option_FileName(const std::string& value)
435  : Option_String(value) {
436  myTypeName = "FILE";
437 }
438 
439 
441  : Option_String(s) {}
442 
443 
445 
446 
450  return (*this);
451 }
452 
453 
454 bool
456  return true;
457 }
458 
459 
460 
461 /* -------------------------------------------------------------------------
462  * Option_UIntVector - methods
463  * ----------------------------------------------------------------------- */
465  : Option() {
466  myTypeName = "INT[]";
467 }
468 
469 
471  : Option(true), myValue(value) {
472  myTypeName = "INT[]";
473 }
474 
475 
477  : Option(s), myValue(s.myValue) {}
478 
479 
481 
482 
486  myValue = s.myValue;
487  return (*this);
488 }
489 
490 
491 const IntVector&
493  return myValue;
494 }
495 
496 
497 bool
498 Option_IntVector::set(const std::string& v) {
499  myValue.clear();
500  try {
501  if (v.find(';') != std::string::npos) {
502  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
503  }
504  StringTokenizer st(v, ";,", true);
505  while (st.hasNext()) {
506  myValue.push_back(TplConvert<char>::_2int(st.next().c_str()));
507  }
508  return markSet();
509  } catch (EmptyData&) {
510  throw InvalidArgument("Empty element occured in " + v);
511  } catch (...) {
512  throw InvalidArgument("'" + v + "' is not a valid integer vector.");
513  }
514 }
515 
516 
517 std::string
519  std::ostringstream s;
520  for (IntVector::const_iterator i = myValue.begin(); i != myValue.end(); i++) {
521  if (i != myValue.begin()) {
522  s << ',';
523  }
524  s << (*i);
525  }
526  return s.str();
527 }
528 
529 
530 
531 /****************************************************************************/
532