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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Encapsulated SAX-Attributes
20 /****************************************************************************/
21 #ifndef SUMOSAXAttributes_h
22 #define SUMOSAXAttributes_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <vector>
36 #include <set>
37 
38 #include <utils/common/SUMOTime.h>
39 #include <utils/common/ToString.h>
41 #include "SUMOXMLDefinitions.h"
42 
43 
44 // ===========================================================================
45 // class declarations
46 // ===========================================================================
47 class PositionVector;
48 class Boundary;
49 class RGBColor;
50 
51 
52 // ===========================================================================
53 // class definitions
54 // ===========================================================================
64 public:
65  /* @brief Constructor
66  * @param[in] tagName The name of the parsed object type; used for error message generation
67  */
68  SUMOSAXAttributes(const std::string& objectType);
69 
70 
72  virtual ~SUMOSAXAttributes() { }
73 
74 
88  template <typename T>
89  T get(int attr, const char* objectid, bool& ok, bool report = true) const;
90 
91 
107  template <typename T>
108  T getOpt(int attr, const char* objectid, bool& ok, T defaultValue, bool report = true) const;
109 
110 
127  SUMOTime getSUMOTimeReporting(int attr, const char* objectid, bool& ok,
128  bool report = true) const;
129 
130 
131 
150  SUMOTime getOptSUMOTimeReporting(int attr, const char* objectid, bool& ok,
151  SUMOTime defaultValue, bool report = true) const;
152 
153 
154 
157 
163  virtual bool hasAttribute(int id) const = 0;
164 
165 
171  virtual bool hasAttribute(const std::string& id) const = 0;
172 
173 
189  virtual bool getBool(int id) const = 0;
190 
206  virtual int getInt(int id) const = 0;
207 
208 
224  virtual long long int getLong(int id) const = 0;
225 
226 
239  virtual std::string getString(int id) const = 0;
240 
241 
254  virtual std::string getStringSecure(int id,
255  const std::string& def) const = 0;
256 
257 
273  virtual double getFloat(int id) const = 0;
274 
275 
291  virtual double getFloat(const std::string& id) const = 0;
292 
293 
303  virtual std::string getStringSecure(const std::string& id,
304  const std::string& def) const = 0;
305 
306 
313  virtual SumoXMLEdgeFunc getEdgeFunc(bool& ok) const = 0;
314 
315 
322  virtual SumoXMLNodeType getNodeType(bool& ok) const = 0;
323 
324 
331  virtual RGBColor getColor() const = 0;
332 
333 
339  virtual PositionVector getShape(int attr) const = 0;
340 
346  virtual Boundary getBoundary(int attr) const = 0;
347 
353  virtual std::vector<std::string> getStringVector(int attr) const = 0;
354  //}
355 
356 
362  virtual std::string getName(int attr) const = 0;
363 
364 
369  virtual void serialize(std::ostream& os) const = 0;
370 
371 
373  const std::string& getObjectType() const {
374  return myObjectType;
375  }
376 
377 
378  friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
379 
381  virtual SUMOSAXAttributes* clone() const = 0;
382 
384  static const std::string ENCODING;
385 
386 
395  static void parseStringVector(const std::string& def, std::vector<std::string>& into);
396 
397 
406  static void parseStringSet(const std::string& def, std::set<std::string>& into);
407 
408 
409 protected:
410  template <typename T> T getInternal(const int attr) const;
411  void emitUngivenError(const std::string& attrname, const char* objectid) const;
412  void emitEmptyError(const std::string& attrname, const char* objectid) const;
413  void emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const;
414 
415 private:
418 
419 private:
422 
425 
427  std::string myObjectType;
428 
429 };
430 
431 
432 inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
433  src.serialize(os);
434  return os;
435 }
436 
437 
438 template<typename X> struct invalid_return {
439  static const X value;
440  static const std::string type;
441 };
442 
443 
444 template <typename T>
445 T SUMOSAXAttributes::get(int attr, const char* objectid,
446  bool& ok, bool report) const {
447  if (!hasAttribute(attr)) {
448  if (report) {
449  emitUngivenError(getName(attr), objectid);
450  }
451  ok = false;
453  }
454  try {
455  return getInternal<T>(attr);
456  } catch (FormatException&) {
457  if (report) {
458  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
459  }
460  } catch (EmptyData&) {
461  if (report) {
462  emitEmptyError(getName(attr), objectid);
463  }
464  }
465  ok = false;
467 }
468 
469 
470 template <typename T>
471 T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
472  bool& ok, T defaultValue, bool report) const {
473  if (!hasAttribute(attr)) {
474  return defaultValue;
475  }
476  try {
477  return getInternal<T>(attr);
478  } catch (FormatException&) {
479  if (report) {
480  emitFormatError(getName(attr), "of type " + invalid_return<T>::type, objectid);
481  }
482  } catch (EmptyData&) {
483  if (report) {
484  emitEmptyError(getName(attr), objectid);
485  }
486  }
487  ok = false;
489 }
490 
491 
492 #endif
493 
494 /****************************************************************************/
495 
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:47
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:51
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)
Invalidated assignment operator.