Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
SUMOSAXAttributes.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2007-2024 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
33#include "SUMOXMLDefinitions.h"
34
35
36// ===========================================================================
37// class declarations
38// ===========================================================================
39class Position;
40class PositionVector;
41class Boundary;
42class RGBColor;
43
44
45// ===========================================================================
46// class definitions
47// ===========================================================================
57public:
58 /* @brief Constructor
59 * @param[in] tagName The name of the parsed object type; used for error message generation
60 */
61 SUMOSAXAttributes(const std::string& objectType);
62
63
65 virtual ~SUMOSAXAttributes() { }
66
67
81 template <typename T>
82 T get(int attr, const char* objectid, bool& ok, bool report = true) const;
83
84
100 template <typename T>
101 T getOpt(int attr, const char* objectid, bool& ok, T defaultValue = T(), bool report = true) const;
102
103
120 SUMOTime getSUMOTimeReporting(int attr, const char* objectid, bool& ok,
121 bool report = true) const;
122
123
141 SUMOTime getPeriod(const char* objectid, bool& ok, bool report = true) const;
142
143
162 SUMOTime getOptSUMOTimeReporting(int attr, const char* objectid, bool& ok,
163 SUMOTime defaultValue, bool report = true) const;
164
165
185 SUMOTime getOptPeriod(const char* objectid, bool& ok, SUMOTime defaultValue, bool report = true) const;
186
187
188
191
197 virtual bool hasAttribute(int id) const = 0;
198
199
205 virtual bool hasAttribute(const std::string& id) const = 0;
206
207
223 inline bool getBool(int id) const {
224 return StringUtils::toBool(getString(id));
225 }
226
242 inline int getInt(int id) const {
243 return StringUtils::toInt(getString(id));
244 }
245
246
262 virtual long long int getLong(int id) const {
263 return StringUtils::toLong(getString(id));
264 }
265
266
279 virtual std::string getString(int id, bool* isPresent = nullptr) const = 0;
280
281
294 virtual std::string getStringSecure(int id, const std::string& def) const = 0;
295
296
312 inline double getFloat(int id) const {
314 }
315
316
332 virtual double getFloat(const std::string& id) const = 0;
333
334
344 virtual std::string getStringSecure(const std::string& id,
345 const std::string& def) const = 0;
346 //}
347
348
354 virtual std::string getName(int attr) const = 0;
355
360 virtual void serialize(std::ostream& os) const = 0;
361
364 virtual std::vector<std::string> getAttributeNames() const = 0;
365
367 const std::string& getObjectType() const {
368 return myObjectType;
369 }
370
371 friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
372
374 virtual SUMOSAXAttributes* clone() const = 0;
375
377 static const std::string ENCODING;
378
379
380protected:
381 template <typename T> T fromString(const std::string& value) const;
382 void emitUngivenError(const std::string& attrname, const char* objectid) const;
383 void emitEmptyError(const std::string& attrname, const char* objectid) const;
384 void emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const;
385
386private:
389
392
394 std::string myObjectType;
395
396};
397
398
399inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
400 src.serialize(os);
401 return os;
402}
403
404
405template<typename X> struct invalid_return {
406 static const X value;
407};
408
409#define INVALID_RETURN(TYPE) \
410template<> struct invalid_return<TYPE> { \
411 static const TYPE value; \
412}
413INVALID_RETURN(std::string);
415INVALID_RETURN(long long int);
427INVALID_RETURN(std::vector<std::string>);
428INVALID_RETURN(std::vector<int>);
429INVALID_RETURN(std::vector<double>);
430
431
432template <typename T>
433T SUMOSAXAttributes::get(int attr, const char* objectid,
434 bool& ok, bool report) const {
435 try {
436 bool isPresent = true;
437 const std::string& strAttr = getString(attr, &isPresent);
438 if (isPresent) {
439 return fromString<T>(strAttr);
440 }
441 if (report) {
442 emitUngivenError(getName(attr), objectid);
443 }
444 } catch (const FormatException& e) {
445 if (report) {
446 emitFormatError(getName(attr), e.what(), objectid);
447 }
448 } catch (EmptyData&) {
449 if (report) {
450 emitEmptyError(getName(attr), objectid);
451 }
452 }
453 ok = false;
455}
456
457
458template <typename T>
459T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
460 bool& ok, T defaultValue, bool report) const {
461 try {
462 bool isPresent = true;
463 const std::string& strAttr = getString(attr, &isPresent);
464 if (isPresent) {
465 return fromString<T>(strAttr);
466 }
467 return defaultValue;
468 } catch (const FormatException& e) {
469 if (report) {
470 emitFormatError(getName(attr), e.what(), objectid);
471 }
472 } catch (EmptyData&) {
473 if (report) {
474 emitEmptyError(getName(attr), objectid);
475 }
476 }
477 ok = false;
479}
long long int SUMOTime
Definition GUI.h:36
std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
#define INVALID_RETURN(TYPE)
ParkingType
Numbers representing special SUMO-XML-attribute values Information on whether a car is parking on the...
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 std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) 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.
friend std::ostream & operator<<(std::ostream &os, const SUMOSAXAttributes &src)
SUMOTime getOptPeriod(const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read the SUMOTime 'period' attribute.
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
virtual void serialize(std::ostream &os) const =0
Prints all attribute names and values into the given stream.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
virtual ~SUMOSAXAttributes()
Destructor.
T fromString(const std::string &value) const
virtual long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
virtual std::vector< std::string > getAttributeNames() const =0
Retrieves all attribute names.
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.
SUMOSAXAttributes(const SUMOSAXAttributes &src)=delete
Invalidated copy constructor.
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 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.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
void emitUngivenError(const std::string &attrname, const char *objectid) const
virtual SUMOSAXAttributes * clone() const =0
return a new deep-copy attributes object
const std::string & getObjectType() const
return the objecttype to which these attributes belong
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.
SUMOTime getPeriod(const char *objectid, bool &ok, bool report=true) const
Tries to read the SUMOTime 'period' attribute.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
SUMOSAXAttributes & operator=(const SUMOSAXAttributes &src)=delete
Invalidated assignment operator.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
static long long int toLong(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter,...
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter
static const X value