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-2022 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 <config.h>
25 #include <sstream>
26 #include <string>
27 #include <iomanip>
28 #include <algorithm>
29 #include <list>
32 #include <utils/common/Named.h>
35 #include "StdDefs.h"
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
45 template <class T>
46 inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
47  std::ostringstream oss;
48  oss.setf(std::ios::fixed, std::ios::floatfield);
49  oss << std::setprecision(accuracy);
50  oss << t;
51  return oss.str();
52 }
53 
54 
55 template<typename T>
56 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
57  // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
58  std::stringstream stream;
59  stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
60  return stream.str();
61 }
62 
63 
64 inline std::string toString(const Named* obj, std::streamsize accuracy) {
65  UNUSED_PARAMETER(accuracy);
66  return Named::getIDSecure(obj);
67 }
68 
69 
70 template <>
71 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
72  UNUSED_PARAMETER(accuracy);
74 }
75 
76 
77 template <>
78 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
79  UNUSED_PARAMETER(accuracy);
81 }
82 
83 
84 template <>
85 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
86  UNUSED_PARAMETER(accuracy);
88 }
89 
90 
91 template <>
92 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
93  UNUSED_PARAMETER(accuracy);
95 }
96 
97 
98 template <>
99 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
100  UNUSED_PARAMETER(accuracy);
101  return SumoVehicleClassStrings.getString(vClass);
102 }
103 
104 
105 template <>
106 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
107  UNUSED_PARAMETER(accuracy);
109 }
110 
111 template <>
112 inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
113  UNUSED_PARAMETER(accuracy);
115 }
116 
117 template <>
118 inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
119  UNUSED_PARAMETER(accuracy);
121 }
122 
123 template <>
124 inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
125  UNUSED_PARAMETER(accuracy);
127 }
128 
129 template <>
130 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
131  UNUSED_PARAMETER(accuracy);
132  return SUMOXMLDefinitions::LinkStates.getString(linkState);
133 }
134 
135 
136 template <>
137 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
138  UNUSED_PARAMETER(accuracy);
140 }
141 
142 
143 template <>
144 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
145  UNUSED_PARAMETER(accuracy);
147 }
148 
149 
150 template <>
151 inline std::string toString<TrafficLightLayout>(const TrafficLightLayout& layout, std::streamsize accuracy) {
152  UNUSED_PARAMETER(accuracy);
154 }
155 
156 
157 template <>
158 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
159  UNUSED_PARAMETER(accuracy);
161 }
162 
163 template <>
164 inline std::string toString<LatAlignmentDefinition>(const LatAlignmentDefinition& lad, std::streamsize accuracy) {
165  UNUSED_PARAMETER(accuracy);
166  switch (lad) {
168  return "right";
170  return "center";
172  return "arbitrary";
174  return "nice";
176  return "compact";
178  return "left";
181  default:
182  return "";
183  }
184 }
185 
186 template <>
187 inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
188  UNUSED_PARAMETER(accuracy);
189  std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
190  bool hadOne = false;
191  std::ostringstream oss;
192  for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
193  if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
194  if (hadOne) {
195  oss << "|";
196  } else {
197  hadOne = true;
198  }
199  oss << (*it);
200  }
201  }
202  return oss.str();
203 }
204 
205 template <>
206 inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
207  return dist.toStr(accuracy);
208 }
209 
210 template <typename V>
211 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
212  return toString<V>(v.begin(), v.end(), accuracy);
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 template <typename V>
229 inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
230  return toString<V>(v.begin(), v.end(), accuracy);
231 }
232 
233 template <typename V>
234 inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
235  UNUSED_PARAMETER(accuracy);
236  std::ostringstream oss;
237  for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
238  if (it != b) {
239  oss << " ";
240  }
241  oss << Named::getIDSecure(*it);
242  }
243  return oss.str();
244 }
245 
246 
247 
248 //template <typename V>
249 //inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
250 // return toString<V>(v.begin(), v.end(), accuracy);
251 //}
252 //
253 //
254 //template <typename V>
255 //inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
256 // UNUSED_PARAMETER(accuracy);
257 // std::ostringstream oss;
258 // for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
259 // if (it != b) {
260 // oss << " ";
261 // }
262 // oss << Named::getIDSecure(*it);
263 // }
264 // return oss.str();
265 //}
266 
267 
268 template <typename T, typename T_BETWEEN>
269 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
270  std::ostringstream oss;
271  bool connect = false;
272  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
273  if (connect) {
274  oss << toString(between, accuracy);
275  } else {
276  connect = true;
277  }
278  oss << toString(*it, accuracy);
279  }
280  return oss.str();
281 }
282 
283 
284 template <typename T, typename T_BETWEEN>
285 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
286  std::vector<T> sorted(v);
287  std::sort(sorted.begin(), sorted.end());
288  return joinToString(sorted, between, accuracy);
289 }
290 
291 
292 template <typename T, typename T_BETWEEN>
293 inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
294  std::vector<std::string> ids;
295  for (T* n : ns) {
296  ids.push_back(Named::getIDSecure(n));
297  }
298  return joinToStringSorting(ids, between);
299 }
300 
301 
302 template <typename T, typename C, typename T_BETWEEN>
303 inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
304  std::vector<std::string> ids;
305  for (T* n : ns) {
306  ids.push_back(Named::getIDSecure(n));
307  }
308  return joinToString(ids, between);
309 }
310 
311 
312 template <typename V>
313 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
314  UNUSED_PARAMETER(accuracy);
315  std::vector<std::string> ids;
316  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
317  ids.push_back((*it)->getID());
318  }
319  return joinToStringSorting(ids, " ");
320 }
321 
322 
323 template <>
324 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
325  return joinToString(v, " ", accuracy);
326 }
327 
328 
329 template <>
330 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
331  return joinToString(v, " ", accuracy);
332 }
333 
334 
335 template <>
336 inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
337  return joinToString(v, " ", accuracy);
338 }
339 
340 
341 template <typename T, typename T_BETWEEN>
342 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
343  std::ostringstream oss;
344  bool connect = false;
345  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
346  if (connect) {
347  oss << toString(between, accuracy);
348  } else {
349  connect = true;
350  }
351  oss << toString(*it, accuracy);
352  }
353  return oss.str();
354 }
355 
356 
357 template <>
358 inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
359  return joinToString(v, " ");
360 }
361 
362 
363 template <>
364 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
365  return joinToString(v, " ");
366 }
367 
368 
369 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
370 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
371  std::ostringstream oss;
372  bool connect = false;
373  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
374  if (connect) {
375  oss << toString(between, accuracy);
376  } else {
377  connect = true;
378  }
379  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
380  }
381  return oss.str();
382 }
383 
384 
385 template <>
386 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
387  return joinToString(v, ", ", ":");
388 }
LatAlignmentDefinition
Possible ways to choose the lateral alignment, i.e., how vehicles align themselves within their lane.
@ RIGHT
drive on the right side
@ GIVEN
The alignment as offset is given.
@ DEFAULT
No information given; use default.
@ LEFT
drive on the left side
@ ARBITRARY
maintain the current alignment
@ NICE
align with the closest sublane border
@ COMPACT
align with the rightmost sublane that allows keeping the current speed
@ CENTER
drive in the middle
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
TrafficLightLayout
LaneSpreadFunction
Numbers representing special SUMO-XML-attribute values Information how the edge's lateral offset shal...
FringeType
classifying boundary nodes
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:30
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:56
std::string joinNamedToString(const std::set< T *, C > &ns, const T_BETWEEN &between)
Definition: ToString.h:303
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:285
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:144
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:85
std::string toString< Distribution_Parameterized >(const Distribution_Parameterized &dist, std::streamsize accuracy)
Definition: ToString.h:206
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:92
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:269
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:158
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
std::string toString< FringeType >(const FringeType &fringeType, std::streamsize accuracy)
Definition: ToString.h:118
std::string joinNamedToStringSorting(const std::set< T * > &ns, const T_BETWEEN &between)
Definition: ToString.h:293
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:99
std::string toString< LatAlignmentDefinition >(const LatAlignmentDefinition &lad, std::streamsize accuracy)
Definition: ToString.h:164
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition: ToString.h:106
std::string toString< LinkState >(const LinkState &linkState, std::streamsize accuracy)
Definition: ToString.h:130
std::string toString< TrafficLightLayout >(const TrafficLightLayout &layout, std::streamsize accuracy)
Definition: ToString.h:151
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition: ToString.h:78
std::string toString< PersonMode >(const PersonMode &personMode, std::streamsize accuracy)
Definition: ToString.h:124
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition: ToString.h:187
std::string toString< SumoXMLTag >(const SumoXMLTag &tag, std::streamsize accuracy)
Definition: ToString.h:71
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:137
std::string toString< RightOfWay >(const RightOfWay &row, std::streamsize accuracy)
Definition: ToString.h:112
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:54
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:67
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< 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