Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/xml/SUMOSAXAttributesImpl_Cached.h
169678 views
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
/****************************************************************************/
14
/// @file SUMOSAXAttributesImpl_Cached.h
15
/// @author Jakob Erdmann
16
/// @date Dec 2016
17
///
18
// Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend
19
/****************************************************************************/
20
#pragma once
21
#include <config.h>
22
23
#include <string>
24
#include <map>
25
#include <iostream>
26
#include <xercesc/sax2/Attributes.hpp>
27
#include <utils/common/SUMOTime.h>
28
#include <utils/common/StringUtils.h>
29
#include <utils/xml/CommonXMLStructure.h>
30
#include "SUMOSAXAttributes.h"
31
32
33
// ===========================================================================
34
// class definitions
35
// ===========================================================================
36
/**
37
* @class SUMOSAXAttributesImpl_Cached
38
* @brief Encapsulated Xerces-SAX-attributes
39
*
40
* @see SUMOSAXAttributes
41
*/
42
class SUMOSAXAttributesImpl_Cached : public SUMOSAXAttributes {
43
public:
44
/** @brief Constructor
45
*
46
* @param[in] attrs The encapsulated xerces-attributes (string-string)
47
* @param[in] predefinedTagsMML Map of attribute ids to their (readable) string-representation
48
* @param[in] objectType object type in string format
49
*/
50
SUMOSAXAttributesImpl_Cached(const std::map<std::string, std::string>& attrs,
51
const std::vector<std::string>& predefinedTagsMML,
52
const std::string& objectType);
53
54
/// @brief Destructor
55
~SUMOSAXAttributesImpl_Cached();
56
57
/// @name methods for retrieving attribute values
58
/// @{
59
60
/** @brief Returns the information whether the named (by its enum-value) attribute is within the current list
61
*
62
* @param[in] id The id of the searched attribute
63
* @return Whether the attribute is within the attributes
64
*/
65
bool hasAttribute(int id) const;
66
67
/**
68
* @brief Returns the string-value of the named (by its enum-value) attribute
69
*
70
* Tries to retrieve the attribute from the attribute list. The retrieved
71
* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2str.
72
* If the attribute is ==0, TplConvert<XMLCh>::_2str throws an
73
* EmptyData-exception which is passed.
74
*
75
* @param[in] id The id of the attribute to return the value of
76
* @return The attribute's value as a string, if it could be read and parsed
77
* @exception EmptyData If the attribute is not known or the attribute value is an empty string
78
*/
79
std::string getString(int id, bool* isPresent = nullptr) const;
80
81
/**
82
* @brief Returns the string-value of the named (by its enum-value) attribute
83
*
84
* Tries to retrieve the attribute from the attribute list. The retrieved
85
* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2strSec.
86
* If the attribute is ==0, TplConvert<XMLCh>::_2strSec returns the default value.
87
*
88
* @param[in] id The id of the attribute to return the value of
89
* @param[in] def The default value to return if the attribute is not in attributes
90
* @return The attribute's value as a string, if it could be read and parsed
91
* @exception EmptyData If the attribute is not known or the attribute value is an empty string
92
*/
93
std::string getStringSecure(int id, const std::string& def) const;
94
95
/// @brief Returns the information whether the named attribute is within the current list
96
bool hasAttribute(const std::string& id) const;
97
98
/**
99
* @brief Returns the double-value of the named attribute
100
*
101
* Tries to retrieve the attribute from the attribute list. The retrieved
102
* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2double.
103
* If the attribute is empty or ==0, TplConvert<XMLCh>::_2double throws an
104
* EmptyData-exception which is passed.
105
* If the value can not be parsed to a double, TplConvert<XMLCh>::_2double throws a
106
* NumberFormatException-exception which is passed.
107
*
108
* @param[in] id The name of the attribute to return the value of
109
* @return The attribute's value as a float, if it could be read and parsed
110
* @exception EmptyData If the attribute is not known or the attribute value is an empty string
111
* @exception NumberFormatException If the attribute value can not be parsed to an double
112
*/
113
double getFloat(const std::string& id) const;
114
115
/**
116
* @brief Returns the string-value of the named (by its enum-value) attribute
117
*
118
* Tries to retrieve the attribute from the attribute list.
119
* If the attribute is ==0, TplConvert<XMLCh>::_2strSec returns the default value.
120
* @param[in] id The name of the attribute to return the value of
121
* @param[in] def The default value to return if the attribute is not in attributes
122
* @return The attribute's value as a string, if it could be read and parsed
123
*/
124
std::string getStringSecure(const std::string& id,
125
const std::string& def) const;
126
/// @}
127
128
/** @brief Converts the given attribute id into a man readable string
129
*
130
* Returns a "?" if the attribute is not known.
131
*
132
* @param[in] attr The id of the attribute to return the name of
133
* @return The name of the described attribute
134
*/
135
std::string getName(int attr) const;
136
137
/** @brief Prints all attribute names and values into the given stream
138
*
139
* @param[in] os The stream to use
140
*/
141
void serialize(std::ostream& os) const;
142
143
/** @brief Retrieves all attribute names
144
*/
145
std::vector<std::string> getAttributeNames() const;
146
147
/// @brief return a new deep-copy attributes object
148
SUMOSAXAttributes* clone() const;
149
150
private:
151
/** @brief Returns Xerces-value of the named attribute
152
*
153
* It is assumed that this attribute is within the stored attributes.
154
* @param[in] id The id of the attribute to retrieve the vale of
155
* @return The xerces-value of the attribute
156
*/
157
const std::string& getAttributeValueSecure(int id) const;
158
159
private:
160
/// @brief The encapsulated attributes
161
std::map<std::string, std::string> myAttrs;
162
163
/// @brief Map of attribute ids to their (readable) string-representation
164
const std::vector<std::string>& myPredefinedTagsMML;
165
166
private:
167
/// @brief Invalidated copy constructor.
168
SUMOSAXAttributesImpl_Cached(const SUMOSAXAttributesImpl_Cached& src) = delete;
169
170
/// @brief Invalidated assignment operator.
171
SUMOSAXAttributesImpl_Cached& operator=(const SUMOSAXAttributesImpl_Cached& src) = delete;
172
};
173
174