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