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 // for the runtime wrapper see https://github.com/fmtlib/fmt/issues/3107
62 return fmt::format(fmt::runtime("{:.{}f}"), val, accuracy);
63#else
64 std::ostringstream oss;
65 oss.setf(std::ios::fixed, std::ios::floatfield);
66 oss << std::setprecision(accuracy);
67 oss << val;
68 return oss.str();
69#endif
70}
71
72
73template <>
74inline std::string toString(const std::string& val, std::streamsize accuracy) {
75 UNUSED_PARAMETER(accuracy);
76 return val;
77}
78
79
80template<typename T>
81inline std::string toHex(const T i, std::streamsize numDigits = 0) {
82 // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
83 std::stringstream stream;
84 stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
85 return stream.str();
86}
87
88
89inline std::string toString(const Named* obj, std::streamsize accuracy) {
90 UNUSED_PARAMETER(accuracy);
91 return Named::getIDSecure(obj);
92}
93
94
95template <>
96inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
97 UNUSED_PARAMETER(accuracy);
99}
100
101
102template <>
103inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
104 UNUSED_PARAMETER(accuracy);
106}
107
108
109template <>
110inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
111 UNUSED_PARAMETER(accuracy);
113}
114
115
116template <>
117inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
118 UNUSED_PARAMETER(accuracy);
120}
121
122
123template <>
124inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
125 UNUSED_PARAMETER(accuracy);
126 return SumoVehicleClassStrings.getString(vClass);
127}
128
129
130template <>
131inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
132 UNUSED_PARAMETER(accuracy);
134}
135
136template <>
137inline std::string toString<ParkingType>(const ParkingType& pt, std::streamsize accuracy) {
138 UNUSED_PARAMETER(accuracy);
140}
141
142template <>
143inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
144 UNUSED_PARAMETER(accuracy);
146}
147
148template <>
149inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
150 UNUSED_PARAMETER(accuracy);
152}
153
154template <>
155inline std::string toString<RoundaboutType>(const RoundaboutType& roundaboutType, std::streamsize accuracy) {
156 UNUSED_PARAMETER(accuracy);
158}
159
160template <>
161inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
162 UNUSED_PARAMETER(accuracy);
164}
165
166template <>
167inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
168 UNUSED_PARAMETER(accuracy);
170}
171
172
173template <>
174inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
175 UNUSED_PARAMETER(accuracy);
177}
178
179
180template <>
181inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
182 UNUSED_PARAMETER(accuracy);
184}
185
186
187template <>
188inline std::string toString<TrafficLightLayout>(const TrafficLightLayout& layout, std::streamsize accuracy) {
189 UNUSED_PARAMETER(accuracy);
191}
192
193
194template <>
195inline std::string toString<InsertionCheck>(const InsertionCheck& check, std::streamsize accuracy) {
196 UNUSED_PARAMETER(accuracy);
198}
199
200
201template <>
202inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
203 UNUSED_PARAMETER(accuracy);
205}
206
207template <>
208inline std::string toString<LatAlignmentDefinition>(const LatAlignmentDefinition& lad, std::streamsize accuracy) {
209 UNUSED_PARAMETER(accuracy);
210 switch (lad) {
212 return "right";
214 return "center";
216 return "arbitrary";
218 return "nice";
220 return "compact";
222 return "left";
225 default:
226 return "";
227 }
228}
229
230template <>
231inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
232 UNUSED_PARAMETER(accuracy);
233 std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
234 bool hadOne = false;
235 std::ostringstream oss;
236 for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
237 if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
238 if (hadOne) {
239 oss << "|";
240 } else {
241 hadOne = true;
242 }
243 oss << (*it);
244 }
245 }
246 return oss.str();
247}
248
249template <>
250inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
251 return dist.toStr(accuracy);
252}
253
254template <typename V>
255inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
256 return toString<V>(v.begin(), v.end(), accuracy);
257}
258
259
260template <typename V>
261inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
262 UNUSED_PARAMETER(accuracy);
263 std::ostringstream oss;
264 for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
265 if (it != b) {
266 oss << " ";
267 }
268 oss << Named::getIDSecure(*it);
269 }
270 return oss.str();
271}
272
273template <typename V>
274inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
275 return toString<V>(v.begin(), v.end(), accuracy);
276}
277
278template <typename V>
279inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
280 UNUSED_PARAMETER(accuracy);
281 std::ostringstream oss;
282 for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
283 if (it != b) {
284 oss << " ";
285 }
286 oss << Named::getIDSecure(*it);
287 }
288 return oss.str();
289}
290
291
292
293//template <typename V>
294//inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
295// return toString<V>(v.begin(), v.end(), accuracy);
296//}
297//
298//
299//template <typename V>
300//inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
301// UNUSED_PARAMETER(accuracy);
302// std::ostringstream oss;
303// for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
304// if (it != b) {
305// oss << " ";
306// }
307// oss << Named::getIDSecure(*it);
308// }
309// return oss.str();
310//}
311
312
313template <typename T, typename T_BETWEEN>
314inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
315 std::ostringstream oss;
316 bool connect = false;
317 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
318 if (connect) {
319 oss << toString(between, accuracy);
320 } else {
321 connect = true;
322 }
323 oss << toString(*it, accuracy);
324 }
325 return oss.str();
326}
327
328
329template <typename T, typename T_BETWEEN>
330inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
331 std::vector<T> sorted(v);
332 std::sort(sorted.begin(), sorted.end());
333 return joinToString(sorted, between, accuracy);
334}
335
336
337template <typename T, typename T_BETWEEN>
338inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
339 std::vector<std::string> ids;
340 for (T* n : ns) {
341 ids.push_back(Named::getIDSecure(n));
342 }
343 return joinToStringSorting(ids, between);
344}
345
346template <typename T, typename T_BETWEEN>
347inline std::string joinNamedToStringSorting(const std::set<T*, ComparatorIdLess>& ns, const T_BETWEEN& between) {
348 std::vector<std::string> ids;
349 for (T* n : ns) {
350 ids.push_back(Named::getIDSecure(n));
351 }
352 return joinToStringSorting(ids, between);
353}
354
355
356template <typename T, typename C, typename T_BETWEEN>
357inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
358 std::vector<std::string> ids;
359 for (T* n : ns) {
360 ids.push_back(Named::getIDSecure(n));
361 }
362 return joinToString(ids, between);
363}
364
365
366template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
367inline std::string joinNamedToString(const std::map<KEY, VAL, ComparatorIdLess>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
368 std::ostringstream oss;
369 bool connect = false;
370 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
371 if (connect) {
372 oss << toString(between, accuracy);
373 } else {
374 connect = true;
375 }
376 oss << Named::getIDSecure(it->first) << between_keyval << toString(it->second, accuracy);
377 }
378 return oss.str();
379}
380
381
382template <typename V>
383inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
384 UNUSED_PARAMETER(accuracy);
385 std::vector<std::string> ids;
386 for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
387 ids.push_back((*it)->getID());
388 }
389 return joinToStringSorting(ids, " ");
390}
391
392
393template <typename V>
394inline std::string toString(const std::set<V*, ComparatorNumericalIdLess>& v, std::streamsize accuracy = gPrecision) {
395 UNUSED_PARAMETER(accuracy);
396 std::vector<std::string> ids;
397 for (typename std::set<V*, ComparatorNumericalIdLess>::const_iterator it = v.begin(); it != v.end(); ++it) {
398 ids.push_back((*it)->getID());
399 }
400 return joinToStringSorting(ids, " ");
401}
402
403
404template <>
405inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
406 return joinToString(v, " ", accuracy);
407}
408
409
410template <>
411inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
412 return joinToString(v, " ", accuracy);
413}
414
415
416template <>
417inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
418 return joinToString(v, " ", accuracy);
419}
420
421
422template <typename V, typename W>
423inline std::string toString(const std::vector<std::pair<V, W> >& v, std::streamsize accuracy = gPrecision, const std::string& between = ";", const std::string& between2 = ",") {
424 std::ostringstream oss;
425 oss << std::setprecision(accuracy);
426 bool connect = false;
427 for (auto it : v) {
428 if (connect) {
429 oss << toString(between, accuracy);
430 } else {
431 connect = true;
432 }
433 oss << toString(it.first) << between2 << toString(it.second);
434 }
435 return oss.str();
436}
437
438
439template <typename T, typename T_BETWEEN>
440inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
441 std::ostringstream oss;
442 bool connect = false;
443 for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
444 if (connect) {
445 oss << toString(between, accuracy);
446 } else {
447 connect = true;
448 }
449 oss << toString(*it, accuracy);
450 }
451 return oss.str();
452}
453
454
455template <>
456inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
457 return joinToString(v, " ");
458}
459
460
461template <>
462inline std::string toString(const std::set<std::string>& v, std::streamsize) {
463 return joinToString(v, " ");
464}
465
466
467template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
468inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
469 std::ostringstream oss;
470 bool connect = false;
471 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
472 if (connect) {
473 oss << toString(between, accuracy);
474 } else {
475 connect = true;
476 }
477 oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
478 }
479 return oss.str();
480}
481
482
483template <>
484inline std::string toString(const Parameterised::Map& v, std::streamsize) {
485 return joinToString(v, ", ", ":");
486}
487
488template <>
489inline std::string toString(const MMVersion& v, std::streamsize) {
490 // we only need higher accuracy on the minor version for hotfix releases
491 return toString(v.first) + "." + toString(v.second, 0);
492}
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:81
std::string joinNamedToString(const std::set< T *, C > &ns, const T_BETWEEN &between)
Definition ToString.h:357
std::string toString< ParkingType >(const ParkingType &pt, std::streamsize accuracy)
Definition ToString.h:137
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:330
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition ToString.h:181
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition ToString.h:110
std::string toString< Distribution_Parameterized >(const Distribution_Parameterized &dist, std::streamsize accuracy)
Definition ToString.h:250
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition ToString.h:117
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:314
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition ToString.h:202
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:195
std::string toString< RoundaboutType >(const RoundaboutType &roundaboutType, std::streamsize accuracy)
Definition ToString.h:155
std::string toString< FringeType >(const FringeType &fringeType, std::streamsize accuracy)
Definition ToString.h:149
std::string joinNamedToStringSorting(const std::set< T * > &ns, const T_BETWEEN &between)
Definition ToString.h:338
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition ToString.h:124
std::string toString< LatAlignmentDefinition >(const LatAlignmentDefinition &lad, std::streamsize accuracy)
Definition ToString.h:208
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition ToString.h:131
std::string toString< LinkState >(const LinkState &linkState, std::streamsize accuracy)
Definition ToString.h:167
std::string toString< TrafficLightLayout >(const TrafficLightLayout &layout, std::streamsize accuracy)
Definition ToString.h:188
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition ToString.h:103
std::string toString< PersonMode >(const PersonMode &personMode, std::streamsize accuracy)
Definition ToString.h:161
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition ToString.h:231
std::string toString< SumoXMLTag >(const SumoXMLTag &tag, std::streamsize accuracy)
Definition ToString.h:96
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition ToString.h:174
std::string toString< RightOfWay >(const RightOfWay &row, std::streamsize accuracy)
Definition ToString.h:143
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
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)