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 2001-2010 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 <string>
00031 #include <exception>
00032 #include <sstream>
00033 #include "Option.h"
00034 #include <utils/common/TplConvert.h>
00035 #include <utils/common/UtilExceptions.h>
00036 #include <utils/common/StringTokenizer.h>
00037 #include <utils/common/MsgHandler.h>
00038 
00039 #ifdef CHECK_MEMORY_LEAKS
00040 #include <foreign/nvwa/debug_new.h>
00041 #endif // CHECK_MEMORY_LEAKS
00042 
00043 
00044 // ===========================================================================
00045 // method definitions
00046 // ===========================================================================
00047 /* -------------------------------------------------------------------------
00048  * Option - methods
00049  * ----------------------------------------------------------------------- */
00050 Option::Option(bool set) throw()
00051         : myAmSet(set), myHaveTheDefaultValue(true), myAmWritable(true) {}
00052 
00053 
00054 Option::Option(const Option &s) throw()
00055         : myAmSet(s.myAmSet), myHaveTheDefaultValue(s.myHaveTheDefaultValue),
00056         myAmWritable(s.myAmWritable) {}
00057 
00058 
00059 Option::~Option() throw() {}
00060 
00061 
00062 Option &
00063 Option::operator=(const Option &s) throw() {
00064     if (this==&s) {
00065         return *this;
00066     }
00067     myAmSet = s.myAmSet;
00068     myHaveTheDefaultValue = s.myHaveTheDefaultValue;
00069     myAmWritable = s.myAmWritable;
00070     return *this;
00071 }
00072 
00073 
00074 bool
00075 Option::isSet() const throw() {
00076     return myAmSet;
00077 }
00078 
00079 
00080 SUMOReal
00081 Option::getFloat() const throw(InvalidArgument) {
00082     throw InvalidArgument("This is not a SUMOReal-option");
00083 }
00084 
00085 
00086 int
00087 Option::getInt() const throw(InvalidArgument) {
00088     throw InvalidArgument("This is not an int-option");
00089 }
00090 
00091 
00092 std::string
00093 Option::getString() const throw(InvalidArgument) {
00094     throw InvalidArgument("This is not a string-option");
00095 }
00096 
00097 
00098 bool
00099 Option::getBool() const throw(InvalidArgument) {
00100     throw InvalidArgument("This is not a bool-option");
00101 }
00102 
00103 
00104 const IntVector &
00105 Option::getIntVector() const throw(InvalidArgument) {
00106     throw InvalidArgument("This is not an int vector-option");
00107 }
00108 
00109 
00110 bool
00111 Option::set(const std::string &) throw(InvalidArgument) {
00112     throw InvalidArgument("This is an abstract class.");
00113 }
00114 
00115 
00116 bool
00117 Option::set(bool) throw(InvalidArgument) {
00118     throw InvalidArgument("This is not a bool-option.");
00119 }
00120 
00121 
00122 bool
00123 Option::markSet() throw() {
00124     bool ret = myAmWritable;
00125     myHaveTheDefaultValue = false;
00126     myAmSet = true;
00127     myAmWritable = false;
00128     return ret;
00129 }
00130 
00131 
00132 std::string
00133 Option::getValueString() const throw(InvalidArgument) {
00134     throw InvalidArgument("This is an abstract class.");
00135 }
00136 
00137 
00138 bool
00139 Option::isBool() const throw() {
00140     return false;
00141 }
00142 
00143 
00144 bool
00145 Option::isDefault() const throw() {
00146     return myHaveTheDefaultValue;
00147 }
00148 
00149 
00150 bool
00151 Option::isFileName() const throw() {
00152     return false;
00153 }
00154 
00155 
00156 bool
00157 Option::isWriteable() const throw() {
00158     return myAmWritable;
00159 }
00160 
00161 
00162 const std::string &
00163 Option::getDescription() const throw() {
00164     return myDescription;
00165 }
00166 
00167 
00168 const std::string &
00169 Option::getTypeName() const throw() {
00170     return myTypeName;
00171 }
00172 
00173 
00174 
00175 
00176 /* -------------------------------------------------------------------------
00177  * Option_Integer - methods
00178  * ----------------------------------------------------------------------- */
00179 Option_Integer::Option_Integer() throw()
00180         : Option() {
00181     myTypeName = "INT";
00182 }
00183 
00184 
00185 Option_Integer::Option_Integer(int value) throw()
00186         : Option(true), myValue(value) {
00187     myTypeName = "INT";
00188 }
00189 
00190 
00191 Option_Integer::~Option_Integer() throw() {}
00192 
00193 
00194 Option_Integer::Option_Integer(const Option_Integer &s) throw()
00195         : Option(s) {
00196     myValue = s.myValue;
00197 }
00198 
00199 
00200 Option_Integer &
00201 Option_Integer::operator=(const Option_Integer &s) throw() {
00202     if (this==&s) return *this;
00203     Option::operator=(s);
00204     myValue = s.myValue;
00205     return *this;
00206 }
00207 
00208 
00209 int
00210 Option_Integer::getInt() const throw(InvalidArgument) {
00211     return myValue;
00212 }
00213 
00214 
00215 bool
00216 Option_Integer::set(const std::string &v) throw(InvalidArgument) {
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 throw(InvalidArgument) {
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() throw()
00240         : Option() {
00241     myTypeName = "STR";
00242 }
00243 
00244 
00245 Option_String::Option_String(const std::string &value, std::string typeName) throw()
00246         : Option(true), myValue(value) {
00247     myTypeName = typeName;
00248 }
00249 
00250 
00251 Option_String::~Option_String() throw() {}
00252 
00253 
00254 Option_String::Option_String(const Option_String &s) throw()
00255         : Option(s) {
00256     myValue = s.myValue;
00257 }
00258 
00259 
00260 Option_String &
00261 Option_String::operator=(const Option_String &s) throw() {
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 throw(InvalidArgument) {
00273     return myValue;
00274 }
00275 
00276 
00277 bool
00278 Option_String::set(const std::string &v) throw(InvalidArgument) {
00279     myValue = v;
00280     return markSet();
00281 }
00282 
00283 
00284 std::string
00285 Option_String::getValueString() const throw(InvalidArgument) {
00286     return myValue;
00287 }
00288 
00289 
00290 
00291 /* -------------------------------------------------------------------------
00292  * Option_Float - methods
00293  * ----------------------------------------------------------------------- */
00294 Option_Float::Option_Float() throw()
00295         : Option() {
00296     myTypeName = "FLOAT";
00297 }
00298 
00299 
00300 Option_Float::Option_Float(SUMOReal value) throw()
00301         : Option(true), myValue(value) {
00302     myTypeName = "FLOAT";
00303 }
00304 
00305 
00306 Option_Float::~Option_Float() throw() {}
00307 
00308 
00309 Option_Float::Option_Float(const Option_Float &s) throw()
00310         : Option(s) {
00311     myValue = s.myValue;
00312 }
00313 
00314 
00315 Option_Float &
00316 Option_Float::operator=(const Option_Float &s) throw() {
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 throw(InvalidArgument) {
00328     return myValue;
00329 }
00330 
00331 
00332 bool
00333 Option_Float::set(const std::string &v) throw(InvalidArgument) {
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 throw(InvalidArgument) {
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() throw()
00357         : Option() {
00358     myTypeName = "BOOL";
00359 }
00360 
00361 
00362 Option_Bool::Option_Bool(bool value) throw()
00363         : Option(true), myValue(value) {
00364     myTypeName = "BOOL";
00365 }
00366 
00367 
00368 Option_Bool::~Option_Bool() throw() {}
00369 
00370 
00371 Option_Bool::Option_Bool(const Option_Bool &s) throw()
00372         : Option(s) {
00373     myValue = s.myValue;
00374 }
00375 
00376 
00377 Option_Bool &
00378 Option_Bool::operator=(const Option_Bool &s) throw() {
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 throw(InvalidArgument) {
00390     return myValue;
00391 }
00392 
00393 
00394 bool
00395 Option_Bool::set(bool v) throw(InvalidArgument) {
00396     myValue = v;
00397     return markSet();
00398 }
00399 
00400 
00401 std::string
00402 Option_Bool::getValueString() const throw(InvalidArgument) {
00403     if (myValue) {
00404         return "true";
00405     }
00406     return "false";
00407 }
00408 
00409 
00410 bool
00411 Option_Bool::isBool() const throw() {
00412     return true;
00413 }
00414 
00415 
00416 
00417 /* -------------------------------------------------------------------------
00418  * Option_FileName - methods
00419  * ----------------------------------------------------------------------- */
00420 Option_FileName::Option_FileName() throw()
00421         : Option_String() {
00422     myTypeName = "FILE";
00423 }
00424 
00425 
00426 Option_FileName::Option_FileName(const std::string &value) throw()
00427         : Option_String(value) {
00428     myTypeName = "FILE";
00429 }
00430 
00431 
00432 Option_FileName::Option_FileName(const Option_String &s) throw()
00433         : Option_String(s) {}
00434 
00435 
00436 Option_FileName::~Option_FileName() throw() {}
00437 
00438 
00439 Option_FileName &
00440 Option_FileName::operator=(const Option_FileName &s) throw() {
00441     Option_String::operator=(s);
00442     return (*this);
00443 }
00444 
00445 
00446 bool
00447 Option_FileName::isFileName() const throw() {
00448     return true;
00449 }
00450 
00451 
00452 
00453 /* -------------------------------------------------------------------------
00454  * Option_UIntVector - methods
00455  * ----------------------------------------------------------------------- */
00456 Option_IntVector::Option_IntVector() throw()
00457         : Option() {
00458     myTypeName = "INT[]";
00459 }
00460 
00461 
00462 Option_IntVector::Option_IntVector(const IntVector &value) throw()
00463         : Option(true), myValue(value) {
00464     myTypeName = "INT[]";
00465 }
00466 
00467 
00468 Option_IntVector::Option_IntVector(const Option_IntVector &s) throw()
00469         : Option(s), myValue(s.myValue) {}
00470 
00471 
00472 Option_IntVector::~Option_IntVector() throw() {}
00473 
00474 
00475 Option_IntVector &
00476 Option_IntVector::operator=(const Option_IntVector &s) throw() {
00477     Option::operator=(s);
00478     myValue = s.myValue;
00479     return (*this);
00480 }
00481 
00482 
00483 const IntVector &
00484 Option_IntVector::getIntVector() const throw(InvalidArgument) {
00485     return myValue;
00486 }
00487 
00488 
00489 bool
00490 Option_IntVector::set(const std::string &v) throw(InvalidArgument) {
00491     myValue.clear();
00492     try {
00493         if (v.find(';')!=std::string::npos) {
00494             MsgHandler::getWarningInstance()->inform("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
00495         }
00496         StringTokenizer st(v, ";,", true);
00497         while (st.hasNext()) {
00498             myValue.push_back(TplConvert<char>::_2int(st.next().c_str()));
00499         }
00500         return markSet();
00501     } catch (EmptyData &) {
00502         throw InvalidArgument("Empty element occured in " + v);
00503     } catch (...) {
00504         throw InvalidArgument("'" + v + "' is not a valid integer vector.");
00505     }
00506 }
00507 
00508 
00509 std::string
00510 Option_IntVector::getValueString() const throw(InvalidArgument) {
00511     std::ostringstream s;
00512     for (IntVector::const_iterator i=myValue.begin(); i!=myValue.end(); i++) {
00513         if (i!=myValue.begin()) {
00514             s << ',';
00515         }
00516         s << (*i);
00517     }
00518     return s.str();
00519 }
00520 
00521 
00522 
00523 /****************************************************************************/
00524 

Generated on Wed May 5 00:06:35 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6