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-2022 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 Position;
39 class PositionVector;
40 class Boundary;
41 class RGBColor;
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
56 public:
57  /* @brief Constructor
58  * @param[in] tagName The name of the parsed object type; used for error message generation
59  */
60  SUMOSAXAttributes(const std::string& objectType);
61 
62 
64  virtual ~SUMOSAXAttributes() { }
65 
66 
80  template <typename T>
81  T get(int attr, const char* objectid, bool& ok, bool report = true) const;
82 
83 
99  template <typename T>
100  T getOpt(int attr, const char* objectid, bool& ok, T defaultValue, bool report = true) const;
101 
102 
119  SUMOTime getSUMOTimeReporting(int attr, const char* objectid, bool& ok,
120  bool report = true) const;
121 
122 
123 
142  SUMOTime getOptSUMOTimeReporting(int attr, const char* objectid, bool& ok,
143  SUMOTime defaultValue, bool report = true) const;
144 
145 
146 
149 
155  virtual bool hasAttribute(int id) const = 0;
156 
157 
163  virtual bool hasAttribute(const std::string& id) const = 0;
164 
165 
181  virtual bool getBool(int id) const = 0;
182 
198  virtual int getInt(int id) const = 0;
199 
200 
216  virtual long long int getLong(int id) const = 0;
217 
218 
231  virtual std::string getString(int id) const = 0;
232 
233 
246  virtual std::string getStringSecure(int id,
247  const std::string& def) const = 0;
248 
249 
265  virtual double getFloat(int id) const = 0;
266 
267 
283  virtual double getFloat(const std::string& id) const = 0;
284 
285 
295  virtual std::string getStringSecure(const std::string& id,
296  const std::string& def) const = 0;
297 
298 
305  virtual SumoXMLEdgeFunc getEdgeFunc(bool& ok) const = 0;
306 
307 
314  virtual SumoXMLNodeType getNodeType(bool& ok) const = 0;
315 
319  virtual RightOfWay getRightOfWay(bool& ok) const = 0;
320 
322  virtual FringeType getFringeType(bool& ok) const = 0;
323 
330  virtual RGBColor getColor() const = 0;
331 
337  virtual Position getPosition(int attr) const = 0;
338 
344  virtual PositionVector getShape(int attr) const = 0;
345 
351  virtual Boundary getBoundary(int attr) const = 0;
352 
361  const std::vector<std::string> getStringVector(int attr) const;
362 
364  const std::vector<std::string> getOptStringVector(int attr, const char* objectid, bool& ok, bool report = true) const;
365 
374  const std::vector<int> getIntVector(int attr) const;
375 
377  const std::vector<int> getOptIntVector(int attr, const char* objectid, bool& ok, bool report = true) const;
378  //}
379 
380 
386  virtual std::string getName(int attr) const = 0;
387 
388 
393  virtual void serialize(std::ostream& os) const = 0;
394 
397  virtual std::vector<std::string> getAttributeNames() const = 0;
398 
399 
401  const std::string& getObjectType() const {
402  return myObjectType;
403  }
404 
405 
406  friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
407 
409  virtual SUMOSAXAttributes* clone() const = 0;
410 
412  static const std::string ENCODING;
413 
414 
415 protected:
416  template <typename T> T getInternal(const int attr) const;
417  void emitUngivenError(const std::string& attrname, const char* objectid) const;
418  void emitEmptyError(const std::string& attrname, const char* objectid) const;
419  void emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const;
420 
421 private:
423  SUMOSAXAttributes(const SUMOSAXAttributes& src) = delete;
424 
427 
429  std::string myObjectType;
430 
431 };
432 
433 
434 inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
435  src.serialize(os);
436  return os;
437 }
438 
439 
440 template<typename X> struct invalid_return {
441  static const X value;
442  static const std::string type;
443 };
444 
445 template<> struct invalid_return<bool> {
446  static const bool value;
447  static const std::string type;
448 };
449 
450 template<> struct invalid_return<int> {
451  static const int value;
452  static const std::string type;
453 };
454 
455 template<> struct invalid_return<long long int> {
456  static const long long int value;
457  static const std::string type;
458 };
459 
460 template<> struct invalid_return<double> {
461  static const double value;
462  static const std::string type;
463 };
464 
465 template<> struct invalid_return<std::string> {
466  static const std::string value;
467  static const std::string type;
468 };
469 
470 template<> struct invalid_return<RGBColor> {
471  static const RGBColor value;
472  static const std::string type;
473 };
474 
475 template<> struct invalid_return<Position> {
476  static const Position value;
477  static const std::string type;
478 };
479 
480 template<> struct invalid_return<PositionVector> {
481  static const PositionVector value;
482  static const std::string type;
483 };
484 
485 template<> struct invalid_return<Boundary> {
486  static const Boundary value;
487  static const std::string type;
488 };
489 
490 template<> struct invalid_return<std::vector<std::string> > {
491  static const std::vector<std::string> value;
492  static const std::string type;
493 };
494 
495 template<> struct invalid_return<std::vector<int> > {
496  static const std::vector<int> value;
497  static const std::string type;
498 };
499 
500 
501 template <typename T>
502 T SUMOSAXAttributes::get(int attr, const char* objectid,
503  bool& ok, bool report) const {
504  if (!hasAttribute(attr)) {
505  if (report) {
506  emitUngivenError(getName(attr), objectid);
507  }
508  ok = false;
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 template <typename T>
528 T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
529  bool& ok, T defaultValue, bool report) const {
530  if (!hasAttribute(attr)) {
531  return defaultValue;
532  }
533  try {
534  return getInternal<T>(attr);
535  } catch (FormatException&) {
536  if (report) {
537  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
538  }
539  } catch (EmptyData&) {
540  if (report) {
541  emitEmptyError(getName(attr), objectid);
542  }
543  }
544  ok = false;
546 }
std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
long long int SUMOTime
Definition: SUMOTime.h:32
FringeType
classifying boundary nodes
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 point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
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 Position getPosition(int attr) const =0
Tries to read given attribute assuming it is a Position.
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
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)=delete
Invalidated assignment operator.
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.
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)=delete
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 Position value
static const std::string type
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