Eclipse SUMO - Simulation of Urban MObility
SUMOSAXAttributes.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2007-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 /****************************************************************************/
17 // Encapsulated SAX-Attributes
18 /****************************************************************************/
19 #ifndef SUMOSAXAttributes_h
20 #define SUMOSAXAttributes_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <vector>
30 #include <set>
31 
32 #include <utils/common/SUMOTime.h>
33 #include <utils/common/ToString.h>
35 #include "SUMOXMLDefinitions.h"
36 
37 
38 // ===========================================================================
39 // class declarations
40 // ===========================================================================
41 class PositionVector;
42 class Boundary;
43 class RGBColor;
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
58 public:
59  /* @brief Constructor
60  * @param[in] tagName The name of the parsed object type; used for error message generation
61  */
62  SUMOSAXAttributes(const std::string& objectType);
63 
64 
66  virtual ~SUMOSAXAttributes() { }
67 
68 
82  template <typename T>
83  T get(int attr, const char* objectid, bool& ok, bool report = true) const;
84 
85 
101  template <typename T>
102  T getOpt(int attr, const char* objectid, bool& ok, T defaultValue, bool report = true) const;
103 
104 
121  SUMOTime getSUMOTimeReporting(int attr, const char* objectid, bool& ok,
122  bool report = true) const;
123 
124 
125 
144  SUMOTime getOptSUMOTimeReporting(int attr, const char* objectid, bool& ok,
145  SUMOTime defaultValue, bool report = true) const;
146 
147 
148 
151 
157  virtual bool hasAttribute(int id) const = 0;
158 
159 
165  virtual bool hasAttribute(const std::string& id) const = 0;
166 
167 
183  virtual bool getBool(int id) const = 0;
184 
200  virtual int getInt(int id) const = 0;
201 
202 
218  virtual long long int getLong(int id) const = 0;
219 
220 
233  virtual std::string getString(int id) const = 0;
234 
235 
248  virtual std::string getStringSecure(int id,
249  const std::string& def) const = 0;
250 
251 
267  virtual double getFloat(int id) const = 0;
268 
269 
285  virtual double getFloat(const std::string& id) const = 0;
286 
287 
297  virtual std::string getStringSecure(const std::string& id,
298  const std::string& def) const = 0;
299 
300 
307  virtual SumoXMLEdgeFunc getEdgeFunc(bool& ok) const = 0;
308 
309 
316  virtual SumoXMLNodeType getNodeType(bool& ok) const = 0;
317 
321  virtual RightOfWay getRightOfWay(bool& ok) const = 0;
322 
324  virtual FringeType getFringeType(bool& ok) const = 0;
325 
332  virtual RGBColor getColor() const = 0;
333 
334 
340  virtual PositionVector getShape(int attr) const = 0;
341 
347  virtual Boundary getBoundary(int attr) const = 0;
348 
357  const std::vector<std::string> getStringVector(int attr) const;
358 
360  const std::vector<std::string> getOptStringVector(int attr, const char* objectid, bool& ok, bool report = true) const;
361 
370  const std::vector<int> getIntVector(int attr) const;
371 
373  const std::vector<int> getOptIntVector(int attr, const char* objectid, bool& ok, bool report = true) const;
374  //}
375 
376 
382  virtual std::string getName(int attr) const = 0;
383 
384 
389  virtual void serialize(std::ostream& os) const = 0;
390 
393  virtual std::vector<std::string> getAttributeNames() const = 0;
394 
395 
397  const std::string& getObjectType() const {
398  return myObjectType;
399  }
400 
401 
402  friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
403 
405  virtual SUMOSAXAttributes* clone() const = 0;
406 
408  static const std::string ENCODING;
409 
410 
411 protected:
412  template <typename T> T getInternal(const int attr) const;
413  void emitUngivenError(const std::string& attrname, const char* objectid) const;
414  void emitEmptyError(const std::string& attrname, const char* objectid) const;
415  void emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const;
416 
417 private:
420 
423 
425  std::string myObjectType;
426 
427 };
428 
429 
430 inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
431  src.serialize(os);
432  return os;
433 }
434 
435 
436 template<typename X> struct invalid_return {
437  static const X value;
438  static const std::string type;
439 };
440 
441 template<> struct invalid_return<bool> {
442  static const bool value;
443  static const std::string type;
444 };
445 
446 template<> struct invalid_return<int> {
447  static const int value;
448  static const std::string type;
449 };
450 
451 template<> struct invalid_return<long long int> {
452  static const long long int value;
453  static const std::string type;
454 };
455 
456 template<> struct invalid_return<double> {
457  static const double value;
458  static const std::string type;
459 };
460 
461 template<> struct invalid_return<std::string> {
462  static const std::string value;
463  static const std::string type;
464 };
465 
466 template<> struct invalid_return<RGBColor> {
467  static const RGBColor value;
468  static const std::string type;
469 };
470 
471 template<> struct invalid_return<PositionVector> {
472  static const PositionVector value;
473  static const std::string type;
474 };
475 
476 template<> struct invalid_return<Boundary> {
477  static const Boundary value;
478  static const std::string type;
479 };
480 
481 template<> struct invalid_return<std::vector<std::string> > {
482  static const std::vector<std::string> value;
483  static const std::string type;
484 };
485 
486 template<> struct invalid_return<std::vector<int> > {
487  static const std::vector<int> value;
488  static const std::string type;
489 };
490 
491 
492 template <typename T>
493 T SUMOSAXAttributes::get(int attr, const char* objectid,
494  bool& ok, bool report) const {
495  if (!hasAttribute(attr)) {
496  if (report) {
497  emitUngivenError(getName(attr), objectid);
498  }
499  ok = false;
501  }
502  try {
503  return getInternal<T>(attr);
504  } catch (FormatException&) {
505  if (report) {
506  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
507  }
508  } catch (EmptyData&) {
509  if (report) {
510  emitEmptyError(getName(attr), objectid);
511  }
512  }
513  ok = false;
515 }
516 
517 
518 template <typename T>
519 T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
520  bool& ok, T defaultValue, bool report) const {
521  if (!hasAttribute(attr)) {
522  return defaultValue;
523  }
524  try {
525  return getInternal<T>(attr);
526  } catch (FormatException&) {
527  if (report) {
528  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
529  }
530  } catch (EmptyData&) {
531  if (report) {
532  emitEmptyError(getName(attr), objectid);
533  }
534  }
535  ok = false;
537 }
538 
539 
540 #endif
541 
542 /****************************************************************************/
543 
SUMOSAXAttributes::serialize
virtual void serialize(std::ostream &os) const =0
Prints all attribute names and values into the given stream.
SUMOSAXAttributes::getStringVector
const std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
Definition: SUMOSAXAttributes.cpp:114
ToString.h
FringeType
FringeType
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1105
SUMOSAXAttributes::hasAttribute
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
invalid_return< std::vector< std::string > >::value
static const std::vector< std::string > value
Definition: SUMOSAXAttributes.h:482
SUMOTime.h
invalid_return< RGBColor >::type
static const std::string type
Definition: SUMOSAXAttributes.h:468
invalid_return< std::string >::type
static const std::string type
Definition: SUMOSAXAttributes.h:463
SUMOSAXAttributes::getString
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
invalid_return< PositionVector >::type
static const std::string type
Definition: SUMOSAXAttributes.h:473
invalid_return< int >::value
static const int value
Definition: SUMOSAXAttributes.h:447
SUMOSAXAttributes::getInt
virtual int getInt(int id) const =0
Returns the int-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::get
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
Definition: SUMOSAXAttributes.h:493
SUMOSAXAttributes::getOptIntVector
const std::vector< int > getOptIntVector(int attr, const char *objectid, bool &ok, bool report=true) const
convenience function to avoid the default argument and the template stuff at getOpt<>
Definition: SUMOSAXAttributes.cpp:143
SUMOSAXAttributes::operator=
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)
Invalidated assignment operator.
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
invalid_return< bool >::type
static const std::string type
Definition: SUMOSAXAttributes.h:443
SUMOSAXAttributes::myObjectType
std::string myObjectType
the object type to use in error reporting
Definition: SUMOSAXAttributes.h:425
SUMOSAXAttributes::getObjectType
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Definition: SUMOSAXAttributes.h:397
SUMOSAXAttributes::clone
virtual SUMOSAXAttributes * clone() const =0
return a new deep-copy attributes object
SumoXMLEdgeFunc
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
Definition: SUMOXMLDefinitions.h:1074
EmptyData
Definition: UtilExceptions.h:69
invalid_return< Boundary >::value
static const Boundary value
Definition: SUMOSAXAttributes.h:477
SUMOSAXAttributes::getBool
virtual bool getBool(int id) const =0
Returns the bool-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::getFloat
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::getSUMOTimeReporting
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
Definition: SUMOSAXAttributes.cpp:64
PositionVector
A list of positions.
Definition: PositionVector.h:46
SUMOSAXAttributes::getInternal
T getInternal(const int attr) const
invalid_return< std::string >::value
static const std::string value
Definition: SUMOSAXAttributes.h:462
SUMOSAXAttributes::emitFormatError
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:176
SUMOSAXAttributes::getFringeType
virtual FringeType getFringeType(bool &ok) const =0
returns fringe type
SUMOSAXAttributes::~SUMOSAXAttributes
virtual ~SUMOSAXAttributes()
Destructor.
Definition: SUMOSAXAttributes.h:66
RGBColor
Definition: RGBColor.h:40
SUMOSAXAttributes::getShape
virtual PositionVector getShape(int attr) const =0
Tries to read given attribute assuming it is a PositionVector.
invalid_return< std::vector< std::string > >::type
static const std::string type
Definition: SUMOSAXAttributes.h:483
invalid_return< long long int >::value
static const long long int value
Definition: SUMOSAXAttributes.h:452
SumoXMLNodeType
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
Definition: SUMOXMLDefinitions.h:1049
SUMOSAXAttributes::getOptStringVector
const std::vector< std::string > getOptStringVector(int attr, const char *objectid, bool &ok, bool report=true) const
convenience function to avoid the default argument and the template stuff at getOpt<>
Definition: SUMOSAXAttributes.cpp:124
invalid_return< double >::type
static const std::string type
Definition: SUMOSAXAttributes.h:458
invalid_return< long long int >::type
static const std::string type
Definition: SUMOSAXAttributes.h:453
SUMOSAXAttributes::emitUngivenError
void emitUngivenError(const std::string &attrname, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:148
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
invalid_return< bool >::value
static const bool value
Definition: SUMOSAXAttributes.h:442
SUMOSAXAttributes::getRightOfWay
virtual RightOfWay getRightOfWay(bool &ok) const =0
Returns the right-of-way method.
SUMOSAXAttributes::getIntVector
const std::vector< int > getIntVector(int attr) const
Tries to read given attribute assuming it is an int vector.
Definition: SUMOSAXAttributes.cpp:129
UtilExceptions.h
invalid_return::type
static const std::string type
Definition: SUMOSAXAttributes.h:438
SUMOSAXAttributes::getOpt
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
Definition: SUMOSAXAttributes.h:519
FormatException
Definition: UtilExceptions.h:82
invalid_return::value
static const X value
Definition: SUMOSAXAttributes.h:437
operator<<
std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
Definition: SUMOSAXAttributes.h:430
SUMOSAXAttributes::getBoundary
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
invalid_return< std::vector< int > >::type
static const std::string type
Definition: SUMOSAXAttributes.h:488
invalid_return< PositionVector >::value
static const PositionVector value
Definition: SUMOSAXAttributes.h:472
SUMOSAXAttributes::getName
virtual std::string getName(int attr) const =0
Converts the given attribute id into a man readable string.
SUMOSAXAttributes::getOptSUMOTimeReporting
SUMOTime getOptSUMOTimeReporting(int attr, const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
Definition: SUMOSAXAttributes.cpp:91
invalid_return
Definition: SUMOSAXAttributes.h:436
RightOfWay
RightOfWay
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1099
invalid_return< std::vector< int > >::value
static const std::vector< int > value
Definition: SUMOSAXAttributes.h:487
SUMOSAXAttributes::getNodeType
virtual SumoXMLNodeType getNodeType(bool &ok) const =0
Returns the value of the named attribute.
SUMOSAXAttributes::ENCODING
static const std::string ENCODING
The encoding of parsed strings.
Definition: SUMOSAXAttributes.h:408
SUMOSAXAttributes::SUMOSAXAttributes
SUMOSAXAttributes(const std::string &objectType)
Definition: SUMOSAXAttributes.cpp:47
SUMOSAXAttributes::emitEmptyError
void emitEmptyError(const std::string &attrname, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:162
invalid_return< Boundary >::type
static const std::string type
Definition: SUMOSAXAttributes.h:478
config.h
SUMOSAXAttributes::operator<<
friend std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
Definition: SUMOSAXAttributes.h:430
SUMOSAXAttributes::getEdgeFunc
virtual SumoXMLEdgeFunc getEdgeFunc(bool &ok) const =0
Returns the value of the named attribute.
SUMOSAXAttributes::getStringSecure
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
invalid_return< double >::value
static const double value
Definition: SUMOSAXAttributes.h:457
invalid_return< int >::type
static const std::string type
Definition: SUMOSAXAttributes.h:448
SUMOSAXAttributes::getLong
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
SUMOSAXAttributes::getColor
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
SUMOSAXAttributes::getAttributeNames
virtual std::vector< std::string > getAttributeNames() const =0
Retrieves all attribute names.
invalid_return< RGBColor >::value
static const RGBColor value
Definition: SUMOSAXAttributes.h:467
SUMOXMLDefinitions.h