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