SUMO - Simulation of Urban MObility

Option.cpp

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