Path: blob/main/src/utils/xml/SUMOSAXAttributesImpl_Cached.h
169678 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2007-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file SUMOSAXAttributesImpl_Cached.h14/// @author Jakob Erdmann15/// @date Dec 201616///17// Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend18/****************************************************************************/19#pragma once20#include <config.h>2122#include <string>23#include <map>24#include <iostream>25#include <xercesc/sax2/Attributes.hpp>26#include <utils/common/SUMOTime.h>27#include <utils/common/StringUtils.h>28#include <utils/xml/CommonXMLStructure.h>29#include "SUMOSAXAttributes.h"303132// ===========================================================================33// class definitions34// ===========================================================================35/**36* @class SUMOSAXAttributesImpl_Cached37* @brief Encapsulated Xerces-SAX-attributes38*39* @see SUMOSAXAttributes40*/41class SUMOSAXAttributesImpl_Cached : public SUMOSAXAttributes {42public:43/** @brief Constructor44*45* @param[in] attrs The encapsulated xerces-attributes (string-string)46* @param[in] predefinedTagsMML Map of attribute ids to their (readable) string-representation47* @param[in] objectType object type in string format48*/49SUMOSAXAttributesImpl_Cached(const std::map<std::string, std::string>& attrs,50const std::vector<std::string>& predefinedTagsMML,51const std::string& objectType);5253/// @brief Destructor54~SUMOSAXAttributesImpl_Cached();5556/// @name methods for retrieving attribute values57/// @{5859/** @brief Returns the information whether the named (by its enum-value) attribute is within the current list60*61* @param[in] id The id of the searched attribute62* @return Whether the attribute is within the attributes63*/64bool hasAttribute(int id) const;6566/**67* @brief Returns the string-value of the named (by its enum-value) attribute68*69* Tries to retrieve the attribute from the attribute list. The retrieved70* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2str.71* If the attribute is ==0, TplConvert<XMLCh>::_2str throws an72* EmptyData-exception which is passed.73*74* @param[in] id The id of the attribute to return the value of75* @return The attribute's value as a string, if it could be read and parsed76* @exception EmptyData If the attribute is not known or the attribute value is an empty string77*/78std::string getString(int id, bool* isPresent = nullptr) const;7980/**81* @brief Returns the string-value of the named (by its enum-value) attribute82*83* Tries to retrieve the attribute from the attribute list. The retrieved84* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2strSec.85* If the attribute is ==0, TplConvert<XMLCh>::_2strSec returns the default value.86*87* @param[in] id The id of the attribute to return the value of88* @param[in] def The default value to return if the attribute is not in attributes89* @return The attribute's value as a string, if it could be read and parsed90* @exception EmptyData If the attribute is not known or the attribute value is an empty string91*/92std::string getStringSecure(int id, const std::string& def) const;9394/// @brief Returns the information whether the named attribute is within the current list95bool hasAttribute(const std::string& id) const;9697/**98* @brief Returns the double-value of the named attribute99*100* Tries to retrieve the attribute from the attribute list. The retrieved101* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2double.102* If the attribute is empty or ==0, TplConvert<XMLCh>::_2double throws an103* EmptyData-exception which is passed.104* If the value can not be parsed to a double, TplConvert<XMLCh>::_2double throws a105* NumberFormatException-exception which is passed.106*107* @param[in] id The name of the attribute to return the value of108* @return The attribute's value as a float, if it could be read and parsed109* @exception EmptyData If the attribute is not known or the attribute value is an empty string110* @exception NumberFormatException If the attribute value can not be parsed to an double111*/112double getFloat(const std::string& id) const;113114/**115* @brief Returns the string-value of the named (by its enum-value) attribute116*117* Tries to retrieve the attribute from the attribute list.118* If the attribute is ==0, TplConvert<XMLCh>::_2strSec returns the default value.119* @param[in] id The name of the attribute to return the value of120* @param[in] def The default value to return if the attribute is not in attributes121* @return The attribute's value as a string, if it could be read and parsed122*/123std::string getStringSecure(const std::string& id,124const std::string& def) const;125/// @}126127/** @brief Converts the given attribute id into a man readable string128*129* Returns a "?" if the attribute is not known.130*131* @param[in] attr The id of the attribute to return the name of132* @return The name of the described attribute133*/134std::string getName(int attr) const;135136/** @brief Prints all attribute names and values into the given stream137*138* @param[in] os The stream to use139*/140void serialize(std::ostream& os) const;141142/** @brief Retrieves all attribute names143*/144std::vector<std::string> getAttributeNames() const;145146/// @brief return a new deep-copy attributes object147SUMOSAXAttributes* clone() const;148149private:150/** @brief Returns Xerces-value of the named attribute151*152* It is assumed that this attribute is within the stored attributes.153* @param[in] id The id of the attribute to retrieve the vale of154* @return The xerces-value of the attribute155*/156const std::string& getAttributeValueSecure(int id) const;157158private:159/// @brief The encapsulated attributes160std::map<std::string, std::string> myAttrs;161162/// @brief Map of attribute ids to their (readable) string-representation163const std::vector<std::string>& myPredefinedTagsMML;164165private:166/// @brief Invalidated copy constructor.167SUMOSAXAttributesImpl_Cached(const SUMOSAXAttributesImpl_Cached& src) = delete;168169/// @brief Invalidated assignment operator.170SUMOSAXAttributesImpl_Cached& operator=(const SUMOSAXAttributesImpl_Cached& src) = delete;171};172173174