SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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 SUMOReal getFloat(int id) const = 0;
275 
276 
292  virtual SUMOReal 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  static const std::string ENCODING;
383 
384 
393  static void parseStringVector(const std::string& def, std::vector<std::string>& into);
394 
395 
404  static void parseStringSet(const std::string& def, std::set<std::string>& into);
405 
406 
407 protected:
408  template <typename T> T getInternal(const int attr) const;
409  void emitUngivenError(const std::string& attrname, const char* objectid) const;
410  void emitEmptyError(const std::string& attrname, const char* objectid) const;
411  void emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const;
412 
413 private:
416 
417 private:
420 
423 
425  std::string myObjectType;
426 
427 };
428 
429 
430 inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
431  src.serialize(os);
432  return os;
433 }
434 
435 
436 template<typename X> struct invalid_return {
437  static const X value;
438  static const std::string type;
439 };
440 
441 
442 template <typename T>
443 T SUMOSAXAttributes::get(int attr, const char* objectid,
444  bool& ok, bool report) const {
445  if (!hasAttribute(attr)) {
446  if (report) {
447  emitUngivenError(getName(attr), objectid);
448  }
449  ok = false;
451  }
452  try {
453  return getInternal<T>(attr);
454  } catch (FormatException&) {
455  if (report) {
456  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
457  }
458  } catch (EmptyData&) {
459  if (report) {
460  emitEmptyError(getName(attr), objectid);
461  }
462  }
463  ok = false;
465 }
466 
467 
468 template <typename T>
469 T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
470  bool& ok, T defaultValue, bool report) const {
471  if (!hasAttribute(attr)) {
472  return defaultValue;
473  }
474  try {
475  return getInternal<T>(attr);
476  } catch (FormatException&) {
477  if (report) {
478  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
479  }
480  } catch (EmptyData&) {
481  if (report) {
482  emitEmptyError(getName(attr), objectid);
483  }
484  }
485  ok = false;
487 }
488 
489 
490 #endif
491 
492 /****************************************************************************/
493 
SUMOSAXAttributes(const std::string &objectType)
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
long long int SUMOTime
Definition: SUMOTime.h:43
static void parseStringSet(const std::string &def, std::set< std::string > &into)
Splits the given string, stores it in a set.
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
void emitUngivenError(const std::string &attrname, const char *objectid) const
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...
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
const std::string & getObjectType() const
return the objecttype to which these attributes belong
static const X value
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
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.
T getInternal(const int attr) const
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.
Encapsulated SAX-Attributes.
A list of positions.
virtual SUMOReal getFloat(int id) const =0
Returns the SUMOReal-value of the named (by its enum-value) attribute.
static const std::string ENCODING
The encoding of parsed strings.
std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
virtual SumoXMLNodeType getNodeType(bool &ok) const =0
Returns the value of the named attribute.
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 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 ...
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
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.
void emitEmptyError(const std::string &attrname, const char *objectid) const
#define SUMOReal
Definition: config.h:214
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.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)
Invalidated assignment operator.
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const