SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  SVCPermissions defaultPermissions) {
56  myDefaultType.numLanes = defaultNumLanes;
57  myDefaultType.width = defaultLaneWidth;
58  myDefaultType.speed = defaultSpeed;
59  myDefaultType.priority = defaultPriority;
60  myDefaultType.permissions = defaultPermissions;
61 }
62 
63 
64 void
65 NBTypeCont::insert(const std::string& id, int numLanes, SUMOReal maxSpeed, int prio,
66  SVCPermissions permissions, SUMOReal width, bool oneWayIsDefault, SUMOReal sidewalkWidth, SUMOReal bikeLaneWidth) {
67 
68  TypeDefinition newType(numLanes, maxSpeed, prio, width, permissions, oneWayIsDefault, sidewalkWidth, bikeLaneWidth);
69  TypesCont::iterator old = myTypes.find(id);
70  if (old != myTypes.end()) {
71  newType.restrictions.insert(old->second.restrictions.begin(), old->second.restrictions.end());
72  newType.attrs.insert(old->second.attrs.begin(), old->second.attrs.end());
73  }
74  myTypes[id] = newType;
75 }
76 
77 
78 bool
79 NBTypeCont::knows(const std::string& type) const {
80  return myTypes.find(type) != myTypes.end();
81 }
82 
83 
84 bool
85 NBTypeCont::markAsToDiscard(const std::string& id) {
86  TypesCont::iterator i = myTypes.find(id);
87  if (i == myTypes.end()) {
88  return false;
89  }
90  (*i).second.discard = true;
91  return true;
92 }
93 
94 
95 bool
96 NBTypeCont::markAsSet(const std::string& id, const SumoXMLAttr attr) {
97  TypesCont::iterator i = myTypes.find(id);
98  if (i == myTypes.end()) {
99  return false;
100  }
101  (*i).second.attrs.insert(attr);
102  return true;
103 }
104 
105 
106 bool
107 NBTypeCont::addRestriction(const std::string& id, const SUMOVehicleClass svc, const SUMOReal speed) {
108  TypesCont::iterator i = myTypes.find(id);
109  if (i == myTypes.end()) {
110  return false;
111  }
112  (*i).second.restrictions[svc] = speed;
113  return true;
114 }
115 
116 
117 bool
118 NBTypeCont::copyRestrictionsAndAttrs(const std::string& fromId, const std::string& toId) {
119  TypesCont::iterator from = myTypes.find(fromId);
120  TypesCont::iterator to = myTypes.find(toId);
121  if (from == myTypes.end() || to == myTypes.end()) {
122  return false;
123  }
124  to->second.restrictions.insert(from->second.restrictions.begin(), from->second.restrictions.end());
125  to->second.attrs.insert(from->second.attrs.begin(), from->second.attrs.end());
126  return true;
127 }
128 
129 
130 void
132  for (TypesCont::const_iterator i = myTypes.begin(); i != myTypes.end(); ++i) {
133  into.openTag(SUMO_TAG_TYPE);
134  into.writeAttr(SUMO_ATTR_ID, i->first);
135  const NBTypeCont::TypeDefinition& type = i->second;
136  if (type.attrs.count(SUMO_ATTR_PRIORITY) > 0) {
138  }
139  if (type.attrs.count(SUMO_ATTR_NUMLANES) > 0) {
141  }
142  if (type.attrs.count(SUMO_ATTR_SPEED) > 0) {
143  into.writeAttr(SUMO_ATTR_SPEED, type.speed);
144  }
145  if (type.attrs.count(SUMO_ATTR_DISALLOW) > 0 || type.attrs.count(SUMO_ATTR_ALLOW) > 0) {
146  writePermissions(into, type.permissions);
147  }
148  if (type.attrs.count(SUMO_ATTR_ONEWAY) > 0) {
149  into.writeAttr(SUMO_ATTR_ONEWAY, type.oneWay);
150  }
151  if (type.attrs.count(SUMO_ATTR_DISCARD) > 0) {
152  into.writeAttr(SUMO_ATTR_DISCARD, type.discard);
153  }
154  if (type.attrs.count(SUMO_ATTR_WIDTH) > 0) {
155  into.writeAttr(SUMO_ATTR_WIDTH, type.width);
156  }
157  if (type.attrs.count(SUMO_ATTR_SIDEWALKWIDTH) > 0) {
159  }
160  if (type.attrs.count(SUMO_ATTR_BIKELANEWIDTH) > 0) {
162  }
163  for (std::map<SUMOVehicleClass, SUMOReal>::const_iterator j = type.restrictions.begin(); j != type.restrictions.end(); ++j) {
166  into.writeAttr(SUMO_ATTR_SPEED, j->second);
167  into.closeTag();
168  }
169  into.closeTag();
170  }
171  if (!myTypes.empty()) {
172  into.lf();
173  }
174 }
175 
176 
177 // ------------ Type-dependant Retrieval methods
178 int
179 NBTypeCont::getNumLanes(const std::string& type) const {
180  return getType(type).numLanes;
181 }
182 
183 
184 SUMOReal
185 NBTypeCont::getSpeed(const std::string& type) const {
186  return getType(type).speed;
187 }
188 
189 
190 int
191 NBTypeCont::getPriority(const std::string& type) const {
192  return getType(type).priority;
193 }
194 
195 
196 bool
197 NBTypeCont::getIsOneWay(const std::string& type) const {
198  return getType(type).oneWay;
199 }
200 
201 
202 bool
203 NBTypeCont::getShallBeDiscarded(const std::string& type) const {
204  return getType(type).discard;
205 }
206 
207 
208 bool
209 NBTypeCont::wasSet(const std::string& type, const SumoXMLAttr attr) const {
210  return getType(type).attrs.count(attr) > 0;
211 }
212 
213 
215 NBTypeCont::getPermissions(const std::string& type) const {
216  return getType(type).permissions;
217 }
218 
219 
220 SUMOReal
221 NBTypeCont::getWidth(const std::string& type) const {
222  return getType(type).width;
223 }
224 
225 
226 SUMOReal
227 NBTypeCont::getSidewalkWidth(const std::string& type) const {
228  return getType(type).sidewalkWidth;
229 }
230 
231 
232 SUMOReal
233 NBTypeCont::getBikeLaneWidth(const std::string& type) const {
234  return getType(type).bikeLaneWidth;
235 }
236 
237 
239 NBTypeCont::getType(const std::string& name) const {
240  TypesCont::const_iterator i = myTypes.find(name);
241  if (i == myTypes.end()) {
242  return myDefaultType;
243  }
244  return (*i).second;
245 }
246 
247 /****************************************************************************/
248 
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:283
int numLanes
The number of lanes of an edge.
Definition: NBTypeCont.h:261
bool wasSet(const std::string &type, const SumoXMLAttr attr) const
Returns whether an attribute of a type was set.
Definition: NBTypeCont.cpp:209
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:239
SUMOReal width
The width of lanes of edges of this type [m].
Definition: NBTypeCont.h:273
int SVCPermissions
bool getIsOneWay(const std::string &type) const
Returns whether edges are one-way per default for the given type.
Definition: NBTypeCont.cpp:197
SUMOReal speed
The maximal velocity on an edge in m/s.
Definition: NBTypeCont.h:263
bool markAsSet(const std::string &id, const SumoXMLAttr attr)
Marks an attribute of a type as set.
Definition: NBTypeCont.cpp:96
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
void setDefaults(int defaultNumLanes, SUMOReal defaultLaneWidth, SUMOReal defaultSpeed, int defaultPriority, SVCPermissions defaultPermissions)
Sets the default values.
Definition: NBTypeCont.cpp:51
bool oneWay
Whether one-way traffic is mostly common for this type (mostly unused)
Definition: NBTypeCont.h:269
SUMOReal getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:221
SUMOReal getSidewalkWidth(const std::string &type) const
Returns the lane width for a sidewalk to be added [m].
Definition: NBTypeCont.cpp:227
SUMOReal getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:185
SUMOReal getBikeLaneWidth(const std::string &type) const
Returns the lane width for a bike lane to be added [m].
Definition: NBTypeCont.cpp:233
int getNumLanes(const std::string &type) const
Returns the number of lanes for the given type.
Definition: NBTypeCont.cpp:179
int getPriority(const std::string &type) const
Returns the priority for the given type.
Definition: NBTypeCont.cpp:191
bool knows(const std::string &type) const
Returns whether the named type is in the container.
Definition: NBTypeCont.cpp:79
std::string getVehicleClassNames(SVCPermissions permissions)
Returns the ids of the given classes, divided using a ' '.
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:65
void writeTypes(OutputDevice &into) const
writes all types a s XML
Definition: NBTypeCont.cpp:131
bool markAsToDiscard(const std::string &id)
Marks a type as to be discarded.
Definition: NBTypeCont.cpp:85
TypeDefinition myDefaultType
The default type.
Definition: NBTypeCont.h:299
bool getShallBeDiscarded(const std::string &type) const
Returns the information whether edges of this type shall be discarded.
Definition: NBTypeCont.cpp:203
bool copyRestrictionsAndAttrs(const std::string &fromId, const std::string &toId)
Copy restrictions to a type.
Definition: NBTypeCont.cpp:118
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:214
SVCPermissions getPermissions(const std::string &type) const
Returns allowed vehicle classes for the given type.
Definition: NBTypeCont.cpp:215
int priority
The priority of an edge.
Definition: NBTypeCont.h:265
SVCPermissions permissions
List of vehicle types that are allowed on this edge.
Definition: NBTypeCont.h:267
std::map< SUMOVehicleClass, SUMOReal > restrictions
The vehicle class specific speed restrictions.
Definition: NBTypeCont.h:281
TypesCont myTypes
The container of types.
Definition: NBTypeCont.h:305
bool addRestriction(const std::string &id, const SUMOVehicleClass svc, const SUMOReal speed)
Adds a restriction to a type.
Definition: NBTypeCont.cpp:107
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:271