Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // -------------------
22 /****************************************************************************/
23 #pragma once
24 #include <sstream>
25 #include <string>
26 #include <iomanip>
27 #include <algorithm>
28 #include <list>
31 #include <utils/common/Named.h>
33 #include "StdDefs.h"
34 
35 
36 // ===========================================================================
37 // class definitions
38 // ===========================================================================
43 template <class T>
44 inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
45  std::ostringstream oss;
46  oss.setf(std::ios::fixed, std::ios::floatfield);
47  oss << std::setprecision(accuracy);
48  oss << t;
49  return oss.str();
50 }
51 
52 
53 template<typename T>
54 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
55  // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
56  std::stringstream stream;
57  stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
58  return stream.str();
59 }
60 
61 
62 inline std::string toString(const Named* obj, std::streamsize accuracy) {
63  UNUSED_PARAMETER(accuracy);
64  return Named::getIDSecure(obj);
65 }
66 
67 
68 template <>
69 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
70  UNUSED_PARAMETER(accuracy);
72 }
73 
74 
75 template <>
76 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
77  UNUSED_PARAMETER(accuracy);
79 }
80 
81 
82 template <>
83 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
84  UNUSED_PARAMETER(accuracy);
86 }
87 
88 
89 template <>
90 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
91  UNUSED_PARAMETER(accuracy);
93 }
94 
95 
96 template <>
97 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
98  UNUSED_PARAMETER(accuracy);
99  return SumoVehicleClassStrings.getString(vClass);
100 }
101 
102 
103 template <>
104 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
105  UNUSED_PARAMETER(accuracy);
107 }
108 
109 template <>
110 inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
111  UNUSED_PARAMETER(accuracy);
113 }
114 
115 template <>
116 inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
117  UNUSED_PARAMETER(accuracy);
119 }
120 
121 template <>
122 inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
123  UNUSED_PARAMETER(accuracy);
125 }
126 
127 template <>
128 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
129  UNUSED_PARAMETER(accuracy);
130  return SUMOXMLDefinitions::LinkStates.getString(linkState);
131 }
132 
133 
134 template <>
135 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
136  UNUSED_PARAMETER(accuracy);
138 }
139 
140 
141 template <>
142 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
143  UNUSED_PARAMETER(accuracy);
145 }
146 
147 
148 template <>
149 inline std::string toString<TrafficLightLayout>(const TrafficLightLayout& layout, std::streamsize accuracy) {
150  UNUSED_PARAMETER(accuracy);
152 }
153 
154 
155 template <>
156 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
157  UNUSED_PARAMETER(accuracy);
159 }
160 
161 template <>
162 inline std::string toString<LateralAlignment>(const LateralAlignment& latA, std::streamsize accuracy) {
163  UNUSED_PARAMETER(accuracy);
165 }
166 
167 template <>
168 inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
169  UNUSED_PARAMETER(accuracy);
170  std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
171  bool hadOne = false;
172  std::ostringstream oss;
173  for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
174  if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
175  if (hadOne) {
176  oss << "|";
177  } else {
178  hadOne = true;
179  }
180  oss << (*it);
181  }
182  }
183  return oss.str();
184 }
185 
186 template <>
187 inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
188  return dist.toStr(accuracy);
189 }
190 
191 template <typename V>
192 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
193  return toString<V>(v.begin(), v.end(), accuracy);
194 }
195 
196 template <typename V>
197 inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
198  UNUSED_PARAMETER(accuracy);
199  std::ostringstream oss;
200  for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
201  if (it != b) {
202  oss << " ";
203  }
204  oss << Named::getIDSecure(*it);
205  }
206  return oss.str();
207 }
208 
209 template <typename V>
210 inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
211  return toString<V>(v.begin(), v.end(), accuracy);
212 }
213 
214 template <typename V>
215 inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
216  UNUSED_PARAMETER(accuracy);
217  std::ostringstream oss;
218  for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
219  if (it != b) {
220  oss << " ";
221  }
222  oss << Named::getIDSecure(*it);
223  }
224  return oss.str();
225 }
226 
227 
228 
229 //template <typename V>
230 //inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
231 // return toString<V>(v.begin(), v.end(), accuracy);
232 //}
233 //
234 //
235 //template <typename V>
236 //inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
237 // UNUSED_PARAMETER(accuracy);
238 // std::ostringstream oss;
239 // for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
240 // if (it != b) {
241 // oss << " ";
242 // }
243 // oss << Named::getIDSecure(*it);
244 // }
245 // return oss.str();
246 //}
247 
248 
249 template <typename T, typename T_BETWEEN>
250 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
251  std::ostringstream oss;
252  bool connect = false;
253  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
254  if (connect) {
255  oss << toString(between, accuracy);
256  } else {
257  connect = true;
258  }
259  oss << toString(*it, accuracy);
260  }
261  return oss.str();
262 }
263 
264 
265 template <typename T, typename T_BETWEEN>
266 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
267  std::vector<T> sorted(v);
268  std::sort(sorted.begin(), sorted.end());
269  return joinToString(sorted, between, accuracy);
270 }
271 
272 
273 template <typename T, typename T_BETWEEN>
274 inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
275  std::vector<std::string> ids;
276  for (T* n : ns) {
277  ids.push_back(Named::getIDSecure(n));
278  }
279  return joinToStringSorting(ids, between);
280 }
281 
282 
283 template <typename T, typename C, typename T_BETWEEN>
284 inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
285  std::vector<std::string> ids;
286  for (T* n : ns) {
287  ids.push_back(Named::getIDSecure(n));
288  }
289  return joinToString(ids, between);
290 }
291 
292 
293 template <typename V>
294 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
295  UNUSED_PARAMETER(accuracy);
296  std::vector<std::string> ids;
297  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
298  ids.push_back((*it)->getID());
299  }
300  return joinToStringSorting(ids, " ");
301 }
302 
303 
304 template <>
305 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
306  return joinToString(v, " ", accuracy);
307 }
308 
309 
310 template <>
311 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
312  return joinToString(v, " ", accuracy);
313 }
314 
315 
316 template <>
317 inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
318  return joinToString(v, " ", accuracy);
319 }
320 
321 
322 template <typename T, typename T_BETWEEN>
323 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
324  std::ostringstream oss;
325  bool connect = false;
326  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
327  if (connect) {
328  oss << toString(between, accuracy);
329  } else {
330  connect = true;
331  }
332  oss << toString(*it, accuracy);
333  }
334  return oss.str();
335 }
336 
337 
338 template <>
339 inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
340  return joinToString(v, " ");
341 }
342 
343 
344 template <>
345 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
346  return joinToString(v, " ");
347 }
348 
349 
350 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
351 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
352  std::ostringstream oss;
353  bool connect = false;
354  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
355  if (connect) {
356  oss << toString(between, accuracy);
357  } else {
358  connect = true;
359  }
360  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
361  }
362  return oss.str();
363 }
364 
365 
366 template <>
367 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
368  return joinToString(v, ", ", ":");
369 }
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
TrafficLightType
SumoXMLTag
Numbers representing SUMO-XML - element names.
PersonMode
travel modes for persons
LateralAlignment
Numbers representing special SUMO-XML-attribute values Information how vehicles align themselves with...
TrafficLightLayout
LaneSpreadFunction
Numbers representing special SUMO-XML-attribute values Information how the edge's lateral offset shal...
FringeType
algorithms for computing right of way
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)....
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
LaneChangeAction
The state of a vehicle's lane-change behavior.
LaneChangeModel
RightOfWay
algorithms for computing right of way
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:25
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:54
std::string joinNamedToString(const std::set< T *, C > &ns, const T_BETWEEN &between)
Definition: ToString.h:284
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:266
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:142
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:83
std::string toString< Distribution_Parameterized >(const Distribution_Parameterized &dist, std::streamsize accuracy)
Definition: ToString.h:187
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:90
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:250
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:156
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
std::string toString< FringeType >(const FringeType &fringeType, std::streamsize accuracy)
Definition: ToString.h:116
std::string joinNamedToStringSorting(const std::set< T * > &ns, const T_BETWEEN &between)
Definition: ToString.h:274
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:97
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition: ToString.h:104
std::string toString< LinkState >(const LinkState &linkState, std::streamsize accuracy)
Definition: ToString.h:128
std::string toString< TrafficLightLayout >(const TrafficLightLayout &layout, std::streamsize accuracy)
Definition: ToString.h:149
std::string toString< LateralAlignment >(const LateralAlignment &latA, std::streamsize accuracy)
Definition: ToString.h:162
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition: ToString.h:76
std::string toString< PersonMode >(const PersonMode &personMode, std::streamsize accuracy)
Definition: ToString.h:122
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition: ToString.h:168
std::string toString< SumoXMLTag >(const SumoXMLTag &tag, std::streamsize accuracy)
Definition: ToString.h:69
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:135
std::string toString< RightOfWay >(const RightOfWay &row, std::streamsize accuracy)
Definition: ToString.h:110
std::string toStr(std::streamsize accuracy) const
Returns the string representation of this distribution.
Base class for objects which have an id.
Definition: Named.h:53
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:66
static StringBijection< LaneSpreadFunction > LaneSpreadFunctions
lane spread functions
static StringBijection< LaneChangeAction > LaneChangeActions
lane change actions
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
static StringBijection< PersonMode > PersonModeValues
person modes
static StringBijection< LinkState > LinkStates
link states
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static StringBijection< TrafficLightLayout > TrafficLightLayouts
traffic light layouts
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< LaneChangeModel > LaneChangeModels
lane change models
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
static StringBijection< LinkDirection > LinkDirections
link directions
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
static StringBijection< FringeType > FringeTypeValues
fringe types
const std::string & getString(const T key) const
std::vector< std::string > getStrings() const