Eclipse SUMO - Simulation of Urban MObility
Option.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Classes representing a single program option (with different types)
17 /****************************************************************************/
18 #ifndef Option_h
19 #define Option_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <vector>
29 #include <exception>
31 
32 
33 // ===========================================================================
34 // class definitions
35 // ===========================================================================
40 typedef std::vector<int> IntVector;
45 typedef std::vector<std::string> StringVector;
46 
47 /* -------------------------------------------------------------------------
48  * Option
49  * ----------------------------------------------------------------------- */
75 class Option {
76 public:
78  virtual ~Option();
79 
80 
84  bool isSet() const;
85 
86 
89  void unSet();
90 
91 
100  virtual double getFloat() const;
101 
102 
111  virtual int getInt() const;
112 
113 
123  virtual std::string getString() const;
124 
125 
134  virtual bool getBool() const;
135 
136 
145  virtual const IntVector& getIntVector() const;
146 
155  virtual const StringVector& getStringVector() const;
156 
172  virtual bool set(const std::string& v) = 0;
173 
174 
181  virtual std::string getValueString() const = 0;
182 
183 
190  virtual bool isBool() const;
191 
192 
197  virtual bool isDefault() const;
198 
199 
206  virtual bool isFileName() const;
207 
208 
216  bool isWriteable() const;
217 
218 
224  void resetWritable();
225 
226 
232  void resetDefault();
233 
234 
241  const std::string& getDescription() const;
242 
243 
250  void setDescription(const std::string& desc);
251 
252 
259  virtual const std::string& getTypeName() const;
260 
261 
266  template<class OptionType, class ValueType>
267  static OptionType* makeUnsetWithDefault(ValueType def) {
268  OptionType* o = new OptionType(def);
269  o->unSet();
270  return o;
271  }
272 
273 
274 protected:
281  bool markSet();
282 
283 
284 protected:
292  Option(bool set = false);
293 
294 
296  Option(const Option& s);
297 
298 
300  virtual Option& operator=(const Option& s);
301 
302 
303 protected:
305  std::string myTypeName;
306 
307 
308 private:
310  bool myAmSet;
311 
314 
317 
319  std::string myDescription;
320 
321 };
322 
323 
324 /* -------------------------------------------------------------------------
325  * Option_Integer
326  * ----------------------------------------------------------------------- */
331 class Option_Integer : public Option {
332 public:
339  Option_Integer(int value);
340 
341 
343  Option_Integer(const Option_Integer& s);
344 
345 
347  ~Option_Integer();
348 
349 
352 
353 
358  int getInt() const;
359 
360 
376  bool set(const std::string& v);
377 
378 
386  std::string getValueString() const;
387 
388 
389 private:
391  int myValue;
392 
393 };
394 
395 
396 /* -------------------------------------------------------------------------
397  * Option_String
398  * ----------------------------------------------------------------------- */
399 class Option_String : public Option {
400 public:
405  Option_String();
406 
407 
414  Option_String(const std::string& value, std::string typeName = "STR");
415 
416 
418  Option_String(const Option_String& s);
419 
420 
422  virtual ~Option_String();
423 
424 
427 
428 
433  std::string getString() const;
434 
435 
447  bool set(const std::string& v);
448 
449 
457  std::string getValueString() const;
458 
459 
460 protected:
462  std::string myValue;
463 
464 };
465 
466 
467 /* -------------------------------------------------------------------------
468  * Option_Float
469  * ----------------------------------------------------------------------- */
470 class Option_Float : public Option {
471 public:
478  Option_Float(double value);
479 
480 
482  Option_Float(const Option_Float& s);
483 
484 
486  ~Option_Float();
487 
488 
491 
492 
497  double getFloat() const;
498 
499 
515  bool set(const std::string& v);
516 
517 
525  std::string getValueString() const;
526 
527 
528 private:
530  double myValue;
531 
532 };
533 
534 
535 /* -------------------------------------------------------------------------
536  * Option_Bool
537  * ----------------------------------------------------------------------- */
538 class Option_Bool : public Option {
539 public:
546  Option_Bool(bool value);
547 
548 
550  Option_Bool(const Option_Bool& s);
551 
552 
554  ~Option_Bool();
555 
556 
558  Option_Bool& operator=(const Option_Bool& s);
559 
560 
565  bool getBool() const;
566 
568  virtual bool set(const std::string& v);
569 
570 
578  virtual std::string getValueString() const;
579 
580 
588  bool isBool() const;
589 
590 
591 protected:
593  bool myValue;
594 
595 };
596 
597 
598 
599 /* -------------------------------------------------------------------------
600  * Option_BoolExtended
601  * ----------------------------------------------------------------------- */
603 public:
611  Option_BoolExtended(bool value);
612 
613 
616 
617 
620 
621 
624 
625 
627  bool set(const std::string& v);
628 
629 
637  std::string getValueString() const;
638 
639 
640 private:
642  std::string myValueString;
643 
644 };
645 
646 
647 /* -------------------------------------------------------------------------
648  * Option_IntVector
649  * ----------------------------------------------------------------------- */
650 class Option_IntVector : public Option {
651 public:
655 
656 
661  Option_IntVector(const IntVector& value);
662 
663 
666 
667 
669  virtual ~Option_IntVector();
670 
671 
674 
675 
680  const IntVector& getIntVector() const;
681 
682 
698  bool set(const std::string& v);
699 
700 
708  std::string getValueString() const;
709 
710 
711 private:
714 };
715 
716 
717 /* -------------------------------------------------------------------------
718  * Option_StringVector
719  * ----------------------------------------------------------------------- */
720 class Option_StringVector : public Option {
721 public:
725 
730  Option_StringVector(const StringVector& value);
731 
734 
736  virtual ~Option_StringVector();
737 
740 
745  const StringVector& getStringVector() const;
746 
763  bool set(const std::string& v);
764 
772  std::string getValueString() const;
773 
774 private:
777 };
778 
779 
780 /* -------------------------------------------------------------------------
781  * Option_FileName
782  * ----------------------------------------------------------------------- */
784 public:
787  Option_FileName();
788 
793  Option_FileName(const StringVector& value);
794 
797 
799  virtual ~Option_FileName();
800 
803 
810  bool isFileName() const;
811 
820  std::string getString() const;
821 
829  std::string getValueString() const;
830 };
831 #endif
832 
833 /****************************************************************************/
834 
Option_StringVector::getStringVector
const StringVector & getStringVector() const
Returns the stored string vector.
Definition: Option.cpp:543
Option_StringVector::operator=
Option_StringVector & operator=(const Option_StringVector &s)
Assignment operator.
Definition: Option.cpp:536
Option_FileName::getString
std::string getString() const
Legacy method that returns the stored filenames as a comma-separated string.
Definition: Option.cpp:599
Option::myAmSet
bool myAmSet
information whether the value is set
Definition: Option.h:310
Option::~Option
virtual ~Option()
Definition: Option.cpp:52
Option_BoolExtended::~Option_BoolExtended
~Option_BoolExtended()
Destructor.
Definition: Option.cpp:416
Option_String::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:285
Option_BoolExtended
Definition: Option.h:602
Option
A class representing a single program option.
Definition: Option.h:75
Option_Bool
Definition: Option.h:538
Option_StringVector::myValue
StringVector myValue
Definition: Option.h:776
Option_Integer::set
bool set(const std::string &v)
Stores the given value after parsing it into an integer.
Definition: Option.cpp:216
Option_Bool::isBool
bool isBool() const
Returns true, the information whether the option is a bool option.
Definition: Option.cpp:402
Option::isWriteable
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:143
Option_Integer::operator=
Option_Integer & operator=(const Option_Integer &s)
Assignment operator.
Definition: Option.cpp:199
Option_BoolExtended::operator=
Option_BoolExtended & operator=(const Option_BoolExtended &s)
Assignment operator.
Definition: Option.cpp:426
Option_Float::operator=
Option_Float & operator=(const Option_Float &s)
Assignment operator.
Definition: Option.cpp:310
Option::resetDefault
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:155
Option_IntVector::Option_IntVector
Option_IntVector()
Constructor for an option with no default value.
Definition: Option.cpp:459
Option_Bool::operator=
Option_Bool & operator=(const Option_Bool &s)
Assignment operator.
Definition: Option.cpp:365
Option::isFileName
virtual bool isFileName() const
Returns the information whether this option is a file name.
Definition: Option.cpp:137
Option_Bool::set
virtual bool set(const std::string &v)
Definition: Option.cpp:382
Option_Float::myValue
double myValue
Definition: Option.h:530
Option_StringVector::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:567
Option_StringVector::~Option_StringVector
virtual ~Option_StringVector()
Destructor.
Definition: Option.cpp:533
Option_IntVector::myValue
IntVector myValue
Definition: Option.h:713
Option_FileName::isFileName
bool isFileName() const
Returns true, the information whether this option is a file name.
Definition: Option.cpp:594
Option::isBool
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition: Option.cpp:125
Option::setDescription
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:167
Option::isDefault
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:131
Option_IntVector::set
bool set(const std::string &v)
Stores the given value after parsing it into a vector of integers.
Definition: Option.cpp:493
Option::myDescription
std::string myDescription
The description what this option does.
Definition: Option.h:319
IntVector
std::vector< int > IntVector
Definition of a vector of ints.
Definition: Option.h:40
Option_Integer::getInt
int getInt() const
Returns the stored integer value.
Definition: Option.cpp:210
Option::getTypeName
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:173
Option::Option
Option(bool set=false)
Constructor.
Definition: Option.cpp:43
Option_Integer::Option_Integer
Option_Integer(int value)
Constructor for an option with a default value.
Definition: Option.cpp:183
Option_BoolExtended::Option_BoolExtended
Option_BoolExtended(bool value)
Constructor for an option that can be used without an argument like Option_BoolExtended but which als...
Definition: Option.cpp:411
Option_Float::getFloat
double getFloat() const
Returns the stored double value.
Definition: Option.cpp:321
StringVector
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition: Option.h:45
Option_Bool::getValueString
virtual std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:393
Option_FileName::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:603
Option_String::getString
std::string getString() const
Returns the stored string value.
Definition: Option.cpp:272
Option_String::operator=
Option_String & operator=(const Option_String &s)
Assignment operator.
Definition: Option.cpp:261
Option_BoolExtended::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:451
Option_Integer::~Option_Integer
~Option_Integer()
Destructor.
Definition: Option.cpp:189
Option::getDescription
const std::string & getDescription() const
Returns the description of what this option does.
Definition: Option.cpp:161
Option_Float::Option_Float
Option_Float(double value)
Constructor for an option with a default value.
Definition: Option.cpp:294
Option_StringVector::set
bool set(const std::string &v)
Stores the given value after parsing it into a vector of strings.
Definition: Option.cpp:548
Option::isSet
bool isSet() const
returns the information whether this options holds a valid value
Definition: Option.cpp:68
Option_IntVector::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:513
Option_String::Option_String
Option_String()
Constructor for an option with no default value.
Definition: Option.cpp:239
Option_String
Definition: Option.h:399
UtilExceptions.h
Option_Float::set
bool set(const std::string &v)
Stores the given value after parsing it into a double.
Definition: Option.cpp:327
Option_FileName::Option_FileName
Option_FileName()
Constructor for an option with no default value.
Definition: Option.cpp:575
Option::set
virtual bool set(const std::string &v)=0
Stores the given value.
Option::getFloat
virtual double getFloat() const
Returns the stored double value.
Definition: Option.cpp:74
Option_Bool::~Option_Bool
~Option_Bool()
Destructor.
Definition: Option.cpp:355
Option_Float::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:338
Option_String::set
bool set(const std::string &v)
Stores the given value.
Definition: Option.cpp:278
Option_StringVector
Definition: Option.h:720
Option::resetWritable
void resetWritable()
Resets the option to be writeable.
Definition: Option.cpp:149
Option_Integer::getValueString
std::string getValueString() const
Returns the string-representation of the value.
Definition: Option.cpp:228
Option_IntVector::~Option_IntVector
virtual ~Option_IntVector()
Destructor.
Definition: Option.cpp:475
Option::unSet
void unSet()
marks this option as unset
Definition: Option.cpp:118
Option_Float
Definition: Option.h:470
Option_IntVector::getIntVector
const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:487
Option_FileName::~Option_FileName
virtual ~Option_FileName()
Destructor.
Definition: Option.cpp:587
Option_Bool::getBool
bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:376
Option::getString
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:86
Option_StringVector::Option_StringVector
Option_StringVector()
Constructor for an option with no default value.
Definition: Option.cpp:521
Option_Float::~Option_Float
~Option_Float()
Destructor.
Definition: Option.cpp:300
Option::makeUnsetWithDefault
static OptionType * makeUnsetWithDefault(ValueType def)
Create a new Option of the given type with given default value but make it unset.
Definition: Option.h:267
Option_Bool::Option_Bool
Option_Bool(bool value)
Constructor for an option with a default value.
Definition: Option.cpp:349
Option_Bool::myValue
bool myValue
Definition: Option.h:593
Option::getIntVector
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:98
Option_BoolExtended::myValueString
std::string myValueString
Definition: Option.h:642
config.h
Option_FileName
Definition: Option.h:783
Option_BoolExtended::set
bool set(const std::string &v)
Definition: Option.cpp:438
Option::markSet
bool markSet()
Marks the information as set.
Definition: Option.cpp:108
Option_IntVector
Definition: Option.h:650
Option::getBool
virtual bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:92
Option::operator=
virtual Option & operator=(const Option &s)
Assignment operator.
Definition: Option.cpp:56
Option::getStringVector
virtual const StringVector & getStringVector() const
Returns the stored string vector.
Definition: Option.cpp:103
Option::myTypeName
std::string myTypeName
A type name for this option (has presets, but may be overwritten)
Definition: Option.h:305
Option::getInt
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:80
Option_Integer::myValue
int myValue
Definition: Option.h:391
Option_String::~Option_String
virtual ~Option_String()
Destructor.
Definition: Option.cpp:251
Option_Integer
An integer-option.
Definition: Option.h:331
Option::getValueString
virtual std::string getValueString() const =0
Returns the string-representation of the value.
Option_String::myValue
std::string myValue
Definition: Option.h:462
Option::myAmWritable
bool myAmWritable
information whether the value may be changed
Definition: Option.h:316
Option_IntVector::operator=
Option_IntVector & operator=(const Option_IntVector &s)
Assignment operator.
Definition: Option.cpp:479
Option_FileName::operator=
Option_FileName & operator=(const Option_FileName &s)
Assignment operator.
Definition: Option.cpp:589
Option::myHaveTheDefaultValue
bool myHaveTheDefaultValue
information whether the value is the default value (is then set)
Definition: Option.h:313