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-2025 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// ===========================================================================
45template <class T>
46inline 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
55template<typename T>
56inline 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
64inline std::string toString(const Named* obj, std::streamsize accuracy) {
65 UNUSED_PARAMETER(accuracy);
66 return Named::getIDSecure(obj);
67}
68
69
70template <>
71inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
72 UNUSED_PARAMETER(accuracy);
74}
75
76
77template <>
78inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
79 UNUSED_PARAMETER(accuracy);
81}
82
83
84template <>
85inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
86 UNUSED_PARAMETER(accuracy);
88}
89
90
91template <>
92inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
93 UNUSED_PARAMETER(accuracy);
95}
96
97
98template <>
99inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
100 UNUSED_PARAMETER(accuracy);
101 return SumoVehicleClassStrings.getString(vClass);
102}
103
104
105template <>
106inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
107 UNUSED_PARAMETER(accuracy);
109}
110
111template <>
112inline std::string toString<ParkingType>(const ParkingType& pt, std::streamsize accuracy) {
113 UNUSED_PARAMETER(accuracy);
115}
116
117template <>
118inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
119 UNUSED_PARAMETER(accuracy);
121}
122
123template <>
124inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
125 UNUSED_PARAMETER(accuracy);
127}
128
129template <>
130inline std::string toString<RoundaboutType>(const RoundaboutType& roundaboutType, std::streamsize accuracy) {
131 UNUSED_PARAMETER(accuracy);
133}
134
135template <>
136inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
137 UNUSED_PARAMETER(accuracy);
139}
140
141template <>
142inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
143 UNUSED_PARAMETER(accuracy);
145}
146
147
148template <>
149inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
150 UNUSED_PARAMETER(accuracy);
152}
153
154
155template <>
156inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
157 UNUSED_PARAMETER(accuracy);
159}
160
161
162template <>
163inline std::string toString<TrafficLightLayout>(const TrafficLightLayout& layout, std::streamsize accuracy) {
164 UNUSED_PARAMETER(accuracy);
166}
167
168
169template <>
170inline std::string toString<InsertionCheck>(const InsertionCheck& check, std::streamsize accuracy) {
171 UNUSED_PARAMETER(accuracy);
173}
174
175
176template <>
177inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
178 UNUSED_PARAMETER(accuracy);
180}
181
182template <>
183inline std::string toString<LatAlignmentDefinition>(const LatAlignmentDefinition& lad, std::streamsize accuracy) {
184 UNUSED_PARAMETER(accuracy);
185 switch (lad) {
187 return "right";
189 return "center";
191 return "arbitrary";
193 return "nice";
195 return "compact";
197 return "left";
200 default:
201 return "";
202 }
203}
204
205template <>
206inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
207 UNUSED_PARAMETER(accuracy);
208 std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
209 bool hadOne = false;
210 std::ostringstream oss;
211 for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
212 if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
213 if (hadOne) {
214 oss << "|";
215 } else {
216 hadOne = true;
217 }
218 oss << (*it);
219 }
220 }
221 return oss.str();
222}
223
224template <>
225inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
226 return dist.toStr(accuracy);
227}
228
229template <typename V>
230inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
231 return toString<V>(v.begin(), v.end(), accuracy);
232}
233
234
235template <typename V>
236inline 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
248template <typename V>
249inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
250 return toString<V>(v.begin(), v.end(), accuracy);
251}
252
253template <typename V>
254inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
255 UNUSED_PARAMETER(accuracy);
256 std::ostringstream oss;
257 for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
258 if (it != b) {
259 oss << " ";
260 }
261 oss << Named::getIDSecure(*it);
262 }
263 return oss.str();
264}
265
266
267
268//template <typename V>
269//inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
270// return toString<V>(v.begin(), v.end(), accuracy);
271//}
272//
273//
274//template <typename V>
275//inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
276// UNUSED_PARAMETER(accuracy);
277// std::ostringstream oss;
278// for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
279// if (it != b) {
280// oss << " ";
281// }
282// oss << Named::getIDSecure(*it);
283// }
284// return oss.str();
285//}
286
287
288template <typename T, typename T_BETWEEN>
289inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
290 std::ostringstream oss;
291 bool connect = false;
292 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
293 if (connect) {
294 oss << toString(between, accuracy);
295 } else {
296 connect = true;
297 }
298 oss << toString(*it, accuracy);
299 }
300 return oss.str();
301}
302
303
304template <typename T, typename T_BETWEEN>
305inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
306 std::vector<T> sorted(v);
307 std::sort(sorted.begin(), sorted.end());
308 return joinToString(sorted, between, accuracy);
309}
310
311
312template <typename T, typename T_BETWEEN>
313inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
314 std::vector<std::string> ids;
315 for (T* n : ns) {
316 ids.push_back(Named::getIDSecure(n));
317 }
318 return joinToStringSorting(ids, between);
319}
320
321template <typename T, typename T_BETWEEN>
322inline std::string joinNamedToStringSorting(const std::set<T*, ComparatorIdLess>& ns, const T_BETWEEN& between) {
323 std::vector<std::string> ids;
324 for (T* n : ns) {
325 ids.push_back(Named::getIDSecure(n));
326 }
327 return joinToStringSorting(ids, between);
328}
329
330
331template <typename T, typename C, typename T_BETWEEN>
332inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
333 std::vector<std::string> ids;
334 for (T* n : ns) {
335 ids.push_back(Named::getIDSecure(n));
336 }
337 return joinToString(ids, between);
338}
339
340
341template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
342inline std::string joinNamedToString(const std::map<KEY, VAL, ComparatorIdLess>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
343 std::ostringstream oss;
344 bool connect = false;
345 for (typename std::map<KEY, VAL>::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 << Named::getIDSecure(it->first) << between_keyval << toString(it->second, accuracy);
352 }
353 return oss.str();
354}
355
356
357template <typename V>
358inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
359 UNUSED_PARAMETER(accuracy);
360 std::vector<std::string> ids;
361 for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
362 ids.push_back((*it)->getID());
363 }
364 return joinToStringSorting(ids, " ");
365}
366
367
368template <>
369inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
370 return joinToString(v, " ", accuracy);
371}
372
373
374template <>
375inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
376 return joinToString(v, " ", accuracy);
377}
378
379
380template <>
381inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
382 return joinToString(v, " ", accuracy);
383}
384
385
386template <typename V, typename W>
387inline std::string toString(const std::vector<std::pair<V, W> >& v, std::streamsize accuracy = gPrecision, const std::string& between = ";", const std::string& between2 = ",") {
388 std::ostringstream oss;
389 oss << std::setprecision(accuracy);
390 bool connect = false;
391 for (auto it : v) {
392 if (connect) {
393 oss << toString(between, accuracy);
394 } else {
395 connect = true;
396 }
397 oss << toString(it.first) << between2 << toString(it.second);
398 }
399 return oss.str();
400}
401
402
403template <typename T, typename T_BETWEEN>
404inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
405 std::ostringstream oss;
406 bool connect = false;
407 for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
408 if (connect) {
409 oss << toString(between, accuracy);
410 } else {
411 connect = true;
412 }
413 oss << toString(*it, accuracy);
414 }
415 return oss.str();
416}
417
418
419template <>
420inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
421 return joinToString(v, " ");
422}
423
424
425template <>
426inline std::string toString(const std::set<std::string>& v, std::streamsize) {
427 return joinToString(v, " ");
428}
429
430
431template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
432inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
433 std::ostringstream oss;
434 bool connect = false;
435 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
436 if (connect) {
437 oss << toString(between, accuracy);
438 } else {
439 connect = true;
440 }
441 oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
442 }
443 return oss.str();
444}
445
446
447template <>
448inline std::string toString(const Parameterised::Map& v, std::streamsize) {
449 return joinToString(v, ", ", ":");
450}
451
452template <>
453inline std::string toString(const MMVersion& v, std::streamsize) {
454 // we only need higher accuracy on the minor version for hotfix releases
455 return toString(v.first) + "." + toString(v.second, 0);
456}
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:56
std::string joinNamedToString(const std::set< T *, C > &ns, const T_BETWEEN &between)
Definition ToString.h:332
std::string toString< ParkingType >(const ParkingType &pt, std::streamsize accuracy)
Definition ToString.h:112
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:305
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition ToString.h:156
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:225
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:289
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition ToString.h:177
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
std::string toString< InsertionCheck >(const InsertionCheck &check, std::streamsize accuracy)
Definition ToString.h:170
std::string toString< RoundaboutType >(const RoundaboutType &roundaboutType, std::streamsize accuracy)
Definition ToString.h:130
std::string toString< FringeType >(const FringeType &fringeType, std::streamsize accuracy)
Definition ToString.h:124
std::string joinNamedToStringSorting(const std::set< T * > &ns, const T_BETWEEN &between)
Definition ToString.h:313
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:183
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:142
std::string toString< TrafficLightLayout >(const TrafficLightLayout &layout, std::streamsize accuracy)
Definition ToString.h:163
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:136
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition ToString.h:206
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:149
std::string toString< RightOfWay >(const RightOfWay &row, std::streamsize accuracy)
Definition ToString.h:118
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)