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-2026 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
34#include "SUMOXMLDefinitions.h"
35
36// ===========================================================================
37// class declarations
38// ===========================================================================
39
40class Position;
41class PositionVector;
42class Boundary;
43class RGBColor;
44class StopOffset;
45
46// ===========================================================================
47// class definitions
48// ===========================================================================
58public:
59 /* @brief Constructor
60 * @param[in] tagName The name of the parsed object type; used for error message generation
61 */
62 SUMOSAXAttributes(const std::string& objectType);
63
64
66 virtual ~SUMOSAXAttributes() { }
67
68
82 template <typename T>
83 T get(int attr, const char* objectid, bool& ok, bool report = true) const;
84
85
101 template <typename T>
102 T getOpt(int attr, const char* objectid, bool& ok, T defaultValue = T(), bool report = true) const;
103
104
121 SUMOTime getSUMOTimeReporting(int attr, const char* objectid, bool& ok,
122 bool report = true) const;
123
124
142 SUMOTime getPeriod(const char* objectid, bool& ok, bool report = true) const;
143
144
163 SUMOTime getOptSUMOTimeReporting(int attr, const char* objectid, bool& ok,
164 SUMOTime defaultValue, bool report = true) const;
165
185 SUMOTime getOptOffsetReporting(int attr, const char* objectid, bool& ok,
186 SUMOTime defaultValue, bool report = true) const;
187
207 SUMOTime getOptPeriod(const char* objectid, bool& ok, SUMOTime defaultValue, bool report = true) const;
208
209
210
213
219 virtual bool hasAttribute(int id) const = 0;
220
224 StopOffset getStopOffset(bool& ok) const;
225
226
232 virtual bool hasAttribute(const std::string& id) const = 0;
233
234
250 inline bool getBool(int id) const {
251 return StringUtils::toBool(getString(id));
252 }
253
269 inline int getInt(int id) const {
270 return StringUtils::toInt(getString(id));
271 }
272
273
289 virtual long long int getLong(int id) const {
290 return StringUtils::toLong(getString(id));
291 }
292
293
306 virtual std::string getString(int id, bool* isPresent = nullptr) const = 0;
307
308
321 virtual std::string getStringSecure(int id, const std::string& def) const = 0;
322
323
339 inline double getFloat(int id) const {
341 }
342
343
359 virtual double getFloat(const std::string& id) const = 0;
360
361
371 virtual std::string getStringSecure(const std::string& id,
372 const std::string& def) const = 0;
373 //}
374
375
381 virtual std::string getName(int attr) const = 0;
382
387 virtual void serialize(std::ostream& os) const = 0;
388
391 virtual std::vector<std::string> getAttributeNames() const = 0;
392
394 const std::string& getObjectType() const {
395 return myObjectType;
396 }
397
398 friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
399
401 virtual SUMOSAXAttributes* clone() const = 0;
402
404 static const std::string ENCODING;
405
406
407protected:
408 template <typename T> T fromString(const std::string& value) 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
413private:
416
419
421 std::string myObjectType;
422
423};
424
425
426inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
427 src.serialize(os);
428 return os;
429}
430
431
432template<typename X> struct invalid_return {
433 static const X value;
434};
435
436#define INVALID_RETURN(TYPE) \
437template<> struct invalid_return<TYPE> { \
438 static const TYPE value; \
439}
440INVALID_RETURN(std::string);
442INVALID_RETURN(long long int);
455INVALID_RETURN(std::vector<std::string>);
456INVALID_RETURN(std::vector<int>);
457INVALID_RETURN(std::vector<double>);
458
459
460template <typename T>
461T SUMOSAXAttributes::get(int attr, const char* objectid,
462 bool& ok, bool report) const {
463 try {
464 bool isPresent = true;
465 const std::string& strAttr = getString(attr, &isPresent);
466 if (isPresent) {
467 return fromString<T>(strAttr);
468 }
469 if (report) {
470 emitUngivenError(getName(attr), objectid);
471 }
472 } catch (const FormatException& e) {
473 if (report) {
474 emitFormatError(getName(attr), e.what(), objectid);
475 }
476 } catch (EmptyData&) {
477 if (report) {
478 emitEmptyError(getName(attr), objectid);
479 }
480 }
481 ok = false;
483}
484
485
486template <typename T>
487T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
488 bool& ok, T defaultValue, bool report) const {
489 try {
490 bool isPresent = true;
491 const std::string& strAttr = getString(attr, &isPresent);
492 if (isPresent) {
493 return fromString<T>(strAttr);
494 }
495 return defaultValue;
496 } catch (const FormatException& e) {
497 if (report) {
498 emitFormatError(getName(attr), e.what(), objectid);
499 }
500 } catch (EmptyData&) {
501 if (report) {
502 emitEmptyError(getName(attr), objectid);
503 }
504 }
505 ok = false;
507}
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...
RoundaboutType
classifying roundabout type for nodes
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 getOptOffsetReporting(int attr, const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read given attribute assuming it is a tls offset (SUMOTime or "begin")
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.
StopOffset getStopOffset(bool &ok) const
Parses a stop offset definition (vClasses/exceptions and value) from the attributes.
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.
stop offset
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