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-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