SUMO - Simulation of Urban MObility
Parameterised.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // A super class for objects with additional parameters
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
26 
27 #include "Parameterised.h"
28 
29 
30 // ===========================================================================
31 // method definitions
32 // ===========================================================================
34 
35 
37 
38 
39 Parameterised::Parameterised(const std::map<std::string, std::string>& mapArg)
40  : myMap(mapArg) {
41 }
42 
43 
44 void
45 Parameterised::setParameter(const std::string& key, const std::string& value) {
46  myMap[key] = value;
47 }
48 
49 
50 void
51 Parameterised::unsetParameter(const std::string& key) {
52  myMap.erase(key);
53 }
54 
55 
56 void
57 Parameterised::updateParameter(const std::map<std::string, std::string>& mapArg) {
58  for (auto i : mapArg) {
59  myMap[i.first] = i.second;
60  }
61 }
62 
63 
64 bool
65 Parameterised::knowsParameter(const std::string& key) const {
66  return myMap.find(key) != myMap.end();
67 }
68 
69 
70 const std::string
71 Parameterised::getParameter(const std::string& key, const std::string& defaultValue) const {
72  std::map<std::string, std::string>::const_iterator i = myMap.find(key);
73  if (i != myMap.end()) {
74  return i->second;
75  }
76  return defaultValue;
77 }
78 
79 
80 double
81 Parameterised::getDouble(const std::string& key, const double defaultValue) const {
82  std::map<std::string, std::string>::const_iterator i = myMap.find(key);
83  if (i != myMap.end()) {
84  try {
85  return StringUtils::toDouble(i->second);
86  } catch (NumberFormatException&) {
87  WRITE_WARNING("Invalid conversion from string to double (" + i->second + ")");
88  return defaultValue;
89  } catch (EmptyData&) {
90  WRITE_WARNING("Invalid conversion from string to double (empty value)");
91  return defaultValue;
92  }
93  }
94  return defaultValue;
95 }
96 
97 
98 void
100  myMap.clear();
101 }
102 
103 
104 const std::map<std::string, std::string>&
106  return myMap;
107 }
108 
109 
110 void
112  // iterate over all parameters and write it
113  for (auto i : myMap) {
114  device.openTag(SUMO_TAG_PARAM);
117  device.closeTag();
118  }
119 }
120 
121 /****************************************************************************/
122 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
std::map< std::string, std::string > myMap
The key->value map.
void unsetParameter(const std::string &key)
Removes a parameter.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
void updateParameter(const std::map< std::string, std::string > &mapArg)
Adds or updates all given parameters from the map.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
parameter associated to a certain key
~Parameterised()
Destructor.
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
double getDouble(const std::string &key, const double defaultValue) const
Returns the value for a given key converted to a double.
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Parameterised()
Constructor.
const std::map< std::string, std::string > & getParametersMap() const
Returns the inner key/value map.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void clearParameter()
Clears the parameter map.