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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Encapsulated SAX-Attributes
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <string>
26 #include <vector>
27 #include <set>
28 
29 #include <utils/common/SUMOTime.h>
30 #include <utils/common/ToString.h>
32 #include "SUMOXMLDefinitions.h"
33 
34 
35 // ===========================================================================
36 // class declarations
37 // ===========================================================================
38 class PositionVector;
39 class Boundary;
40 class RGBColor;
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
55 public:
56  /* @brief Constructor
57  * @param[in] tagName The name of the parsed object type; used for error message generation
58  */
59  SUMOSAXAttributes(const std::string& objectType);
60 
61 
63  virtual ~SUMOSAXAttributes() { }
64 
65 
79  template <typename T>
80  T get(int attr, const char* objectid, bool& ok, bool report = true) const;
81 
82 
98  template <typename T>
99  T getOpt(int attr, const char* objectid, bool& ok, T defaultValue, bool report = true) const;
100 
101 
118  SUMOTime getSUMOTimeReporting(int attr, const char* objectid, bool& ok,
119  bool report = true) const;
120 
121 
122 
141  SUMOTime getOptSUMOTimeReporting(int attr, const char* objectid, bool& ok,
142  SUMOTime defaultValue, bool report = true) const;
143 
144 
145 
148 
154  virtual bool hasAttribute(int id) const = 0;
155 
156 
162  virtual bool hasAttribute(const std::string& id) const = 0;
163 
164 
180  virtual bool getBool(int id) const = 0;
181 
197  virtual int getInt(int id) const = 0;
198 
199 
215  virtual long long int getLong(int id) const = 0;
216 
217 
230  virtual std::string getString(int id) const = 0;
231 
232 
245  virtual std::string getStringSecure(int id,
246  const std::string& def) const = 0;
247 
248 
264  virtual double getFloat(int id) const = 0;
265 
266 
282  virtual double getFloat(const std::string& id) const = 0;
283 
284 
294  virtual std::string getStringSecure(const std::string& id,
295  const std::string& def) const = 0;
296 
297 
304  virtual SumoXMLEdgeFunc getEdgeFunc(bool& ok) const = 0;
305 
306 
313  virtual SumoXMLNodeType getNodeType(bool& ok) const = 0;
314 
318  virtual RightOfWay getRightOfWay(bool& ok) const = 0;
319 
321  virtual FringeType getFringeType(bool& ok) const = 0;
322 
329  virtual RGBColor getColor() const = 0;
330 
331 
337  virtual PositionVector getShape(int attr) const = 0;
338 
344  virtual Boundary getBoundary(int attr) const = 0;
345 
354  const std::vector<std::string> getStringVector(int attr) const;
355 
357  const std::vector<std::string> getOptStringVector(int attr, const char* objectid, bool& ok, bool report = true) const;
358 
367  const std::vector<int> getIntVector(int attr) const;
368 
370  const std::vector<int> getOptIntVector(int attr, const char* objectid, bool& ok, bool report = true) const;
371  //}
372 
373 
379  virtual std::string getName(int attr) const = 0;
380 
381 
386  virtual void serialize(std::ostream& os) const = 0;
387 
390  virtual std::vector<std::string> getAttributeNames() const = 0;
391 
392 
394  const std::string& getObjectType() const {
395  return myObjectType;
396  }
397 
398 
399  friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
400 
402  virtual SUMOSAXAttributes* clone() const = 0;
403 
405  static const std::string ENCODING;
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 template<> struct invalid_return<std::vector<std::string> > {
479  static const std::vector<std::string> value;
480  static const std::string type;
481 };
482 
483 template<> struct invalid_return<std::vector<int> > {
484  static const std::vector<int> value;
485  static const std::string type;
486 };
487 
488 
489 template <typename T>
490 T SUMOSAXAttributes::get(int attr, const char* objectid,
491  bool& ok, bool report) const {
492  if (!hasAttribute(attr)) {
493  if (report) {
494  emitUngivenError(getName(attr), objectid);
495  }
496  ok = false;
498  }
499  try {
500  return getInternal<T>(attr);
501  } catch (FormatException&) {
502  if (report) {
503  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
504  }
505  } catch (EmptyData&) {
506  if (report) {
507  emitEmptyError(getName(attr), objectid);
508  }
509  }
510  ok = false;
512 }
513 
514 
515 template <typename T>
516 T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
517  bool& ok, T defaultValue, bool report) const {
518  if (!hasAttribute(attr)) {
519  return defaultValue;
520  }
521  try {
522  return getInternal<T>(attr);
523  } catch (FormatException&) {
524  if (report) {
525  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
526  }
527  } catch (EmptyData&) {
528  if (report) {
529  emitEmptyError(getName(attr), objectid);
530  }
531  }
532  ok = false;
534 }
std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
long long int SUMOTime
Definition: SUMOTime.h:31
FringeType
algorithms for computing right of way
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
RightOfWay
algorithms for computing right of way
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
A list of positions.
Encapsulated SAX-Attributes.
virtual bool getBool(int id) const =0
Returns the bool-value of the named (by its enum-value) attribute.
virtual int getInt(int id) const =0
Returns the int-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.
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
virtual std::string getStringSecure(const std::string &id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
virtual PositionVector getShape(int attr) const =0
Tries to read given attribute assuming it is a PositionVector.
virtual void serialize(std::ostream &os) const =0
Prints all attribute names and values into the given stream.
virtual std::vector< std::string > getAttributeNames() const =0
Retrieves all attribute names.
virtual SUMOSAXAttributes * clone() const =0
return a new deep-copy attributes object
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<>
virtual ~SUMOSAXAttributes()
Destructor.
SUMOSAXAttributes(const std::string &objectType)
const std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)
Invalidated assignment operator.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
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.
virtual FringeType getFringeType(bool &ok) const =0
returns fringe type
virtual RightOfWay getRightOfWay(bool &ok) const =0
Returns the right-of-way method.
SUMOSAXAttributes(const SUMOSAXAttributes &src)
Invalidated copy constructor.
T getInternal(const int attr) const
const std::vector< int > getIntVector(int attr) const
Tries to read given attribute assuming it is an int vector.
friend std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
virtual double getFloat(const std::string &id) const =0
Returns the double-value of the named attribute.
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
virtual std::string getName(int attr) const =0
Converts the given attribute id into a man readable string.
void emitEmptyError(const std::string &attrname, const char *objectid) const
std::string myObjectType
the object type to use in error reporting
static const std::string ENCODING
The encoding of parsed strings.
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
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<>
void emitUngivenError(const std::string &attrname, const char *objectid) const
virtual SumoXMLNodeType getNodeType(bool &ok) const =0
Returns the value of the named attribute.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
virtual bool hasAttribute(const std::string &id) const =0
Returns the information whether the named attribute is within the current list.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
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.
static const std::string type
static const Boundary value
static const std::string type
static const PositionVector value
static const std::string type
static const RGBColor value
static const bool value
static const std::string type
static const double value
static const std::string type
static const int value
static const std::string type
static const std::string type
static const long long int value
static const std::string value
static const std::string type
static const std::vector< int > value
static const std::vector< std::string > value
static const X value
static const std::string type