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-2018 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 
323 
330  virtual RGBColor getColor() const = 0;
331 
332 
338  virtual PositionVector getShape(int attr) const = 0;
339 
345  virtual Boundary getBoundary(int attr) const = 0;
346 
352  virtual std::vector<std::string> getStringVector(int attr) const = 0;
353  //}
354 
355 
361  virtual std::string getName(int attr) const = 0;
362 
363 
368  virtual void serialize(std::ostream& os) const = 0;
369 
370 
372  const std::string& getObjectType() const {
373  return myObjectType;
374  }
375 
376 
377  friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
378 
380  virtual SUMOSAXAttributes* clone() const = 0;
381 
383  static const std::string ENCODING;
384 
385 
394  static void parseStringVector(const std::string& def, std::vector<std::string>& into);
395 
396 
405  static void parseStringSet(const std::string& def, std::set<std::string>& into);
406 
407 
408 protected:
409  template <typename T> T getInternal(const int attr) const;
410  void emitUngivenError(const std::string& attrname, const char* objectid) const;
411  void emitEmptyError(const std::string& attrname, const char* objectid) const;
412  void emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const;
413 
414 private:
417 
420 
422  std::string myObjectType;
423 
424 };
425 
426 
427 inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
428  src.serialize(os);
429  return os;
430 }
431 
432 
433 template<typename X> struct invalid_return {
434  static const X value;
435  static const std::string type;
436 };
437 
438 template<> struct invalid_return<bool> {
439  static const bool value;
440  static const std::string type;
441 };
442 
443 template<> struct invalid_return<int> {
444  static const int value;
445  static const std::string type;
446 };
447 
448 template<> struct invalid_return<long long int> {
449  static const long long int value;
450  static const std::string type;
451 };
452 
453 template<> struct invalid_return<double> {
454  static const double value;
455  static const std::string type;
456 };
457 
458 template<> struct invalid_return<std::string> {
459  static const std::string value;
460  static const std::string type;
461 };
462 
463 template<> struct invalid_return<RGBColor> {
464  static const RGBColor value;
465  static const std::string type;
466 };
467 
468 template<> struct invalid_return<PositionVector> {
469  static const PositionVector value;
470  static const std::string type;
471 };
472 
473 template<> struct invalid_return<Boundary> {
474  static const Boundary value;
475  static const std::string type;
476 };
477 
478 
479 template <typename T>
480 T SUMOSAXAttributes::get(int attr, const char* objectid,
481  bool& ok, bool report) const {
482  if (!hasAttribute(attr)) {
483  if (report) {
484  emitUngivenError(getName(attr), objectid);
485  }
486  ok = false;
488  }
489  try {
490  return getInternal<T>(attr);
491  } catch (FormatException&) {
492  if (report) {
493  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
494  }
495  } catch (EmptyData&) {
496  if (report) {
497  emitEmptyError(getName(attr), objectid);
498  }
499  }
500  ok = false;
502 }
503 
504 
505 template <typename T>
506 T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
507  bool& ok, T defaultValue, bool report) const {
508  if (!hasAttribute(attr)) {
509  return defaultValue;
510  }
511  try {
512  return getInternal<T>(attr);
513  } catch (FormatException&) {
514  if (report) {
515  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
516  }
517  } catch (EmptyData&) {
518  if (report) {
519  emitEmptyError(getName(attr), objectid);
520  }
521  }
522  ok = false;
524 }
525 
526 
527 #endif
528 
529 /****************************************************************************/
530 
SUMOSAXAttributes(const std::string &objectType)
static const long long int value
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
long long int SUMOTime
Definition: SUMOTime.h:36
static void parseStringSet(const std::string &def, std::set< std::string > &into)
Splits the given string, stores it in a set.
static const std::string type
virtual SUMOSAXAttributes * clone() const =0
return a new deep-copy attributes object
virtual PositionVector getShape(int attr) const =0
Tries to read given attribute assuming it is a PositionVector.
virtual std::string getName(int attr) const =0
Converts the given attribute id into a man readable string.
std::string myObjectType
the object type to use in error reporting
static const bool value
static const PositionVector value
const std::string & getObjectType() const
return the objecttype to which these attributes belong
static const std::string type
static const Boundary value
static const std::string type
static const std::string value
RightOfWay
algorithms for computing right of way
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
virtual SumoXMLEdgeFunc getEdgeFunc(bool &ok) const =0
Returns the value of the named attribute.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
static const X value
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
static const std::string type
virtual RightOfWay getRightOfWay(bool &ok) const =0
Returns the right-of-way method.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
static const std::string type
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
A list of positions.
static const std::string ENCODING
The encoding of parsed strings.
void emitUngivenError(const std::string &attrname, const char *objectid) const
static const RGBColor value
static const double value
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
virtual SumoXMLNodeType getNodeType(bool &ok) const =0
Returns the value of the named attribute.
void emitEmptyError(const std::string &attrname, const char *objectid) const
virtual void serialize(std::ostream &os) const =0
Prints all attribute names and values into the given stream.
virtual std::vector< std::string > getStringVector(int attr) const =0
Tries to read given attribute assuming it is a string vector.
static const std::string type
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
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.
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
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.
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
T getInternal(const int attr) const
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
static const std::string type
virtual ~SUMOSAXAttributes()
Destructor.
friend std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
virtual int getInt(int id) const =0
Returns the int-value of the named (by its enum-value) attribute.
virtual bool getBool(int id) const =0
Returns the bool-value of the named (by its enum-value) attribute.
static const std::string type
static const std::string type
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)
Invalidated assignment operator.
static const int value