SUMO - Simulation of Urban MObility
ToString.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // -------------------
19 /****************************************************************************/
20 #ifndef ToString_h
21 #define ToString_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <sstream>
30 #include <string>
31 #include <iomanip>
32 #include <algorithm>
33 #include <list>
36 #include <utils/common/Named.h>
38 #include "StdDefs.h"
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
48 template <class T>
49 inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
50  std::ostringstream oss;
51  oss.setf(std::ios::fixed, std::ios::floatfield);
52  oss << std::setprecision(accuracy);
53  oss << t;
54  return oss.str();
55 }
56 
57 
58 template<typename T>
59 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
60  // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
61  std::stringstream stream;
62  stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
63  return stream.str();
64 }
65 
66 
67 inline std::string toString(const Named* obj, std::streamsize accuracy) {
68  UNUSED_PARAMETER(accuracy);
69  return Named::getIDSecure(obj);
70 }
71 
72 
73 template <>
74 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
75  UNUSED_PARAMETER(accuracy);
77 }
78 
79 
80 template <>
81 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
82  UNUSED_PARAMETER(accuracy);
84 }
85 
86 
87 template <>
88 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
89  UNUSED_PARAMETER(accuracy);
91 }
92 
93 
94 template <>
95 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
96  UNUSED_PARAMETER(accuracy);
98 }
99 
100 
101 template <>
102 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
103  UNUSED_PARAMETER(accuracy);
104  return SumoVehicleClassStrings.getString(vClass);
105 }
106 
107 
108 template <>
109 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
110  UNUSED_PARAMETER(accuracy);
112 }
113 
114 template <>
115 inline std::string toString<RightOfWay>(const RightOfWay& row, 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 T, typename T_BETWEEN>
261 inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
262  std::vector<std::string> ids;
263  for (T* n : ns) {
264  ids.push_back(Named::getIDSecure(n));
265  }
266  return joinToStringSorting(ids, between);
267 }
268 
269 
270 template <typename T, typename C, typename T_BETWEEN>
271 inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
272  std::vector<std::string> ids;
273  for (T* n : ns) {
274  ids.push_back(Named::getIDSecure(n));
275  }
276  return joinToString(ids, between);
277 }
278 
279 
280 template <typename V>
281 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
282  UNUSED_PARAMETER(accuracy);
283  std::vector<std::string> ids;
284  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
285  ids.push_back((*it)->getID());
286  }
287  return joinToStringSorting(ids, " ");
288 }
289 
290 
291 template <>
292 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
293  return joinToString(v, " ", accuracy);
294 }
295 
296 
297 template <>
298 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
299  return joinToString(v, " ", accuracy);
300 }
301 
302 
303 template <>
304 inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
305  return joinToString(v, " ", accuracy);
306 }
307 
308 
309 template <typename T, typename T_BETWEEN>
310 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
311  std::ostringstream oss;
312  bool connect = false;
313  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
314  if (connect) {
315  oss << toString(between, accuracy);
316  } else {
317  connect = true;
318  }
319  oss << toString(*it, accuracy);
320  }
321  return oss.str();
322 }
323 
324 
325 template <>
326 inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
327  return joinToString(v, " ");
328 }
329 
330 
331 template <>
332 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
333  return joinToString(v, " ");
334 }
335 
336 
337 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
338 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
339  std::ostringstream oss;
340  bool connect = false;
341  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
342  if (connect) {
343  oss << toString(between, accuracy);
344  } else {
345  connect = true;
346  }
347  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
348  }
349  return oss.str();
350 }
351 
352 
353 template <>
354 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
355  return joinToString(v, ", ", ":");
356 }
357 
358 
359 #endif
360 
361 /****************************************************************************/
362 
static StringBijection< RightOfWay > RightOfWayValues
lane spread functions
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
std::string joinNamedToString(const std::set< T *, C > &ns, const T_BETWEEN &between)
Definition: ToString.h:271
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:27
const std::string & getString(const T key) const
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:71
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static StringBijection< LinkState > LinkStates
link states
std::string joinNamedToStringSorting(const std::set< T *> &ns, const T_BETWEEN &between)
Definition: ToString.h:261
RightOfWay
algorithms for computing right of way
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:33
std::vector< std::string > getStrings() const
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::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:49
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:136
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
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:74
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:58
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:95
LaneChangeAction
The state of a vehicle&#39;s lane-change behavior.
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:59
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:102
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< RightOfWay >(const RightOfWay &row, std::streamsize accuracy)
Definition: ToString.h:115
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition: ToString.h:81
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:109
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:88
TrafficLightType