Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
ToString.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2002-2026 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>
30#ifdef HAVE_FMT
31#include <fmt/format.h>
32#endif
35#include <utils/common/Named.h>
38#include "StdDefs.h"
39
40
41// ===========================================================================
42// class definitions
43// ===========================================================================
48template <class T>
49inline 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
58template <>
59inline std::string toString(const double& val, std::streamsize accuracy) {
60#ifdef HAVE_FMT
61 return fmt::format("{:.{}f}", val, accuracy);
62#else
63 std::ostringstream oss;
64 oss.setf(std::ios::fixed, std::ios::floatfield);
65 oss << std::setprecision(accuracy);
66 oss << val;
67 return oss.str();
68#endif
69}
70
71
72template <>
73inline std::string toString(const std::string& val, std::streamsize accuracy) {
74 UNUSED_PARAMETER(accuracy);
75 return val;
76}
77
78
79template<typename T>
80inline std::string toHex(const T i, std::streamsize numDigits = 0) {
81 // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
82 std::stringstream stream;
83 stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
84 return stream.str();
85}
86
87
88inline std::string toString(const Named* obj, std::streamsize accuracy) {
89 UNUSED_PARAMETER(accuracy);
90 return Named::getIDSecure(obj);
91}
92
93
94template <>
95inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
96 UNUSED_PARAMETER(accuracy);
98}
99
100
101template <>
102inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
103 UNUSED_PARAMETER(accuracy);
105}
106
107
108template <>
109inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
110 UNUSED_PARAMETER(accuracy);
112}
113
114
115template <>
116inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
117 UNUSED_PARAMETER(accuracy);
119}
120
121
122template <>
123inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
124 UNUSED_PARAMETER(accuracy);
125 return SumoVehicleClassStrings.getString(vClass);
126}
127
128
129template <>
130inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
131 UNUSED_PARAMETER(accuracy);
133}
134
135template <>
136inline std::string toString<ParkingType>(const ParkingType& pt, std::streamsize accuracy) {
137 UNUSED_PARAMETER(accuracy);
139}
140
141template <>
142inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
143 UNUSED_PARAMETER(accuracy);
145}
146
147template <>
148inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
149 UNUSED_PARAMETER(accuracy);
151}
152
153template <>
154inline std::string toString<RoundaboutType>(const RoundaboutType& roundaboutType, std::streamsize accuracy) {
155 UNUSED_PARAMETER(accuracy);
157}
158
159template <>
160inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
161 UNUSED_PARAMETER(accuracy);
163}
164
165template <>
166inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
167 UNUSED_PARAMETER(accuracy);
169}
170
171
172template <>
173inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
174 UNUSED_PARAMETER(accuracy);
176}
177
178
179template <>
180inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
181 UNUSED_PARAMETER(accuracy);
183}
184
185
186template <>
187inline std::string toString<TrafficLightLayout>(const TrafficLightLayout& layout, std::streamsize accuracy) {
188 UNUSED_PARAMETER(accuracy);
190}
191
192
193template <>
194inline std::string toString<InsertionCheck>(const InsertionCheck& check, std::streamsize accuracy) {
195 UNUSED_PARAMETER(accuracy);
197}
198
199
200template <>
201inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
202 UNUSED_PARAMETER(accuracy);
204}
205
206template <>
207inline std::string toString<LatAlignmentDefinition>(const LatAlignmentDefinition& lad, std::streamsize accuracy) {
208 UNUSED_PARAMETER(accuracy);
209 switch (lad) {
211 return "right";
213 return "center";
215 return "arbitrary";
217 return "nice";
219 return "compact";
221 return "left";
224 default:
225 return "";
226 }
227}
228
229template <>
230inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
231 UNUSED_PARAMETER(accuracy);
232 std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
233 bool hadOne = false;
234 std::ostringstream oss;
235 for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
236 if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
237 if (hadOne) {
238 oss << "|";
239 } else {
240 hadOne = true;
241 }
242 oss << (*it);
243 }
244 }
245 return oss.str();
246}
247
248template <>
249inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
250 return dist.toStr(accuracy);
251}
252
253template <typename V>
254inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
255 return toString<V>(v.begin(), v.end(), accuracy);
256}
257
258
259template <typename V>
260inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
261 UNUSED_PARAMETER(accuracy);
262 std::ostringstream oss;
263 for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
264 if (it != b) {
265 oss << " ";
266 }
267 oss << Named::getIDSecure(*it);
268 }
269 return oss.str();
270}
271
272template <typename V>
273inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
274 return toString<V>(v.begin(), v.end(), accuracy);
275}
276
277template <typename V>
278inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
279 UNUSED_PARAMETER(accuracy);
280 std::ostringstream oss;
281 for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
282 if (it != b) {
283 oss << " ";
284 }
285 oss << Named::getIDSecure(*it);
286 }
287 return oss.str();
288}
289
290
291
292//template <typename V>
293//inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
294// return toString<V>(v.begin(), v.end(), accuracy);
295//}
296//
297//
298//template <typename V>
299//inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
300// UNUSED_PARAMETER(accuracy);
301// std::ostringstream oss;
302// for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
303// if (it != b) {
304// oss << " ";
305// }
306// oss << Named::getIDSecure(*it);
307// }
308// return oss.str();
309//}
310
311
312template <typename T, typename T_BETWEEN>
313inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
314 std::ostringstream oss;
315 bool connect = false;
316 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
317 if (connect) {
318 oss << toString(between, accuracy);
319 } else {
320 connect = true;
321 }
322 oss << toString(*it, accuracy);
323 }
324 return oss.str();
325}
326
327
328template <typename T, typename T_BETWEEN>
329inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
330 std::vector<T> sorted(v);
331 std::sort(sorted.begin(), sorted.end());
332 return joinToString(sorted, between, accuracy);
333}
334
335
336template <typename T, typename T_BETWEEN>
337inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
338 std::vector<std::string> ids;
339 for (T* n : ns) {
340 ids.push_back(Named::getIDSecure(n));
341 }
342 return joinToStringSorting(ids, between);
343}
344
345template <typename T, typename T_BETWEEN>
346inline std::string joinNamedToStringSorting(const std::set<T*, ComparatorIdLess>& ns, const T_BETWEEN& between) {
347 std::vector<std::string> ids;
348 for (T* n : ns) {
349 ids.push_back(Named::getIDSecure(n));
350 }
351 return joinToStringSorting(ids, between);
352}
353
354
355template <typename T, typename C, typename T_BETWEEN>
356inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
357 std::vector<std::string> ids;
358 for (T* n : ns) {
359 ids.push_back(Named::getIDSecure(n));
360 }
361 return joinToString(ids, between);
362}
363
364
365template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
366inline std::string joinNamedToString(const std::map<KEY, VAL, ComparatorIdLess>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
367 std::ostringstream oss;
368 bool connect = false;
369 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
370 if (connect) {
371 oss << toString(between, accuracy);
372 } else {
373 connect = true;
374 }
375 oss << Named::getIDSecure(it->first) << between_keyval << toString(it->second, accuracy);
376 }
377 return oss.str();
378}
379
380
381template <typename V>
382inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
383 UNUSED_PARAMETER(accuracy);
384 std::vector<std::string> ids;
385 for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
386 ids.push_back((*it)->getID());
387 }
388 return joinToStringSorting(ids, " ");
389}
390
391
392template <typename V>
393inline std::string toString(const std::set<V*, ComparatorNumericalIdLess>& v, std::streamsize accuracy = gPrecision) {
394 UNUSED_PARAMETER(accuracy);
395 std::vector<std::string> ids;
396 for (typename std::set<V*, ComparatorNumericalIdLess>::const_iterator it = v.begin(); it != v.end(); ++it) {
397 ids.push_back((*it)->getID());
398 }
399 return joinToStringSorting(ids, " ");
400}
401
402
403template <>
404inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
405 return joinToString(v, " ", accuracy);
406}
407
408
409template <>
410inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
411 return joinToString(v, " ", accuracy);
412}
413
414
415template <>
416inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
417 return joinToString(v, " ", accuracy);
418}
419
420
421template <typename V, typename W>
422inline std::string toString(const std::vector<std::pair<V, W> >& v, std::streamsize accuracy = gPrecision, const std::string& between = ";", const std::string& between2 = ",") {
423 std::ostringstream oss;
424 oss << std::setprecision(accuracy);
425 bool connect = false;
426 for (auto it : v) {
427 if (connect) {
428 oss << toString(between, accuracy);
429 } else {
430 connect = true;
431 }
432 oss << toString(it.first) << between2 << toString(it.second);
433 }
434 return oss.str();
435}
436
437
438template <typename T, typename T_BETWEEN>
439inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
440 std::ostringstream oss;
441 bool connect = false;
442 for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
443 if (connect) {
444 oss << toString(between, accuracy);
445 } else {
446 connect = true;
447 }
448 oss << toString(*it, accuracy);
449 }
450 return oss.str();
451}
452
453
454template <>
455inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
456 return joinToString(v, " ");
457}
458
459
460template <>
461inline std::string toString(const std::set<std::string>& v, std::streamsize) {
462 return joinToString(v, " ");
463}
464
465
466template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
467inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
468 std::ostringstream oss;
469 bool connect = false;
470 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
471 if (connect) {
472 oss << toString(between, accuracy);
473 } else {
474 connect = true;
475 }
476 oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
477 }
478 return oss.str();
479}
480
481
482template <>
483inline std::string toString(const Parameterised::Map& v, std::streamsize) {
484 return joinToString(v, ", ", ":");
485}
486
487template <>
488inline std::string toString(const MMVersion& v, std::streamsize) {
489 // we only need higher accuracy on the minor version for hotfix releases
490 return toString(v.first) + "." + toString(v.second, 0);
491}
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.
InsertionCheck
different checking levels for vehicle insertion
SumoXMLTag
Numbers representing SUMO-XML - element names.
PersonMode
travel modes for persons
ParkingType
Numbers representing special SUMO-XML-attribute values Information on whether a car is parking on the...
TrafficLightLayout
LaneSpreadFunction
Numbers representing special SUMO-XML-attribute values Information how the edge's lateral offset shal...
RoundaboutType
classifying roundabout type for nodes
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.
RightOfWay
algorithms for computing right of way
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
int gPrecision
the precision for floating point outputs
Definition StdDefs.cpp:27
std::pair< int, double > MMVersion
(M)ajor/(M)inor version for written networks and default version for loading
Definition StdDefs.h:71
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:80
std::string joinNamedToString(const std::set< T *, C > &ns, const T_BETWEEN &between)
Definition ToString.h:356
std::string toString< ParkingType >(const ParkingType &pt, std::streamsize accuracy)
Definition ToString.h:136
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:329
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition ToString.h:180
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition ToString.h:109
std::string toString< Distribution_Parameterized >(const Distribution_Parameterized &dist, std::streamsize accuracy)
Definition ToString.h:249
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition ToString.h:116
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:313
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition ToString.h:201
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
std::string toString< InsertionCheck >(const InsertionCheck &check, std::streamsize accuracy)
Definition ToString.h:194
std::string toString< RoundaboutType >(const RoundaboutType &roundaboutType, std::streamsize accuracy)
Definition ToString.h:154
std::string toString< FringeType >(const FringeType &fringeType, std::streamsize accuracy)
Definition ToString.h:148
std::string joinNamedToStringSorting(const std::set< T * > &ns, const T_BETWEEN &between)
Definition ToString.h:337
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition ToString.h:123
std::string toString< LatAlignmentDefinition >(const LatAlignmentDefinition &lad, std::streamsize accuracy)
Definition ToString.h:207
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition ToString.h:130
std::string toString< LinkState >(const LinkState &linkState, std::streamsize accuracy)
Definition ToString.h:166
std::string toString< TrafficLightLayout >(const TrafficLightLayout &layout, std::streamsize accuracy)
Definition ToString.h:187
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition ToString.h:102
std::string toString< PersonMode >(const PersonMode &personMode, std::streamsize accuracy)
Definition ToString.h:160
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition ToString.h:230
std::string toString< SumoXMLTag >(const SumoXMLTag &tag, std::streamsize accuracy)
Definition ToString.h:95
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition ToString.h:173
std::string toString< RightOfWay >(const RightOfWay &row, std::streamsize accuracy)
Definition ToString.h:142
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
std::map< std::string, std::string > Map
parameters map
static StringBijection< LaneSpreadFunction > LaneSpreadFunctions
lane spread functions
static StringBijection< LaneChangeAction > LaneChangeActions
lane change actions
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static StringBijection< InsertionCheck > InsertionChecks
traffic light layouts
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
static StringBijection< PersonMode > PersonModeValues
person modes
static StringBijection< LinkState > LinkStates
link states
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static StringBijection< TrafficLightLayout > TrafficLightLayouts
traffic light layouts
static SequentialStringBijection Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< ParkingType > ParkingTypes
parking types
static StringBijection< LaneChangeModel > LaneChangeModels
lane change models
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
static StringBijection< LinkDirection > LinkDirections
link directions
static StringBijection< RoundaboutType > RoundaboutTypeValues
fringe types
static StringBijection< FringeType > FringeTypeValues
fringe types
const std::string & getString(int key) const
const std::string & getString(const T key) const
get string
std::vector< std::string > getStrings() const
get all strings
#define UNUSED_PARAMETER(x)