SUMO - Simulation of Urban MObility
ToString.h
Go to the documentation of this file.
1 /****************************************************************************/
10 // -------------------
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2002-2017 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 #ifndef ToString_h
24 #define ToString_h
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <sstream>
37 #include <string>
38 #include <iomanip>
39 #include <algorithm>
40 #include <list>
43 #include <utils/common/Named.h>
45 #include "StdDefs.h"
46 
47 
48 // ===========================================================================
49 // class definitions
50 // ===========================================================================
55 template <class T>
56 inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
57  std::ostringstream oss;
58  oss.setf(std::ios::fixed , std::ios::floatfield);
59  oss << std::setprecision(accuracy);
60  oss << t;
61  return oss.str();
62 }
63 
64 
65 template<typename T>
66 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
67  // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
68  std::stringstream stream;
69  stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
70  return stream.str();
71 }
72 
73 
74 inline std::string toString(const Named* obj, std::streamsize accuracy) {
75  UNUSED_PARAMETER(accuracy);
76  return Named::getIDSecure(obj);
77 }
78 
79 template <>
80 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
81  UNUSED_PARAMETER(accuracy);
83 }
84 
85 
86 template <>
87 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
88  UNUSED_PARAMETER(accuracy);
90 }
91 
92 
93 template <>
94 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
95  UNUSED_PARAMETER(accuracy);
97 }
98 
99 
100 template <>
101 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
102  UNUSED_PARAMETER(accuracy);
104 }
105 
106 
107 template <>
108 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
109  UNUSED_PARAMETER(accuracy);
110  return SumoVehicleClassStrings.getString(vClass);
111 }
112 
113 
114 template <>
115 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
116  UNUSED_PARAMETER(accuracy);
118 }
119 
120 
121 template <>
122 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
123  UNUSED_PARAMETER(accuracy);
124  return SUMOXMLDefinitions::LinkStates.getString(linkState);
125 }
126 
127 
128 template <>
129 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
130  UNUSED_PARAMETER(accuracy);
132 }
133 
134 
135 template <>
136 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
137  UNUSED_PARAMETER(accuracy);
139 }
140 
141 
142 template <>
143 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
144  UNUSED_PARAMETER(accuracy);
146 }
147 
148 template <>
149 inline std::string toString<LateralAlignment>(const LateralAlignment& latA, std::streamsize accuracy) {
150  UNUSED_PARAMETER(accuracy);
152 }
153 
154 template <>
155 inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
156  UNUSED_PARAMETER(accuracy);
157  std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
158  bool hadOne = false;
159  std::ostringstream oss;
160  for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
161  if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
162  if (hadOne) {
163  oss << "|";
164  } else {
165  hadOne = true;
166  }
167  oss << (*it);
168  }
169  }
170  return oss.str();
171 }
172 
173 template <>
174 inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
175  return dist.toStr(accuracy);
176 }
177 
178 template <typename V>
179 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
180  return toString<V>(v.begin(), v.end(), accuracy);
181 }
182 
183 template <typename V>
184 inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
185  UNUSED_PARAMETER(accuracy);
186  std::ostringstream oss;
187  for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
188  if (it != b) {
189  oss << " ";
190  }
191  oss << Named::getIDSecure(*it);
192  }
193  return oss.str();
194 }
195 
196 template <typename V>
197 inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
198  return toString<V>(v.begin(), v.end(), accuracy);
199 }
200 
201 template <typename V>
202 inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
203  UNUSED_PARAMETER(accuracy);
204  std::ostringstream oss;
205  for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
206  if (it != b) {
207  oss << " ";
208  }
209  oss << Named::getIDSecure(*it);
210  }
211  return oss.str();
212 }
213 
214 
215 
216 //template <typename V>
217 //inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
218 // return toString<V>(v.begin(), v.end(), accuracy);
219 //}
220 //
221 //
222 //template <typename V>
223 //inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
224 // UNUSED_PARAMETER(accuracy);
225 // std::ostringstream oss;
226 // for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
227 // if (it != b) {
228 // oss << " ";
229 // }
230 // oss << Named::getIDSecure(*it);
231 // }
232 // return oss.str();
233 //}
234 
235 
236 template <typename T, typename T_BETWEEN>
237 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
238  std::ostringstream oss;
239  bool connect = false;
240  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
241  if (connect) {
242  oss << toString(between, accuracy);
243  } else {
244  connect = true;
245  }
246  oss << toString(*it, accuracy);
247  }
248  return oss.str();
249 }
250 
251 
252 template <typename T, typename T_BETWEEN>
253 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
254  std::vector<T> sorted(v);
255  std::sort(sorted.begin(), sorted.end());
256  return joinToString(sorted, between, accuracy);
257 }
258 
259 
260 template <typename V>
261 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
262  UNUSED_PARAMETER(accuracy);
263  std::vector<std::string> ids;
264  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
265  ids.push_back((*it)->getID());
266  }
267  return joinToStringSorting(ids, " ");
268 }
269 
270 
271 template <>
272 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
273  return joinToString(v, " ", accuracy);
274 }
275 
276 
277 template <>
278 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
279  return joinToString(v, " ", accuracy);
280 }
281 
282 
283 template <>
284 inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
285  return joinToString(v, " ", accuracy);
286 }
287 
288 
289 template <typename T, typename T_BETWEEN>
290 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
291  std::ostringstream oss;
292  bool connect = false;
293  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
294  if (connect) {
295  oss << toString(between, accuracy);
296  } else {
297  connect = true;
298  }
299  oss << toString(*it, accuracy);
300  }
301  return oss.str();
302 }
303 
304 
305 template <>
306 inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
307  return joinToString(v, " ");
308 }
309 
310 
311 template <>
312 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
313  return joinToString(v, " ");
314 }
315 
316 
317 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
318 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
319  std::ostringstream oss;
320  bool connect = false;
321  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
322  if (connect) {
323  oss << toString(between, accuracy);
324  } else {
325  connect = true;
326  }
327  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
328  }
329  return oss.str();
330 }
331 
332 
333 template <>
334 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
335  return joinToString(v, ", ", ":");
336 }
337 
338 
339 #endif
340 
341 /****************************************************************************/
342 
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:129
static StringBijection< SumoXMLNodeType > NodeTypes
node types
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
static StringBijection< LaneSpreadFunction > LaneSpreadFunctions
lane spread functions
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:30
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition: ToString.h:155
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition: Named.h:59
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static StringBijection< LinkState > LinkStates
link states
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:40
LateralAlignment
Numbers representing special SUMO-XML-attribute values Information how vehicles align themselves with...
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
std::vector< std::string > getStrings() const
std::string toString< Distribution_Parameterized >(const Distribution_Parameterized &dist, std::streamsize accuracy)
Definition: ToString.h:174
static StringBijection< LinkDirection > LinkDirections
link directions
LaneChangeModel
static StringBijection< LaneChangeAction > LaneChangeActions
lane change actions
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:136
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
const std::string & getString(const T key) const
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
std::string toString< SumoXMLTag >(const SumoXMLTag &tag, std::streamsize accuracy)
Definition: ToString.h:80
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:143
Base class for objects which have an id.
Definition: Named.h:46
std::string toString< LateralAlignment >(const LateralAlignment &latA, std::streamsize accuracy)
Definition: ToString.h:149
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:101
LaneChangeAction
The state of a vehicle&#39;s lane-change behavior.
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
std::string toString< LinkState >(const LinkState &linkState, std::streamsize accuracy)
Definition: ToString.h:122
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:253
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:108
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
LaneSpreadFunction
Numbers representing special SUMO-XML-attribute values Information how the edge&#39;s lateral offset shal...
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition: ToString.h:87
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static StringBijection< LaneChangeModel > LaneChangeModels
lane change models
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:237
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition: ToString.h:115
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:94
TrafficLightType