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 /****************************************************************************/
16 // Encapsulated SAX-Attributes
17 /****************************************************************************/
18 #ifndef SUMOSAXAttributes_h
19 #define SUMOSAXAttributes_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <vector>
29 #include <set>
30 
31 #include <utils/common/SUMOTime.h>
32 #include <utils/common/ToString.h>
34 #include "SUMOXMLDefinitions.h"
35 
36 
37 // ===========================================================================
38 // class declarations
39 // ===========================================================================
40 class PositionVector;
41 class Boundary;
42 class RGBColor;
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
57 public:
58  /* @brief Constructor
59  * @param[in] tagName The name of the parsed object type; used for error message generation
60  */
61  SUMOSAXAttributes(const std::string& objectType);
62 
63 
65  virtual ~SUMOSAXAttributes() { }
66 
67 
81  template <typename T>
82  T get(int attr, const char* objectid, bool& ok, bool report = true) const;
83 
84 
100  template <typename T>
101  T getOpt(int attr, const char* objectid, bool& ok, T defaultValue, bool report = true) const;
102 
103 
120  SUMOTime getSUMOTimeReporting(int attr, const char* objectid, bool& ok,
121  bool report = true) const;
122 
123 
124 
143  SUMOTime getOptSUMOTimeReporting(int attr, const char* objectid, bool& ok,
144  SUMOTime defaultValue, bool report = true) const;
145 
146 
147 
150 
156  virtual bool hasAttribute(int id) const = 0;
157 
158 
164  virtual bool hasAttribute(const std::string& id) const = 0;
165 
166 
182  virtual bool getBool(int id) const = 0;
183 
199  virtual int getInt(int id) const = 0;
200 
201 
217  virtual long long int getLong(int id) const = 0;
218 
219 
232  virtual std::string getString(int id) const = 0;
233 
234 
247  virtual std::string getStringSecure(int id,
248  const std::string& def) const = 0;
249 
250 
266  virtual double getFloat(int id) const = 0;
267 
268 
284  virtual double getFloat(const std::string& id) const = 0;
285 
286 
296  virtual std::string getStringSecure(const std::string& id,
297  const std::string& def) const = 0;
298 
299 
306  virtual SumoXMLEdgeFunc getEdgeFunc(bool& ok) const = 0;
307 
308 
315  virtual SumoXMLNodeType getNodeType(bool& ok) const = 0;
316 
320  virtual RightOfWay getRightOfWay(bool& ok) const = 0;
321 
323  virtual FringeType getFringeType(bool& ok) const = 0;
324 
331  virtual RGBColor getColor() const = 0;
332 
333 
339  virtual PositionVector getShape(int attr) const = 0;
340 
346  virtual Boundary getBoundary(int attr) const = 0;
347 
356  const std::vector<std::string> getStringVector(int attr) const;
357 
359  const std::vector<std::string> getOptStringVector(int attr, const char* objectid, bool& ok, bool report = true) const;
360 
369  const std::vector<int> getIntVector(int attr) const;
370 
372  const std::vector<int> getOptIntVector(int attr, const char* objectid, bool& ok, bool report = true) const;
373  //}
374 
375 
381  virtual std::string getName(int attr) const = 0;
382 
383 
388  virtual void serialize(std::ostream& os) const = 0;
389 
392  virtual std::vector<std::string> getAttributeNames() const = 0;
393 
394 
396  const std::string& getObjectType() const {
397  return myObjectType;
398  }
399 
400 
401  friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
402 
404  virtual SUMOSAXAttributes* clone() const = 0;
405 
407  static const std::string ENCODING;
408 
409 
410 protected:
411  template <typename T> T getInternal(const int attr) const;
412  void emitUngivenError(const std::string& attrname, const char* objectid) const;
413  void emitEmptyError(const std::string& attrname, const char* objectid) const;
414  void emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const;
415 
416 private:
419 
422 
424  std::string myObjectType;
425 
426 };
427 
428 
429 inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
430  src.serialize(os);
431  return os;
432 }
433 
434 
435 template<typename X> struct invalid_return {
436  static const X value;
437  static const std::string type;
438 };
439 
440 template<> struct invalid_return<bool> {
441  static const bool value;
442  static const std::string type;
443 };
444 
445 template<> struct invalid_return<int> {
446  static const int value;
447  static const std::string type;
448 };
449 
450 template<> struct invalid_return<long long int> {
451  static const long long int value;
452  static const std::string type;
453 };
454 
455 template<> struct invalid_return<double> {
456  static const double value;
457  static const std::string type;
458 };
459 
460 template<> struct invalid_return<std::string> {
461  static const std::string value;
462  static const std::string type;
463 };
464 
465 template<> struct invalid_return<RGBColor> {
466  static const RGBColor value;
467  static const std::string type;
468 };
469 
470 template<> struct invalid_return<PositionVector> {
471  static const PositionVector value;
472  static const std::string type;
473 };
474 
475 template<> struct invalid_return<Boundary> {
476  static const Boundary value;
477  static const std::string type;
478 };
479 
480 template<> struct invalid_return<std::vector<std::string> > {
481  static const std::vector<std::string> value;
482  static const std::string type;
483 };
484 
485 template<> struct invalid_return<std::vector<int> > {
486  static const std::vector<int> value;
487  static const std::string type;
488 };
489 
490 
491 template <typename T>
492 T SUMOSAXAttributes::get(int attr, const char* objectid,
493  bool& ok, bool report) const {
494  if (!hasAttribute(attr)) {
495  if (report) {
496  emitUngivenError(getName(attr), objectid);
497  }
498  ok = false;
500  }
501  try {
502  return getInternal<T>(attr);
503  } catch (FormatException&) {
504  if (report) {
505  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
506  }
507  } catch (EmptyData&) {
508  if (report) {
509  emitEmptyError(getName(attr), objectid);
510  }
511  }
512  ok = false;
514 }
515 
516 
517 template <typename T>
518 T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
519  bool& ok, T defaultValue, bool report) const {
520  if (!hasAttribute(attr)) {
521  return defaultValue;
522  }
523  try {
524  return getInternal<T>(attr);
525  } catch (FormatException&) {
526  if (report) {
527  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
528  }
529  } catch (EmptyData&) {
530  if (report) {
531  emitEmptyError(getName(attr), objectid);
532  }
533  }
534  ok = false;
536 }
537 
538 
539 #endif
540 
541 /****************************************************************************/
542 
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:113
ToString.h
FringeType
FringeType
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1110
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:481
SUMOTime.h
invalid_return< RGBColor >::type
static const std::string type
Definition: SUMOSAXAttributes.h:467
invalid_return< std::string >::type
static const std::string type
Definition: SUMOSAXAttributes.h:462
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:472
invalid_return< int >::value
static const int value
Definition: SUMOSAXAttributes.h:446
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:492
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:142
SUMOSAXAttributes::operator=
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)
Invalidated assignment operator.
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
invalid_return< bool >::type
static const std::string type
Definition: SUMOSAXAttributes.h:442
SUMOSAXAttributes::myObjectType
std::string myObjectType
the object type to use in error reporting
Definition: SUMOSAXAttributes.h:424
SUMOSAXAttributes::getObjectType
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Definition: SUMOSAXAttributes.h:396
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:1079
EmptyData
Definition: UtilExceptions.h:68
invalid_return< Boundary >::value
static const Boundary value
Definition: SUMOSAXAttributes.h:476
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:63
PositionVector
A list of positions.
Definition: PositionVector.h:45
SUMOSAXAttributes::getInternal
T getInternal(const int attr) const
invalid_return< std::string >::value
static const std::string value
Definition: SUMOSAXAttributes.h:461
SUMOSAXAttributes::emitFormatError
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:175
SUMOSAXAttributes::getFringeType
virtual FringeType getFringeType(bool &ok) const =0
returns fringe type
SUMOSAXAttributes::~SUMOSAXAttributes
virtual ~SUMOSAXAttributes()
Destructor.
Definition: SUMOSAXAttributes.h:65
RGBColor
Definition: RGBColor.h:39
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:482
invalid_return< long long int >::value
static const long long int value
Definition: SUMOSAXAttributes.h:451
SumoXMLNodeType
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
Definition: SUMOXMLDefinitions.h:1054
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:123
invalid_return< double >::type
static const std::string type
Definition: SUMOSAXAttributes.h:457
invalid_return< long long int >::type
static const std::string type
Definition: SUMOSAXAttributes.h:452
SUMOSAXAttributes::emitUngivenError
void emitUngivenError(const std::string &attrname, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:147
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
invalid_return< bool >::value
static const bool value
Definition: SUMOSAXAttributes.h:441
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:128
UtilExceptions.h
invalid_return::type
static const std::string type
Definition: SUMOSAXAttributes.h:437
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:518
FormatException
Definition: UtilExceptions.h:81
invalid_return::value
static const X value
Definition: SUMOSAXAttributes.h:436
operator<<
std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
Definition: SUMOSAXAttributes.h:429
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:487
invalid_return< PositionVector >::value
static const PositionVector value
Definition: SUMOSAXAttributes.h:471
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:90
invalid_return
Definition: SUMOSAXAttributes.h:435
RightOfWay
RightOfWay
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1104
invalid_return< std::vector< int > >::value
static const std::vector< int > value
Definition: SUMOSAXAttributes.h:486
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:407
SUMOSAXAttributes::SUMOSAXAttributes
SUMOSAXAttributes(const std::string &objectType)
Definition: SUMOSAXAttributes.cpp:46
SUMOSAXAttributes::emitEmptyError
void emitEmptyError(const std::string &attrname, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:161
invalid_return< Boundary >::type
static const std::string type
Definition: SUMOSAXAttributes.h:477
config.h
SUMOSAXAttributes::operator<<
friend std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
Definition: SUMOSAXAttributes.h:429
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:456
invalid_return< int >::type
static const std::string type
Definition: SUMOSAXAttributes.h:447
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:56
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:466
SUMOXMLDefinitions.h