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 template <>
73 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
74  UNUSED_PARAMETER(accuracy);
76 }
77 
78 
79 template <>
80 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
81  UNUSED_PARAMETER(accuracy);
83 }
84 
85 
86 template <>
87 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
88  UNUSED_PARAMETER(accuracy);
90 }
91 
92 
93 template <>
94 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
95  UNUSED_PARAMETER(accuracy);
97 }
98 
99 
100 template <>
101 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
102  UNUSED_PARAMETER(accuracy);
103  return SumoVehicleClassStrings.getString(vClass);
104 }
105 
106 
107 template <>
108 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
109  UNUSED_PARAMETER(accuracy);
111 }
112 
113 
114 template <>
115 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
116  UNUSED_PARAMETER(accuracy);
117  return SUMOXMLDefinitions::LinkStates.getString(linkState);
118 }
119 
120 
121 template <>
122 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
123  UNUSED_PARAMETER(accuracy);
125 }
126 
127 
128 template <>
129 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
130  UNUSED_PARAMETER(accuracy);
132 }
133 
134 
135 template <>
136 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
137  UNUSED_PARAMETER(accuracy);
139 }
140 
141 template <>
142 inline std::string toString<LateralAlignment>(const LateralAlignment& latA, std::streamsize accuracy) {
143  UNUSED_PARAMETER(accuracy);
145 }
146 
147 template <>
148 inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
149  UNUSED_PARAMETER(accuracy);
150  std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
151  bool hadOne = false;
152  std::ostringstream oss;
153  for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
154  if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
155  if (hadOne) {
156  oss << "|";
157  } else {
158  hadOne = true;
159  }
160  oss << (*it);
161  }
162  }
163  return oss.str();
164 }
165 
166 template <>
167 inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
168  return dist.toStr(accuracy);
169 }
170 
171 template <typename V>
172 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
173  return toString<V>(v.begin(), v.end(), accuracy);
174 }
175 
176 template <typename V>
177 inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
178  UNUSED_PARAMETER(accuracy);
179  std::ostringstream oss;
180  for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
181  if (it != b) {
182  oss << " ";
183  }
184  oss << Named::getIDSecure(*it);
185  }
186  return oss.str();
187 }
188 
189 template <typename V>
190 inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
191  return toString<V>(v.begin(), v.end(), accuracy);
192 }
193 
194 template <typename V>
195 inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
196  UNUSED_PARAMETER(accuracy);
197  std::ostringstream oss;
198  for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
199  if (it != b) {
200  oss << " ";
201  }
202  oss << Named::getIDSecure(*it);
203  }
204  return oss.str();
205 }
206 
207 
208 
209 //template <typename V>
210 //inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
211 // return toString<V>(v.begin(), v.end(), accuracy);
212 //}
213 //
214 //
215 //template <typename V>
216 //inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
217 // UNUSED_PARAMETER(accuracy);
218 // std::ostringstream oss;
219 // for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
220 // if (it != b) {
221 // oss << " ";
222 // }
223 // oss << Named::getIDSecure(*it);
224 // }
225 // return oss.str();
226 //}
227 
228 
229 template <typename T, typename T_BETWEEN>
230 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
231  std::ostringstream oss;
232  bool connect = false;
233  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
234  if (connect) {
235  oss << toString(between, accuracy);
236  } else {
237  connect = true;
238  }
239  oss << toString(*it, accuracy);
240  }
241  return oss.str();
242 }
243 
244 
245 template <typename T, typename T_BETWEEN>
246 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
247  std::vector<T> sorted(v);
248  std::sort(sorted.begin(), sorted.end());
249  return joinToString(sorted, between, accuracy);
250 }
251 
252 
253 template <typename T, typename T_BETWEEN>
254 inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
255  std::vector<std::string> ids;
256  for (T* n : ns) {
257  ids.push_back(Named::getIDSecure(n));
258  }
259  return joinToStringSorting(ids, between);
260 }
261 
262 
263 template <typename T, typename C, typename T_BETWEEN>
264 inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
265  std::vector<std::string> ids;
266  for (T* n : ns) {
267  ids.push_back(Named::getIDSecure(n));
268  }
269  return joinToString(ids, between);
270 }
271 
272 
273 template <typename V>
274 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
275  UNUSED_PARAMETER(accuracy);
276  std::vector<std::string> ids;
277  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
278  ids.push_back((*it)->getID());
279  }
280  return joinToStringSorting(ids, " ");
281 }
282 
283 
284 template <>
285 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
286  return joinToString(v, " ", accuracy);
287 }
288 
289 
290 template <>
291 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
292  return joinToString(v, " ", accuracy);
293 }
294 
295 
296 template <>
297 inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
298  return joinToString(v, " ", accuracy);
299 }
300 
301 
302 template <typename T, typename T_BETWEEN>
303 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
304  std::ostringstream oss;
305  bool connect = false;
306  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
307  if (connect) {
308  oss << toString(between, accuracy);
309  } else {
310  connect = true;
311  }
312  oss << toString(*it, accuracy);
313  }
314  return oss.str();
315 }
316 
317 
318 template <>
319 inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
320  return joinToString(v, " ");
321 }
322 
323 
324 template <>
325 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
326  return joinToString(v, " ");
327 }
328 
329 
330 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
331 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
332  std::ostringstream oss;
333  bool connect = false;
334  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
335  if (connect) {
336  oss << toString(between, accuracy);
337  } else {
338  connect = true;
339  }
340  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
341  }
342  return oss.str();
343 }
344 
345 
346 template <>
347 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
348  return joinToString(v, ", ", ":");
349 }
350 
351 
352 #endif
353 
354 /****************************************************************************/
355 
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:122
static StringBijection< SumoXMLNodeType > NodeTypes
node types
std::string joinNamedToString(const std::set< T *, C > &ns, const T_BETWEEN &between)
Definition: ToString.h:264
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:148
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:62
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:254
#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:167
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:129
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:73
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:136
Base class for objects which have an id.
Definition: Named.h:49
std::string toString< LateralAlignment >(const LateralAlignment &latA, std::streamsize accuracy)
Definition: ToString.h:142
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:94
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:115
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:246
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:101
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:80
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:230
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition: ToString.h:108
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:87
TrafficLightType