SUMO - Simulation of Urban MObility
SUMOSAXAttributes.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Encapsulated SAX-Attributes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2007-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef SUMOSAXAttributes_h
23 #define SUMOSAXAttributes_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <vector>
37 #include <set>
38 
39 #include <utils/common/SUMOTime.h>
40 #include <utils/common/ToString.h>
42 #include "SUMOXMLDefinitions.h"
43 
44 
45 // ===========================================================================
46 // class declarations
47 // ===========================================================================
48 class PositionVector;
49 class Boundary;
50 class RGBColor;
51 
52 
53 // ===========================================================================
54 // class definitions
55 // ===========================================================================
65 public:
66  /* @brief Constructor
67  * @param[in] tagName The name of the parsed object type; used for error message generation
68  */
69  SUMOSAXAttributes(const std::string& objectType);
70 
71 
73  virtual ~SUMOSAXAttributes() { }
74 
75 
89  template <typename T>
90  T get(int attr, const char* objectid, bool& ok, bool report = true) const;
91 
92 
108  template <typename T>
109  T getOpt(int attr, const char* objectid, bool& ok, T defaultValue, bool report = true) const;
110 
111 
128  SUMOTime getSUMOTimeReporting(int attr, const char* objectid, bool& ok,
129  bool report = true) const;
130 
131 
132 
151  SUMOTime getOptSUMOTimeReporting(int attr, const char* objectid, bool& ok,
152  SUMOTime defaultValue, bool report = true) const;
153 
154 
155 
158 
164  virtual bool hasAttribute(int id) const = 0;
165 
166 
172  virtual bool hasAttribute(const std::string& id) const = 0;
173 
174 
190  virtual bool getBool(int id) const = 0;
191 
207  virtual int getInt(int id) const = 0;
208 
209 
225  virtual long long int getLong(int id) const = 0;
226 
227 
240  virtual std::string getString(int id) const = 0;
241 
242 
255  virtual std::string getStringSecure(int id,
256  const std::string& def) const = 0;
257 
258 
274  virtual double getFloat(int id) const = 0;
275 
276 
292  virtual double getFloat(const std::string& id) const = 0;
293 
294 
304  virtual std::string getStringSecure(const std::string& id,
305  const std::string& def) const = 0;
306 
307 
314  virtual SumoXMLEdgeFunc getEdgeFunc(bool& ok) const = 0;
315 
316 
323  virtual SumoXMLNodeType getNodeType(bool& ok) const = 0;
324 
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 
354  virtual std::vector<std::string> getStringVector(int attr) const = 0;
355  //}
356 
357 
363  virtual std::string getName(int attr) const = 0;
364 
365 
370  virtual void serialize(std::ostream& os) const = 0;
371 
372 
374  const std::string& getObjectType() const {
375  return myObjectType;
376  }
377 
378 
379  friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
380 
382  virtual SUMOSAXAttributes* clone() const = 0;
383 
385  static const std::string ENCODING;
386 
387 
396  static void parseStringVector(const std::string& def, std::vector<std::string>& into);
397 
398 
407  static void parseStringSet(const std::string& def, std::set<std::string>& into);
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 
420 private:
423 
426 
428  std::string myObjectType;
429 
430 };
431 
432 
433 inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
434  src.serialize(os);
435  return os;
436 }
437 
438 
439 template<typename X> struct invalid_return {
440  static const X value;
441  static const std::string type;
442 };
443 
444 
445 template <typename T>
446 T SUMOSAXAttributes::get(int attr, const char* objectid,
447  bool& ok, bool report) const {
448  if (!hasAttribute(attr)) {
449  if (report) {
450  emitUngivenError(getName(attr), objectid);
451  }
452  ok = false;
454  }
455  try {
456  return getInternal<T>(attr);
457  } catch (FormatException&) {
458  if (report) {
459  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
460  }
461  } catch (EmptyData&) {
462  if (report) {
463  emitEmptyError(getName(attr), objectid);
464  }
465  }
466  ok = false;
468 }
469 
470 
471 template <typename T>
472 T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
473  bool& ok, T defaultValue, bool report) const {
474  if (!hasAttribute(attr)) {
475  return defaultValue;
476  }
477  try {
478  return getInternal<T>(attr);
479  } catch (FormatException&) {
480  if (report) {
481  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
482  }
483  } catch (EmptyData&) {
484  if (report) {
485  emitEmptyError(getName(attr), objectid);
486  }
487  }
488  ok = false;
490 }
491 
492 
493 #endif
494 
495 /****************************************************************************/
496 
SUMOSAXAttributes(const std::string &objectType)
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
static void parseStringSet(const std::string &def, std::set< std::string > &into)
Splits the given string, stores it in a set.
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
const std::string & getObjectType() const
return the objecttype to which these attributes belong
static const std::string type
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:48
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 void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
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
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.
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.
static bool myHaveInformedAboutDeprecatedDivider
Information whether the usage of a deprecated divider was reported.
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...
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.
long long int SUMOTime
Definition: TraCIDefs.h:52
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)
Invalidated assignment operator.