SUMO - Simulation of Urban MObility
Option.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // A class representing a single program option
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <algorithm>
00034 #include <string>
00035 #include <exception>
00036 #include <sstream>
00037 #include "Option.h"
00038 #include <utils/common/TplConvert.h>
00039 #include <utils/common/UtilExceptions.h>
00040 #include <utils/common/StringTokenizer.h>
00041 #include <utils/common/MsgHandler.h>
00042 
00043 #ifdef CHECK_MEMORY_LEAKS
00044 #include <foreign/nvwa/debug_new.h>
00045 #endif // CHECK_MEMORY_LEAKS
00046 
00047 
00048 // ===========================================================================
00049 // method definitions
00050 // ===========================================================================
00051 /* -------------------------------------------------------------------------
00052  * Option - methods
00053  * ----------------------------------------------------------------------- */
00054 Option::Option(bool set)
00055     : myAmSet(set), myHaveTheDefaultValue(true), myAmWritable(true) {}
00056 
00057 
00058 Option::Option(const Option& s)
00059     : myAmSet(s.myAmSet), myHaveTheDefaultValue(s.myHaveTheDefaultValue),
00060       myAmWritable(s.myAmWritable) {}
00061 
00062 
00063 Option::~Option() {}
00064 
00065 
00066 Option&
00067 Option::operator=(const Option& s) {
00068     if (this == &s) {
00069         return *this;
00070     }
00071     myAmSet = s.myAmSet;
00072     myHaveTheDefaultValue = s.myHaveTheDefaultValue;
00073     myAmWritable = s.myAmWritable;
00074     return *this;
00075 }
00076 
00077 
00078 bool
00079 Option::isSet() const {
00080     return myAmSet;
00081 }
00082 
00083 
00084 SUMOReal
00085 Option::getFloat() const {
00086     throw InvalidArgument("This is not a SUMOReal-option");
00087 }
00088 
00089 
00090 int
00091 Option::getInt() const {
00092     throw InvalidArgument("This is not an int-option");
00093 }
00094 
00095 
00096 std::string
00097 Option::getString() const {
00098     throw InvalidArgument("This is not a string-option");
00099 }
00100 
00101 
00102 bool
00103 Option::getBool() const {
00104     throw InvalidArgument("This is not a bool-option");
00105 }
00106 
00107 
00108 const IntVector&
00109 Option::getIntVector() const {
00110     throw InvalidArgument("This is not an int vector-option");
00111 }
00112 
00113 
00114 bool
00115 Option::markSet() {
00116     bool ret = myAmWritable;
00117     myHaveTheDefaultValue = false;
00118     myAmSet = true;
00119     myAmWritable = false;
00120     return ret;
00121 }
00122 
00123 
00124 bool
00125 Option::isBool() const {
00126     return false;
00127 }
00128 
00129 
00130 bool
00131 Option::isDefault() const {
00132     return myHaveTheDefaultValue;
00133 }
00134 
00135 
00136 bool
00137 Option::isFileName() const {
00138     return false;
00139 }
00140 
00141 
00142 bool
00143 Option::isWriteable() const {
00144     return myAmWritable;
00145 }
00146 
00147 
00148 void
00149 Option::resetWritable() {
00150     myAmWritable = true;
00151 }
00152 
00153 
00154 const std::string&
00155 Option::getDescription() const {
00156     return myDescription;
00157 }
00158 
00159 
00160 void
00161 Option::setDescription(const std::string& desc) {
00162     myDescription = desc;
00163 }
00164 
00165 
00166 const std::string&
00167 Option::getTypeName() const {
00168     return myTypeName;
00169 }
00170 
00171 
00172 
00173 
00174 /* -------------------------------------------------------------------------
00175  * Option_Integer - methods
00176  * ----------------------------------------------------------------------- */
00177 Option_Integer::Option_Integer()
00178     : Option() {
00179     myTypeName = "INT";
00180 }
00181 
00182 
00183 Option_Integer::Option_Integer(int value)
00184     : Option(true), myValue(value) {
00185     myTypeName = "INT";
00186 }
00187 
00188 
00189 Option_Integer::~Option_Integer() {}
00190 
00191 
00192 Option_Integer::Option_Integer(const Option_Integer& s)
00193     : Option(s) {
00194     myValue = s.myValue;
00195 }
00196 
00197 
00198 Option_Integer&
00199 Option_Integer::operator=(const Option_Integer& s) {
00200     if (this == &s) {
00201         return *this;
00202     }
00203     Option::operator=(s);
00204     myValue = s.myValue;
00205     return *this;
00206 }
00207 
00208 
00209 int
00210 Option_Integer::getInt() const {
00211     return myValue;
00212 }
00213 
00214 
00215 bool
00216 Option_Integer::set(const std::string& v) {
00217     try {
00218         myValue = TplConvert<char>::_2int(v.c_str());
00219         return markSet();
00220     } catch (...) {
00221         std::string s = "'" + v + "' is not a valid integer (should be).";
00222         throw InvalidArgument(s);
00223     }
00224 }
00225 
00226 
00227 std::string
00228 Option_Integer::getValueString() const {
00229     std::ostringstream s;
00230     s << myValue;
00231     return s.str();
00232 }
00233 
00234 
00235 
00236 /* -------------------------------------------------------------------------
00237  * Option_String - methods
00238  * ----------------------------------------------------------------------- */
00239 Option_String::Option_String()
00240     : Option() {
00241     myTypeName = "STR";
00242 }
00243 
00244 
00245 Option_String::Option_String(const std::string& value, std::string typeName)
00246     : Option(true), myValue(value) {
00247     myTypeName = typeName;
00248 }
00249 
00250 
00251 Option_String::~Option_String() {}
00252 
00253 
00254 Option_String::Option_String(const Option_String& s)
00255     : Option(s) {
00256     myValue = s.myValue;
00257 }
00258 
00259 
00260 Option_String&
00261 Option_String::operator=(const Option_String& s) {
00262     if (this == &s) {
00263         return *this;
00264     }
00265     Option::operator=(s);
00266     myValue = s.myValue;
00267     return *this;
00268 }
00269 
00270 
00271 std::string
00272 Option_String::getString() const {
00273     return myValue;
00274 }
00275 
00276 
00277 bool
00278 Option_String::set(const std::string& v) {
00279     myValue = v;
00280     return markSet();
00281 }
00282 
00283 
00284 std::string
00285 Option_String::getValueString() const {
00286     return myValue;
00287 }
00288 
00289 
00290 
00291 /* -------------------------------------------------------------------------
00292  * Option_Float - methods
00293  * ----------------------------------------------------------------------- */
00294 Option_Float::Option_Float()
00295     : Option() {
00296     myTypeName = "FLOAT";
00297 }
00298 
00299 
00300 Option_Float::Option_Float(SUMOReal value)
00301     : Option(true), myValue(value) {
00302     myTypeName = "FLOAT";
00303 }
00304 
00305 
00306 Option_Float::~Option_Float() {}
00307 
00308 
00309 Option_Float::Option_Float(const Option_Float& s)
00310     : Option(s) {
00311     myValue = s.myValue;
00312 }
00313 
00314 
00315 Option_Float&
00316 Option_Float::operator=(const Option_Float& s) {
00317     if (this == &s) {
00318         return *this;
00319     }
00320     Option::operator=(s);
00321     myValue = s.myValue;
00322     return *this;
00323 }
00324 
00325 
00326 SUMOReal
00327 Option_Float::getFloat() const {
00328     return myValue;
00329 }
00330 
00331 
00332 bool
00333 Option_Float::set(const std::string& v) {
00334     try {
00335         myValue = TplConvert<char>::_2SUMOReal(v.c_str());
00336         return markSet();
00337     } catch (...) {
00338         std::string s = "'" + v + "' is not a valid float (should be).";
00339         throw InvalidArgument(s);
00340     }
00341 }
00342 
00343 
00344 std::string
00345 Option_Float::getValueString() const {
00346     std::ostringstream s;
00347     s << myValue;
00348     return s.str();
00349 }
00350 
00351 
00352 
00353 /* -------------------------------------------------------------------------
00354  * Option_Bool - methods
00355  * ----------------------------------------------------------------------- */
00356 Option_Bool::Option_Bool()
00357     : Option() {
00358     myTypeName = "BOOL";
00359 }
00360 
00361 
00362 Option_Bool::Option_Bool(bool value)
00363     : Option(true), myValue(value) {
00364     myTypeName = "BOOL";
00365 }
00366 
00367 
00368 Option_Bool::~Option_Bool() {}
00369 
00370 
00371 Option_Bool::Option_Bool(const Option_Bool& s)
00372     : Option(s) {
00373     myValue = s.myValue;
00374 }
00375 
00376 
00377 Option_Bool&
00378 Option_Bool::operator=(const Option_Bool& s) {
00379     if (this == &s) {
00380         return *this;
00381     }
00382     Option::operator=(s);
00383     myValue = s.myValue;
00384     return *this;
00385 }
00386 
00387 
00388 bool
00389 Option_Bool::getBool() const {
00390     return myValue;
00391 }
00392 
00393 
00394 bool
00395 Option_Bool::set(const std::string& v) {
00396     std::string value = v;
00397     std::transform(value.begin(), value.end(), value.begin(), tolower);
00398     if (value == "1" || value == "yes" || value == "true" || value == "on" || value == "x") {
00399         myValue = true;
00400     } else if (value == "0" || value == "no" || value == "false" || value == "off") {
00401         myValue = false;
00402     } else {
00403         throw ProcessError("Invalid boolean value for option.");
00404     }
00405     return markSet();
00406 }
00407 
00408 
00409 std::string
00410 Option_Bool::getValueString() const {
00411     if (myValue) {
00412         return "true";
00413     }
00414     return "false";
00415 }
00416 
00417 
00418 bool
00419 Option_Bool::isBool() const {
00420     return true;
00421 }
00422 
00423 
00424 
00425 /* -------------------------------------------------------------------------
00426  * Option_FileName - methods
00427  * ----------------------------------------------------------------------- */
00428 Option_FileName::Option_FileName()
00429     : Option_String() {
00430     myTypeName = "FILE";
00431 }
00432 
00433 
00434 Option_FileName::Option_FileName(const std::string& value)
00435     : Option_String(value) {
00436     myTypeName = "FILE";
00437 }
00438 
00439 
00440 Option_FileName::Option_FileName(const Option_String& s)
00441     : Option_String(s) {}
00442 
00443 
00444 Option_FileName::~Option_FileName() {}
00445 
00446 
00447 Option_FileName&
00448 Option_FileName::operator=(const Option_FileName& s) {
00449     Option_String::operator=(s);
00450     return (*this);
00451 }
00452 
00453 
00454 bool
00455 Option_FileName::isFileName() const {
00456     return true;
00457 }
00458 
00459 
00460 
00461 /* -------------------------------------------------------------------------
00462  * Option_UIntVector - methods
00463  * ----------------------------------------------------------------------- */
00464 Option_IntVector::Option_IntVector()
00465     : Option() {
00466     myTypeName = "INT[]";
00467 }
00468 
00469 
00470 Option_IntVector::Option_IntVector(const IntVector& value)
00471     : Option(true), myValue(value) {
00472     myTypeName = "INT[]";
00473 }
00474 
00475 
00476 Option_IntVector::Option_IntVector(const Option_IntVector& s)
00477     : Option(s), myValue(s.myValue) {}
00478 
00479 
00480 Option_IntVector::~Option_IntVector() {}
00481 
00482 
00483 Option_IntVector&
00484 Option_IntVector::operator=(const Option_IntVector& s) {
00485     Option::operator=(s);
00486     myValue = s.myValue;
00487     return (*this);
00488 }
00489 
00490 
00491 const IntVector&
00492 Option_IntVector::getIntVector() const {
00493     return myValue;
00494 }
00495 
00496 
00497 bool
00498 Option_IntVector::set(const std::string& v) {
00499     myValue.clear();
00500     try {
00501         if (v.find(';') != std::string::npos) {
00502             WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
00503         }
00504         StringTokenizer st(v, ";,", true);
00505         while (st.hasNext()) {
00506             myValue.push_back(TplConvert<char>::_2int(st.next().c_str()));
00507         }
00508         return markSet();
00509     } catch (EmptyData&) {
00510         throw InvalidArgument("Empty element occured in " + v);
00511     } catch (...) {
00512         throw InvalidArgument("'" + v + "' is not a valid integer vector.");
00513     }
00514 }
00515 
00516 
00517 std::string
00518 Option_IntVector::getValueString() const {
00519     std::ostringstream s;
00520     for (IntVector::const_iterator i = myValue.begin(); i != myValue.end(); i++) {
00521         if (i != myValue.begin()) {
00522             s << ',';
00523         }
00524         s << (*i);
00525     }
00526     return s.str();
00527 }
00528 
00529 
00530 
00531 /****************************************************************************/
00532 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines