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 "TplConvert.h"
28 #include "Parameterised.h"
29 
30 
31 // ===========================================================================
32 // method definitions
33 // ===========================================================================
35 
36 
38 
39 
40 Parameterised::Parameterised(const std::map<std::string, std::string>& mapArg)
41  : myMap(mapArg) {
42 }
43 
44 
45 void
46 Parameterised::setParameter(const std::string& key, const std::string& value) {
47  myMap[key] = value;
48 }
49 
50 
51 void
52 Parameterised::unsetParameter(const std::string& key) {
53  myMap.erase(key);
54 }
55 
56 
57 void
58 Parameterised::updateParameter(const std::map<std::string, std::string>& mapArg) {
59  for (auto i : mapArg) {
60  myMap[i.first] = i.second;
61  }
62 }
63 
64 
65 bool
66 Parameterised::knowsParameter(const std::string& key) const {
67  return myMap.find(key) != myMap.end();
68 }
69 
70 
71 const std::string
72 Parameterised::getParameter(const std::string& key, const std::string& defaultValue) const {
73  std::map<std::string, std::string>::const_iterator i = myMap.find(key);
74  if (i != myMap.end()) {
75  return i->second;
76  }
77  return defaultValue;
78 }
79 
80 
81 double
82 Parameterised::getDouble(const std::string& key, const double defaultValue) const {
83  std::map<std::string, std::string>::const_iterator i = myMap.find(key);
84  if (i != myMap.end()) {
85  try {
86  return TplConvert::_2double(i->second.c_str());
87  } catch (NumberFormatException&) {
88  WRITE_WARNING("Invalid conversion from string to double (" + i->second + ")");
89  return defaultValue;
90  } catch (EmptyData&) {
91  WRITE_WARNING("Invalid conversion from string to double (empty value)");
92  return defaultValue;
93  }
94  }
95  return defaultValue;
96 }
97 
98 
99 void
101  myMap.clear();
102 }
103 
104 
105 const std::map<std::string, std::string>&
107  return myMap;
108 }
109 
110 
111 void
113  // iterate over all parameters and write it
114  for (auto i : myMap) {
115  device.openTag(SUMO_TAG_PARAM);
118  device.closeTag();
119  }
120 }
121 
122 /****************************************************************************/
123 
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:242
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.
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.
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:305
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.