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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // A super class for objects with additional parameters
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
32 #include "TplConvert.h"
33 #include "Parameterised.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
40 
41 
43 
44 
45 Parameterised::Parameterised(const std::map<std::string, std::string>& mapArg)
46  : myMap(mapArg) {
47 }
48 
49 
50 void
51 Parameterised::setParameter(const std::string& key, const std::string& value) {
52  myMap[key] = value;
53 }
54 
55 
56 void
57 Parameterised::unsetParameter(const std::string& key) {
58  myMap.erase(key);
59 }
60 
61 
62 void
63 Parameterised::updateParameter(const std::map<std::string, std::string>& mapArg) {
64  for (std::map<std::string, std::string>::const_iterator i = mapArg.begin(); i != mapArg.end(); ++i) {
65  myMap[(*i).first] = (*i).second;
66  }
67 }
68 
69 
70 bool
71 Parameterised::knowsParameter(const std::string& key) const {
72  return myMap.find(key) != myMap.end();
73 }
74 
75 
76 const std::string
77 Parameterised::getParameter(const std::string& key, const std::string& defaultValue) const {
78  std::map<std::string, std::string>::const_iterator i = myMap.find(key);
79  if (i != myMap.end()) {
80  return (*i).second;
81  }
82  return defaultValue;
83 }
84 
85 
86 double
87 Parameterised::getDouble(const std::string& key, const double defaultValue) const {
88  std::map<std::string, std::string>::const_iterator i = myMap.find(key);
89  if (i != myMap.end()) {
90  return TplConvert::_2double(i->second.c_str());
91  }
92  return defaultValue;
93 }
94 
95 
96 void
98  myMap.clear();
99 }
100 
101 void
103  for (std::map<std::string, std::string>::const_iterator j = myMap.begin(); j != myMap.end(); ++j) {
104  out.openTag(SUMO_TAG_PARAM);
107  out.closeTag();
108  }
109 }
110 
111 /****************************************************************************/
112 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
std::map< std::string, std::string > myMap
The key->value map.
void unsetParameter(const std::string &key)
Removes a parameter.
void writeParams(OutputDevice &out) const
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.
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:311
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Parameterised()
Constructor.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void clearParameter()
Clears the parameter map.