SUMO - Simulation of Urban MObility
Parameterised.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // A super class for objects with additional parameters
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
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::addParameter(const std::string& key, const std::string& value) {
52  myMap[key] = value;
53 }
54 
55 
56 void
57 Parameterised::addParameter(const std::map<std::string, std::string>& mapArg) {
58  for (std::map<std::string, std::string>::const_iterator i = mapArg.begin(); i != mapArg.end(); ++i) {
59  myMap[(*i).first] = (*i).second;
60  }
61 }
62 
63 
64 void
66  for (std::map<std::string, std::string>::const_iterator i = p.myMap.begin(); i != p.myMap.end(); ++i) {
67  myMap[(*i).first] = (*i).second;
68  }
69 }
70 
71 
72 bool
73 Parameterised::knowsParameter(const std::string& key) const {
74  return myMap.find(key) != myMap.end();
75 }
76 
77 
78 const std::string&
79 Parameterised::getParameter(const std::string& key, const std::string& defaultValue) const {
80  std::map<std::string, std::string>::const_iterator i = myMap.find(key);
81  if (i != myMap.end()) {
82  return (*i).second;
83  }
84  return defaultValue;
85 }
86 
87 
88 double
89 Parameterised::getDouble(const std::string& key, const double defaultValue) const {
90  std::map<std::string, std::string>::const_iterator i = myMap.find(key);
91  if (i != myMap.end()) {
92  return TplConvert::_2double(i->second.c_str());
93  }
94  return defaultValue;
95 }
96 
97 
98 void
100  myMap.clear();
101 }
102 
103 void
105  for (std::map<std::string, std::string>::const_iterator j = myMap.begin(); j != myMap.end(); ++j) {
106  out.openTag(SUMO_TAG_PARAM);
107  out.writeAttr(SUMO_ATTR_KEY, (*j).first);
108  out.writeAttr(SUMO_ATTR_VALUE, (*j).second);
109  out.closeTag();
110  }
111 }
112 
113 /****************************************************************************/
114 
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 writeParams(OutputDevice &out) const
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
parameter associated to a certain key
~Parameterised()
Destructor.
An upper class for objects with additional parameters.
Definition: Parameterised.h:51
double getDouble(const std::string &key, const double defaultValue) const
Returns the value for a given key converted to a double.
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
Parameterised()
Constructor.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void clearParameter()
Clears the parameter map.