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-2025 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;
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
184 SUMOTime getOptOffsetReporting(int attr, const char* objectid, bool& ok,
185 SUMOTime defaultValue, bool report = true) const;
186
206 SUMOTime getOptPeriod(const char* objectid, bool& ok, SUMOTime defaultValue, bool report = true) const;
207
208
209
212
218 virtual bool hasAttribute(int id) const = 0;
219
220
226 virtual bool hasAttribute(const std::string& id) const = 0;
227
228
244 inline bool getBool(int id) const {
245 return StringUtils::toBool(getString(id));
246 }
247
263 inline int getInt(int id) const {
264 return StringUtils::toInt(getString(id));
265 }
266
267
283 virtual long long int getLong(int id) const {
284 return StringUtils::toLong(getString(id));
285 }
286
287
300 virtual std::string getString(int id, bool* isPresent = nullptr) const = 0;
301
302
315 virtual std::string getStringSecure(int id, const std::string& def) const = 0;
316
317
333 inline double getFloat(int id) const {
335 }
336
337
353 virtual double getFloat(const std::string& id) const = 0;
354
355
365 virtual std::string getStringSecure(const std::string& id,
366 const std::string& def) const = 0;
367 //}
368
369
375 virtual std::string getName(int attr) const = 0;
376
381 virtual void serialize(std::ostream& os) const = 0;
382
385 virtual std::vector<std::string> getAttributeNames() const = 0;
386
388 const std::string& getObjectType() const {
389 return myObjectType;
390 }
391
392 friend std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src);
393
395 virtual SUMOSAXAttributes* clone() const = 0;
396
398 static const std::string ENCODING;
399
400
401protected:
402 template <typename T> T fromString(const std::string& value) const;
403 void emitUngivenError(const std::string& attrname, const char* objectid) const;
404 void emitEmptyError(const std::string& attrname, const char* objectid) const;
405 void emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const;
406
407private:
410
413
415 std::string myObjectType;
416
417};
418
419
420inline std::ostream& operator<<(std::ostream& os, const SUMOSAXAttributes& src) {
421 src.serialize(os);
422 return os;
423}
424
425
426template<typename X> struct invalid_return {
427 static const X value;
428};
429
430#define INVALID_RETURN(TYPE) \
431template<> struct invalid_return<TYPE> { \
432 static const TYPE value; \
433}
434INVALID_RETURN(std::string);
436INVALID_RETURN(long long int);
448INVALID_RETURN(std::vector<std::string>);
449INVALID_RETURN(std::vector<int>);
450INVALID_RETURN(std::vector<double>);
451
452
453template <typename T>
454T SUMOSAXAttributes::get(int attr, const char* objectid,
455 bool& ok, bool report) const {
456 try {
457 bool isPresent = true;
458 const std::string& strAttr = getString(attr, &isPresent);
459 if (isPresent) {
460 return fromString<T>(strAttr);
461 }
462 if (report) {
463 emitUngivenError(getName(attr), objectid);
464 }
465 } catch (const FormatException& e) {
466 if (report) {
467 emitFormatError(getName(attr), e.what(), objectid);
468 }
469 } catch (EmptyData&) {
470 if (report) {
471 emitEmptyError(getName(attr), objectid);
472 }
473 }
474 ok = false;
476}
477
478
479template <typename T>
480T SUMOSAXAttributes::getOpt(int attr, const char* objectid,
481 bool& ok, T defaultValue, bool report) const {
482 try {
483 bool isPresent = true;
484 const std::string& strAttr = getString(attr, &isPresent);
485 if (isPresent) {
486 return fromString<T>(strAttr);
487 }
488 return defaultValue;
489 } catch (const FormatException& e) {
490 if (report) {
491 emitFormatError(getName(attr), e.what(), objectid);
492 }
493 } catch (EmptyData&) {
494 if (report) {
495 emitEmptyError(getName(attr), objectid);
496 }
497 }
498 ok = false;
500}
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 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.
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