SUMO - Simulation of Urban MObility
NBTypeCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A storage for the available types of an edge
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <map>
36 #include <iostream>
38 #include <utils/common/ToString.h>
40 #include "NBTypeCont.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 void
51 NBTypeCont::setDefaults(int defaultNumLanes,
52  SUMOReal defaultLaneWidth,
53  SUMOReal defaultSpeed,
54  int defaultPriority) {
55  myDefaultType.numLanes = defaultNumLanes;
56  myDefaultType.width = defaultLaneWidth;
57  myDefaultType.speed = defaultSpeed;
58  myDefaultType.priority = defaultPriority;
59 }
60 
61 
62 void
63 NBTypeCont::insert(const std::string& id, int numLanes, SUMOReal maxSpeed, int prio,
64  SVCPermissions permissions, SUMOReal width, bool oneWayIsDefault, SUMOReal sidewalkWidth, SUMOReal bikeLaneWidth) {
65 
66  TypeDefinition newType(numLanes, maxSpeed, prio, width, permissions, oneWayIsDefault, sidewalkWidth, bikeLaneWidth);
67  TypesCont::iterator old = myTypes.find(id);
68  if (old != myTypes.end()) {
69  newType.restrictions.insert(old->second.restrictions.begin(), old->second.restrictions.end());
70  newType.attrs.insert(old->second.attrs.begin(), old->second.attrs.end());
71  }
72  myTypes[id] = newType;
73 }
74 
75 
76 bool
77 NBTypeCont::knows(const std::string& type) const {
78  return myTypes.find(type) != myTypes.end();
79 }
80 
81 
82 bool
83 NBTypeCont::markAsToDiscard(const std::string& id) {
84  TypesCont::iterator i = myTypes.find(id);
85  if (i == myTypes.end()) {
86  return false;
87  }
88  (*i).second.discard = true;
89  return true;
90 }
91 
92 
93 bool
94 NBTypeCont::markAsSet(const std::string& id, const SumoXMLAttr attr) {
95  TypesCont::iterator i = myTypes.find(id);
96  if (i == myTypes.end()) {
97  return false;
98  }
99  (*i).second.attrs.insert(attr);
100  return true;
101 }
102 
103 
104 bool
105 NBTypeCont::addRestriction(const std::string& id, const SUMOVehicleClass svc, const SUMOReal speed) {
106  TypesCont::iterator i = myTypes.find(id);
107  if (i == myTypes.end()) {
108  return false;
109  }
110  (*i).second.restrictions[svc] = speed;
111  return true;
112 }
113 
114 
115 bool
116 NBTypeCont::copyRestrictionsAndAttrs(const std::string& fromId, const std::string& toId) {
117  TypesCont::iterator from = myTypes.find(fromId);
118  TypesCont::iterator to = myTypes.find(toId);
119  if (from == myTypes.end() || to == myTypes.end()) {
120  return false;
121  }
122  to->second.restrictions.insert(from->second.restrictions.begin(), from->second.restrictions.end());
123  to->second.attrs.insert(from->second.attrs.begin(), from->second.attrs.end());
124  return true;
125 }
126 
127 
128 void
130  for (TypesCont::const_iterator i = myTypes.begin(); i != myTypes.end(); ++i) {
131  into.openTag(SUMO_TAG_TYPE);
132  into.writeAttr(SUMO_ATTR_ID, i->first);
133  const NBTypeCont::TypeDefinition& type = i->second;
134  if (type.attrs.count(SUMO_ATTR_PRIORITY) > 0) {
136  }
137  if (type.attrs.count(SUMO_ATTR_NUMLANES) > 0) {
139  }
140  if (type.attrs.count(SUMO_ATTR_SPEED) > 0) {
141  into.writeAttr(SUMO_ATTR_SPEED, type.speed);
142  }
143  if (type.attrs.count(SUMO_ATTR_DISALLOW) > 0 || type.attrs.count(SUMO_ATTR_ALLOW) > 0) {
144  writePermissions(into, type.permissions);
145  }
146  if (type.attrs.count(SUMO_ATTR_ONEWAY) > 0) {
147  into.writeAttr(SUMO_ATTR_ONEWAY, type.oneWay);
148  }
149  if (type.attrs.count(SUMO_ATTR_DISCARD) > 0) {
150  into.writeAttr(SUMO_ATTR_DISCARD, type.discard);
151  }
152  if (type.attrs.count(SUMO_ATTR_WIDTH) > 0) {
153  into.writeAttr(SUMO_ATTR_WIDTH, type.width);
154  }
155  if (type.attrs.count(SUMO_ATTR_SIDEWALKWIDTH) > 0) {
157  }
158  if (type.attrs.count(SUMO_ATTR_BIKELANEWIDTH) > 0) {
160  }
161  for (std::map<SUMOVehicleClass, SUMOReal>::const_iterator j = type.restrictions.begin(); j != type.restrictions.end(); ++j) {
164  into.writeAttr(SUMO_ATTR_SPEED, j->second);
165  into.closeTag();
166  }
167  into.closeTag();
168  }
169  if (!myTypes.empty()) {
170  into.lf();
171  }
172 }
173 
174 
175 // ------------ Type-dependant Retrieval methods
176 int
177 NBTypeCont::getNumLanes(const std::string& type) const {
178  return getType(type).numLanes;
179 }
180 
181 
182 SUMOReal
183 NBTypeCont::getSpeed(const std::string& type) const {
184  return getType(type).speed;
185 }
186 
187 
188 int
189 NBTypeCont::getPriority(const std::string& type) const {
190  return getType(type).priority;
191 }
192 
193 
194 bool
195 NBTypeCont::getIsOneWay(const std::string& type) const {
196  return getType(type).oneWay;
197 }
198 
199 
200 bool
201 NBTypeCont::getShallBeDiscarded(const std::string& type) const {
202  return getType(type).discard;
203 }
204 
205 
206 bool
207 NBTypeCont::wasSet(const std::string& type, const SumoXMLAttr attr) const {
208  return getType(type).attrs.count(attr) > 0;
209 }
210 
211 
213 NBTypeCont::getPermissions(const std::string& type) const {
214  return getType(type).permissions;
215 }
216 
217 
218 SUMOReal
219 NBTypeCont::getWidth(const std::string& type) const {
220  return getType(type).width;
221 }
222 
223 
224 SUMOReal
225 NBTypeCont::getSidewalkWidth(const std::string& type) const {
226  return getType(type).sidewalkWidth;
227 }
228 
229 
230 SUMOReal
231 NBTypeCont::getBikeLaneWidth(const std::string& type) const {
232  return getType(type).bikeLaneWidth;
233 }
234 
235 
237 NBTypeCont::getType(const std::string& name) const {
238  TypesCont::const_iterator i = myTypes.find(name);
239  if (i == myTypes.end()) {
240  return myDefaultType;
241  }
242  return (*i).second;
243 }
244 
245 /****************************************************************************/
246 
void writePermissions(OutputDevice &into, SVCPermissions permissions)
writes allowed disallowed attributes if needed;
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
std::set< SumoXMLAttr > attrs
The attributes which have been set.
Definition: NBTypeCont.h:281
int numLanes
The number of lanes of an edge.
Definition: NBTypeCont.h:259
bool wasSet(const std::string &type, const SumoXMLAttr attr) const
Returns whether an attribute of a type was set.
Definition: NBTypeCont.cpp:207
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
const TypeDefinition & getType(const std::string &name) const
Retrieve the name or the default type.
Definition: NBTypeCont.cpp:237
SUMOReal width
The width of lanes of edges of this type [m].
Definition: NBTypeCont.h:271
int SVCPermissions
bool getIsOneWay(const std::string &type) const
Returns whether edges are one-way per default for the given type.
Definition: NBTypeCont.cpp:195
SUMOReal speed
The maximal velocity on an edge in m/s.
Definition: NBTypeCont.h:261
bool markAsSet(const std::string &id, const SumoXMLAttr attr)
Marks an attribute of a type as set.
Definition: NBTypeCont.cpp:94
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
bool oneWay
Whether one-way traffic is mostly common for this type (mostly unused)
Definition: NBTypeCont.h:267
SUMOReal getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:219
SUMOReal getSidewalkWidth(const std::string &type) const
Returns the lane width for a sidewalk to be added [m].
Definition: NBTypeCont.cpp:225
SUMOReal getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:183
SUMOReal getBikeLaneWidth(const std::string &type) const
Returns the lane width for a bike lane to be added [m].
Definition: NBTypeCont.cpp:231
int getNumLanes(const std::string &type) const
Returns the number of lanes for the given type.
Definition: NBTypeCont.cpp:177
int getPriority(const std::string &type) const
Returns the priority for the given type.
Definition: NBTypeCont.cpp:189
bool knows(const std::string &type) const
Returns whether the named type is in the container.
Definition: NBTypeCont.cpp:77
std::string getVehicleClassNames(SVCPermissions permissions)
Returns the ids of the given classes, divided using a &#39; &#39;.
void setDefaults(int defaultNumLanes, SUMOReal defaultLaneWidth, SUMOReal defaultSpeed, int defaultPriority)
Sets the default values.
Definition: NBTypeCont.cpp:51
void insert(const std::string &id, int numLanes, SUMOReal maxSpeed, int prio, SVCPermissions permissions, SUMOReal width, bool oneWayIsDefault, SUMOReal sidewalkWidth, SUMOReal bikeLaneWidth)
Adds a type into the list.
Definition: NBTypeCont.cpp:63
void writeTypes(OutputDevice &into) const
writes all types a s XML
Definition: NBTypeCont.cpp:129
bool markAsToDiscard(const std::string &id)
Marks a type as to be discarded.
Definition: NBTypeCont.cpp:83
TypeDefinition myDefaultType
The default type.
Definition: NBTypeCont.h:297
bool getShallBeDiscarded(const std::string &type) const
Returns the information whether edges of this type shall be discarded.
Definition: NBTypeCont.cpp:201
bool copyRestrictionsAndAttrs(const std::string &fromId, const std::string &toId)
Copy restrictions to a type.
Definition: NBTypeCont.cpp:116
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
SVCPermissions getPermissions(const std::string &type) const
Returns allowed vehicle classes for the given type.
Definition: NBTypeCont.cpp:213
int priority
The priority of an edge.
Definition: NBTypeCont.h:263
SVCPermissions permissions
List of vehicle types that are allowed on this edge.
Definition: NBTypeCont.h:265
std::map< SUMOVehicleClass, SUMOReal > restrictions
The vehicle class specific speed restrictions.
Definition: NBTypeCont.h:279
TypesCont myTypes
The container of types.
Definition: NBTypeCont.h:303
bool addRestriction(const std::string &id, const SUMOVehicleClass svc, const SUMOReal speed)
Adds a restriction to a type.
Definition: NBTypeCont.cpp:105
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:235
bool discard
Whether edges of this type shall be discarded.
Definition: NBTypeCont.h:269