SUMO - Simulation of Urban MObility
GNEAttributeCarrier.h
Go to the documentation of this file.
1 /****************************************************************************/
7 // Abstract Base class for gui objects which carry attributes
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 #ifndef GNEAttributeCarrier_h
21 #define GNEAttributeCarrier_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <vector>
35 #include <map>
39 #include <utils/common/RGBColor.h>
41 #include <utils/common/ToString.h>
44 #include "GNEReferenceCounter.h"
45 
46 
47 // ===========================================================================
48 // class declarations
49 // ===========================================================================
50 class GNENet;
51 class GNEUndoList;
52 
53 // ===========================================================================
54 // class definitions
55 // ===========================================================================
64  friend class GNEChange_Attribute;
65 
66 public:
72 
74  virtual ~GNEAttributeCarrier() {};
75 
78  /* @brief method for getting the Attribute of an XML key
79  * @param[in] key The attribute key
80  * @return string with the value associated to key
81  */
82  virtual std::string getAttribute(SumoXMLAttr key) const = 0;
83 
84  /* @brief method for setting the attribute and letting the object perform additional changes
85  * @param[in] key The attribute key
86  * @param[in] value The new value
87  * @param[in] undoList The undoList on which to register changes
88  * @param[in] net optionally the GNENet to inform about gui updates
89  */
90  virtual void setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) = 0;
91 
92  /* @brief method for setting the attribute and letting the object perform additional changes
93  * @param[in] key The attribute key
94  * @param[in] value The new value
95  * @param[in] undoList The undoList on which to register changes
96  */
97  virtual bool isValid(SumoXMLAttr key, const std::string& value) = 0;
99 
101  virtual std::string getAttributeForSelection(SumoXMLAttr key) const;
102 
104  SumoXMLTag getTag() const;
105 
107  FXIcon* getIcon() const;
108 
110  GUIIcon getGUIIcon() const;
111 
113  std::vector<SumoXMLAttr> getAttrs() const;
114 
116  const std::string getID() const;
117 
119  static std::string getAttributeType(SumoXMLTag tag, SumoXMLAttr attr);
120 
122  static const std::vector<std::pair<SumoXMLAttr, std::string> >& allowedAttributes(SumoXMLTag tag);
123 
125  static std::vector<SumoXMLTag> allowedTags();
126 
128  static const std::vector<SumoXMLTag>& allowedNetElementsTags();
129 
131  static const std::vector<SumoXMLTag>& allowedAdditionalTags();
132 
134  static const std::vector<SumoXMLTag>& allowedShapeTags();
135 
137  static bool canBlockMovement(SumoXMLTag tag);
138 
140  static bool canBlockShape(SumoXMLTag tag);
141 
143  static bool isNumerical(SumoXMLTag tag, SumoXMLAttr attr);
144 
146  static bool isInt(SumoXMLTag tag, SumoXMLAttr attr);
147 
149  static bool isFloat(SumoXMLTag tag, SumoXMLAttr attr);
150 
152  static bool isTime(SumoXMLTag tag, SumoXMLAttr attr);
153 
155  static bool isBool(SumoXMLTag tag, SumoXMLAttr attr);
156 
158  static bool isColor(SumoXMLTag tag, SumoXMLAttr attr);
159 
161  static bool isString(SumoXMLTag tag, SumoXMLAttr attr);
162 
164  static bool isList(SumoXMLTag tag, SumoXMLAttr attr);
165 
167  static bool isUnique(SumoXMLTag tag, SumoXMLAttr attr);
168 
170  static bool isDiscrete(SumoXMLTag tag, SumoXMLAttr attr);
171 
173  static bool isPositive(SumoXMLTag tag, SumoXMLAttr attr);
174 
176  static bool isProbability(SumoXMLTag tag, SumoXMLAttr attr);
177 
179  static bool isFilename(SumoXMLTag tag, SumoXMLAttr attr);
180 
182  static bool isNonEditable(SumoXMLTag tag, SumoXMLAttr attr);
183 
185  static bool hasAttribute(SumoXMLTag tag, SumoXMLAttr attr);
186 
188  static bool hasDefaultValue(SumoXMLTag tag, SumoXMLAttr attr);
189 
191  static const std::vector<std::string>& discreteChoices(SumoXMLTag tag, SumoXMLAttr attr);
192 
194  static bool discreteCombinableChoices(SumoXMLTag tag, SumoXMLAttr attr);
195 
197  static std::string getDefinition(SumoXMLTag tag, SumoXMLAttr attr);
198 
200  static int getHigherNumberOfAttributes();
201 
204  template<typename T>
205  static T getDefaultValue(SumoXMLTag tag, SumoXMLAttr attr);
206 
208  template<typename T>
209  static bool canParse(const std::string& string) {
210  try {
211  parse<T>(string);
212  } catch (NumberFormatException&) {
213  return false;
214  } catch (EmptyData&) {
215  return false;
216  } catch (BoolFormatException&) {
217  return false;
218  }
219  return true;
220  }
221 
223  template<typename T>
224  static T parse(const std::string& string);
225 
227  template<typename T>
228  static bool isPositive(const std::string& string) {
229  return canParse<T>(string) && parse<T>(string) > 0;
230  }
231 
233  static bool isValidID(const std::string& value);
234 
236  static bool isValidFilename(const std::string& value);
237 
239  static const std::string LOADED;
240 
242  static const std::string GUESSED;
243 
245  static const std::string MODIFIED;
246 
248  static const std::string APPROVED;
249 
251  static const double INVALID_POSITION;
252 
254  template <typename T>
255  static T parseAttributeFromXML(const SUMOSAXAttributes& attrs, const char* objectid, const SumoXMLTag tag, const SumoXMLAttr attribute, bool& abort, bool report = true) {
256  bool ok = true;
257  std::string parsedAttribute = "0";
258  // only show one warning for every error/warning loading additional
259  if (!abort) {
260  // set additionalOfWarningMessage
261  std::string additionalOfWarningMessage;
262  if (objectid) {
263  additionalOfWarningMessage = toString(tag) + " with ID '" + toString(objectid) + "'";
264  } else {
265  additionalOfWarningMessage = toString(tag);
266  }
267  // first check that attribute exists
268  if (attrs.hasAttribute(attribute)) {
269  // Parse attribute as string
270  parsedAttribute = attrs.get<std::string>(attribute, objectid, ok, false);
271  // check that parsed attribute can be converted to type T
272  if (ok && !canParse<T>(parsedAttribute)) {
273  ok = false;
274  }
275  std::string errorFormat;
276  // Set extra checks for int values
277  if (isInt(tag, attribute)) {
278  if (canParse<int>(parsedAttribute)) {
279  // parse to int and check if can be negative
280  int parsedIntAttribute = parse<int>(parsedAttribute);
281  if (isPositive(tag, attribute) && parsedIntAttribute < 0) {
282  errorFormat = "Cannot be negative; ";
283  ok = false;
284  }
285  } else {
286  errorFormat = "Cannot be parsed to int; ";
287  ok = false;
288  }
289  }
290  // Set extra checks for float(double) values
291  if (isFloat(tag, attribute)) {
292  if (canParse<double>(parsedAttribute)) {
293  // parse to double and check if can be negative
294  double parsedSumoRealAttribute = parse<double>(parsedAttribute);
295  if (isPositive(tag, attribute) && parsedSumoRealAttribute < 0) {
296  errorFormat = "Cannot be negative; ";
297  ok = false;
298  }
299  } else {
300  errorFormat = "Cannot be parsed to float; ";
301  ok = false;
302  }
303  }
304  // set extra check for time(double) values
305  if (isTime(tag, attribute)) {
306  if (canParse<double>(parsedAttribute)) {
307  // parse to SUMO Real and check if is negative
308  double parsedSumoRealAttribute = parse<double>(parsedAttribute);
309  if (parsedSumoRealAttribute < 0) {
310  errorFormat = "Time cannot be negative; ";
311  ok = false;
312  }
313  } else {
314  errorFormat = "Cannot be parsed to time; ";
315  ok = false;
316  }
317  }
318  // set extra check for color values
319  if (isColor(tag, attribute) && !canParse<RGBColor>(parsedAttribute)) {
320  errorFormat = "Invalid RGB format or named color; ";
321  ok = false;
322  }
323  // set extra check for filename values
324  if (isFilename(tag, attribute) && (isValidFilename(parsedAttribute) == false)) {
325  errorFormat = "Filename contains invalid characters; ";
326  ok = false;
327  }
328  // set extra check for Vehicle Classes
329  if ((!ok) && (attribute == SUMO_ATTR_VCLASS)) {
330  errorFormat = "Is not a part of defined set of Vehicle Classes; ";
331  }
332  // set extra check for Vehicle Classes
333  if ((!ok) && (attribute == SUMO_ATTR_GUISHAPE)) {
334  errorFormat = "Is not a part of defined set of Gui Vehicle Shapes; ";
335  }
336  // If attribute has an invalid format
337  if (!ok) {
338  // if attribute has a default value, take it as string. In other case, abort.
339  if (hasDefaultValue(tag, attribute)) {
340  parsedAttribute = toString(getDefaultValue<T>(tag, attribute));
341  // report warning of default value
342  if (report) {
343  WRITE_WARNING("Format of optional " + getAttributeType(tag, attribute) + " attribute '" + toString(attribute) + "' of " +
344  additionalOfWarningMessage + " is invalid; " + errorFormat + "Default value '" + toString(parsedAttribute) + "' will be used.");
345  }
346  } else {
347  WRITE_WARNING("Format of essential " + getAttributeType(tag, attribute) + " attribute '" + toString(attribute) + "' of " +
348  additionalOfWarningMessage + " is invalid; " + errorFormat + "" + toString(tag) + " cannot be created");
349  // set default value of parsedAttribute (to avoid exceptions during conversions)
350  parsedAttribute = "0";
351  abort = true;
352  }
353  }
354  } else {
355  // if attribute has a default value, take it. In other case, abort.
356  if (hasDefaultValue(tag, attribute)) {
357  parsedAttribute = toString(getDefaultValue<T>(tag, attribute));
358  // report warning of default value
359  if (report) {
360  WRITE_WARNING("Optional " + getAttributeType(tag, attribute) + " attribute '" + toString(attribute) + "' of " +
361  additionalOfWarningMessage + " is missing; Default value '" + toString(parsedAttribute) + "' will be used.");
362  }
363  } else {
364  WRITE_WARNING("Essential " + getAttributeType(tag, attribute) + " attribute '" + toString(attribute) + "' of " +
365  additionalOfWarningMessage + " is missing; " + toString(tag) + " cannot be created");
366  abort = true;
367  }
368  }
369  }
370  // return parsed attribute
371  return parse<T>(parsedAttribute);
372  }
373 
374 
375 private:
377  virtual void setAttribute(SumoXMLAttr key, const std::string& value) = 0;
378 
381 
384 
386  static std::map<SumoXMLTag, std::vector<std::pair <SumoXMLAttr, std::string> > > _allowedAttributes;
387 
389  static std::vector<SumoXMLTag> myAllowedNetElementTags;
390 
392  static std::vector<SumoXMLTag> myAllowedAdditionalTags;
393 
395  static std::vector<SumoXMLTag> myAllowedShapeTags;
396 
398  static std::vector<SumoXMLTag> myBlockMovementTags;
399 
401  static std::vector<SumoXMLTag> myBlockShapeTags;
402 
404  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myNumericalIntAttrs;
405 
407  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myNumericalFloatAttrs;
408 
410  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myTimeAttrs;
411 
413  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myBoolAttrs;
414 
416  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myColorAttrs;
417 
419  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myListAttrs;
420 
422  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myUniqueAttrs;
423 
425  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myNonEditableAttrs;
426 
428  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myPositiveAttrs;
429 
431  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myProbabilityAttrs;
432 
434  static std::map<SumoXMLTag, std::set<SumoXMLAttr> > myFileAttrs;
435 
437  static std::map<SumoXMLTag, SumoXMLTag> myAllowedAdditionalWithParentTags;
438 
440  static std::map<SumoXMLTag, std::map<SumoXMLAttr, std::vector<std::string> > > myDiscreteChoices;
441 
443  static std::map<SumoXMLTag, std::map<SumoXMLAttr, std::string> > myAttrDefinitions;
444 
446  static int myMaxNumAttribute;
447 
449  GNEAttributeCarrier& operator=(const GNEAttributeCarrier& src) = delete;
450 };
451 
452 #endif
453 
454 /****************************************************************************/
455 
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myColorAttrs
map with the color attributes
SumoXMLTag
Numbers representing SUMO-XML - element names.
static bool isPositive(const std::string &string)
true if a positive number of type T can be parsed from string
static bool isFloat(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is numerical of type float
static std::string getAttributeType(SumoXMLTag tag, SumoXMLAttr attr)
get type of attribute
GNEAttributeCarrier(SumoXMLTag tag, GUIIcon icon)
Constructor.
const SumoXMLTag myTag
the xml tag to which this attribute carrier corresponds
std::vector< SumoXMLAttr > getAttrs() const
get vector of attributes
GNEAttributeCarrier & operator=(const GNEAttributeCarrier &src)=delete
Invalidated assignment operator.
static std::map< SumoXMLTag, std::map< SumoXMLAttr, std::string > > myAttrDefinitions
map with the definition of attributes
static bool isDiscrete(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is Discrete
GUIIcon
An enumeration of icons used by the gui applications.
Definition: GUIIcons.h:43
static std::vector< SumoXMLTag > myAllowedShapeTags
vector with the allowed tags of shapes
static const std::vector< SumoXMLTag > & allowedShapeTags()
get all editable for tag shape elements
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myPositiveAttrs
map with the positive attributes
static const std::vector< SumoXMLTag > & allowedNetElementsTags()
get all editable for tag net elements
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myUniqueAttrs
map with the unique attributes (i.e. attributes without default values)
static std::map< SumoXMLTag, std::map< SumoXMLAttr, std::vector< std::string > > > myDiscreteChoices
map with the values of discrete choices
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myBoolAttrs
map with the boolean attributes
static const std::string LOADED
feature is still unchanged after being loaded (implies approval)
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myProbabilityAttrs
map with the probability attributes
static int getHigherNumberOfAttributes()
return the number of attributes of the tag with the most highter number of attributes ...
GUIIcon myIcon
icon associated to this AC
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myTimeAttrs
map with the attributes of type time
static bool isPositive(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is only Positive (i.e. cannot take negative values)
A NBNetBuilder extended by visualisation and editing capabilities.
Definition: GNENet.h:92
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static std::map< SumoXMLTag, std::vector< std::pair< SumoXMLAttr, std::string > > > _allowedAttributes
map with the allowed attributes and their default values
static std::vector< SumoXMLTag > myBlockShapeTags
vector with the allowed tags that can block their shapes
static bool isValidFilename(const std::string &value)
true if value is a valid file value
virtual void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)=0
static const std::vector< std::pair< SumoXMLAttr, std::string > > & allowedAttributes(SumoXMLTag tag)
get all editable attributes for tag and their default values.
static T parseAttributeFromXML(const SUMOSAXAttributes &attrs, const char *objectid, const SumoXMLTag tag, const SumoXMLAttr attribute, bool &abort, bool report=true)
Parse attribute from XML and show warnings if there are problems parsing it.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static std::vector< SumoXMLTag > myAllowedNetElementTags
vector with the allowed tags of netElements
static bool isString(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is of type string
static std::vector< SumoXMLTag > allowedTags()
get all editable for tag elements of all types
static const std::vector< std::string > & discreteChoices(SumoXMLTag tag, SumoXMLAttr attr)
return a list of discrete choices for this attribute or an empty vector
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myListAttrs
map with the attributes of type list
static bool isInt(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is numerical or type int
the function-object for an editing operation (abstract base)
static bool isValidID(const std::string &value)
true if value is a valid sumo ID
static const std::string MODIFIED
feature has been manually modified (implies approval)
static bool isTime(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is time
static bool isNonEditable(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is non editable
static std::vector< SumoXMLTag > myBlockMovementTags
vector with the allowed tags that can block their movement
static std::vector< SumoXMLTag > myAllowedAdditionalTags
vector with the allowed tags of additionals
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
static bool hasAttribute(SumoXMLTag tag, SumoXMLAttr attr)
check if an element with certain tag has a certain attribute
virtual bool isValid(SumoXMLAttr key, const std::string &value)=0
static bool canBlockMovement(SumoXMLTag tag)
return true if element tag can block their movement
Encapsulated SAX-Attributes.
static bool hasDefaultValue(SumoXMLTag tag, SumoXMLAttr attr)
check if attribute of an element has a default avlue
static const double INVALID_POSITION
default value for invalid positions (used by POIs and Polygons)
static bool isColor(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is of type color for a certain tag
static bool isFilename(SumoXMLTag tag, SumoXMLAttr attr)
whether a string attribute is a filename
static T getDefaultValue(SumoXMLTag tag, SumoXMLAttr attr)
return the default value of the attribute of an element
const std::string getID() const
function to support debugging
virtual std::string getAttributeForSelection(SumoXMLAttr key) const
method for getting the attribute in the context of object selection
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myNonEditableAttrs
map with the non-editable attributes
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
virtual ~GNEAttributeCarrier()
Destructor.
static bool isUnique(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is unique (may not be edited for a multi-selection and don&#39;t have a default valu...
static bool canParse(const std::string &string)
true if a number of type T can be parsed from string
static int myMaxNumAttribute
maximum number of attributes of all tags
FXIcon * getIcon() const
get FXIcon assigned to this object
static bool isProbability(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is a probability (i.e. oly can values between [0, 1])
static const std::vector< SumoXMLTag > & allowedAdditionalTags()
get all editable for tag additional elements
virtual std::string getAttribute(SumoXMLAttr key) const =0
This functions has to be implemented in allGNEAttributeCarrier.
static const std::string APPROVED
feature has been approved but not changed (i.e. after being reguessed)
static bool isBool(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is of type bool for a certain tag
static std::string getDefinition(SumoXMLTag tag, SumoXMLAttr attr)
return definition of a certain SumoXMLAttr
static bool discreteCombinableChoices(SumoXMLTag tag, SumoXMLAttr attr)
return whether the given attribute allows for a combination of discrete values
SumoXMLTag getTag() const
get XML Tag assigned to this object
static const std::string GUESSED
feature has been reguessed (may still be unchanged be we can&#39;t tell (yet)
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
static bool isNumerical(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is numerical (int or float)
GUIIcon getGUIIcon() const
get GUI icon assigned to this object
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myNumericalIntAttrs
map with the numerical attributes of type Int
static T parse(const std::string &string)
parses a number of type T from string
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myNumericalFloatAttrs
map with the numerical attributes of type Float
static bool canBlockShape(SumoXMLTag tag)
return true if element tag can block their shape
static std::map< SumoXMLTag, SumoXMLTag > myAllowedAdditionalWithParentTags
map with the allowed tags of additionals with parent and their parent
static bool isList(SumoXMLTag tag, SumoXMLAttr attr)
whether an attribute is of type bool
static std::map< SumoXMLTag, std::set< SumoXMLAttr > > myFileAttrs
map with the empty attributes