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-2024 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<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
131 UNUSED_PARAMETER(accuracy);
133}
134
135template <>
136inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
137 UNUSED_PARAMETER(accuracy);
139}
140
141
142template <>
143inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
144 UNUSED_PARAMETER(accuracy);
146}
147
148
149template <>
150inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
151 UNUSED_PARAMETER(accuracy);
153}
154
155
156template <>
157inline std::string toString<TrafficLightLayout>(const TrafficLightLayout& layout, std::streamsize accuracy) {
158 UNUSED_PARAMETER(accuracy);
160}
161
162
163template <>
164inline std::string toString<InsertionCheck>(const InsertionCheck& check, std::streamsize accuracy) {
165 UNUSED_PARAMETER(accuracy);
167}
168
169
170template <>
171inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
172 UNUSED_PARAMETER(accuracy);
174}
175
176template <>
177inline std::string toString<LatAlignmentDefinition>(const LatAlignmentDefinition& lad, std::streamsize accuracy) {
178 UNUSED_PARAMETER(accuracy);
179 switch (lad) {
181 return "right";
183 return "center";
185 return "arbitrary";
187 return "nice";
189 return "compact";
191 return "left";
194 default:
195 return "";
196 }
197}
198
199template <>
200inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
201 UNUSED_PARAMETER(accuracy);
202 std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
203 bool hadOne = false;
204 std::ostringstream oss;
205 for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
206 if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
207 if (hadOne) {
208 oss << "|";
209 } else {
210 hadOne = true;
211 }
212 oss << (*it);
213 }
214 }
215 return oss.str();
216}
217
218template <>
219inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
220 return dist.toStr(accuracy);
221}
222
223template <typename V>
224inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
225 return toString<V>(v.begin(), v.end(), accuracy);
226}
227
228
229template <typename V>
230inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
231 UNUSED_PARAMETER(accuracy);
232 std::ostringstream oss;
233 for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
234 if (it != b) {
235 oss << " ";
236 }
237 oss << Named::getIDSecure(*it);
238 }
239 return oss.str();
240}
241
242template <typename V>
243inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
244 return toString<V>(v.begin(), v.end(), accuracy);
245}
246
247template <typename V>
248inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
249 UNUSED_PARAMETER(accuracy);
250 std::ostringstream oss;
251 for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
252 if (it != b) {
253 oss << " ";
254 }
255 oss << Named::getIDSecure(*it);
256 }
257 return oss.str();
258}
259
260
261
262//template <typename V>
263//inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
264// return toString<V>(v.begin(), v.end(), accuracy);
265//}
266//
267//
268//template <typename V>
269//inline 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
281
282template <typename T, typename T_BETWEEN>
283inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
284 std::ostringstream oss;
285 bool connect = false;
286 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
287 if (connect) {
288 oss << toString(between, accuracy);
289 } else {
290 connect = true;
291 }
292 oss << toString(*it, accuracy);
293 }
294 return oss.str();
295}
296
297
298template <typename T, typename T_BETWEEN>
299inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
300 std::vector<T> sorted(v);
301 std::sort(sorted.begin(), sorted.end());
302 return joinToString(sorted, between, accuracy);
303}
304
305
306template <typename T, typename T_BETWEEN>
307inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
308 std::vector<std::string> ids;
309 for (T* n : ns) {
310 ids.push_back(Named::getIDSecure(n));
311 }
312 return joinToStringSorting(ids, between);
313}
314
315
316template <typename T, typename C, typename T_BETWEEN>
317inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
318 std::vector<std::string> ids;
319 for (T* n : ns) {
320 ids.push_back(Named::getIDSecure(n));
321 }
322 return joinToString(ids, between);
323}
324
325
326template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
327inline std::string joinNamedToString(const std::map<KEY, VAL, ComparatorIdLess>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
328 std::ostringstream oss;
329 bool connect = false;
330 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
331 if (connect) {
332 oss << toString(between, accuracy);
333 } else {
334 connect = true;
335 }
336 oss << Named::getIDSecure(it->first) << between_keyval << toString(it->second, accuracy);
337 }
338 return oss.str();
339}
340
341
342template <typename V>
343inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
344 UNUSED_PARAMETER(accuracy);
345 std::vector<std::string> ids;
346 for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
347 ids.push_back((*it)->getID());
348 }
349 return joinToStringSorting(ids, " ");
350}
351
352
353template <>
354inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
355 return joinToString(v, " ", accuracy);
356}
357
358
359template <>
360inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
361 return joinToString(v, " ", accuracy);
362}
363
364
365template <>
366inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
367 return joinToString(v, " ", accuracy);
368}
369
370
371template <typename V, typename W>
372inline std::string toString(const std::vector<std::pair<V, W> >& v, std::streamsize accuracy = gPrecision, const std::string& between = ";", const std::string& between2 = ",") {
373 std::ostringstream oss;
374 oss << std::setprecision(accuracy);
375 bool connect = false;
376 for (auto it : v) {
377 if (connect) {
378 oss << toString(between, accuracy);
379 } else {
380 connect = true;
381 }
382 oss << toString(it.first) << between2 << toString(it.second);
383 }
384 return oss.str();
385}
386
387
388template <typename T, typename T_BETWEEN>
389inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
390 std::ostringstream oss;
391 bool connect = false;
392 for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
393 if (connect) {
394 oss << toString(between, accuracy);
395 } else {
396 connect = true;
397 }
398 oss << toString(*it, accuracy);
399 }
400 return oss.str();
401}
402
403
404template <>
405inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
406 return joinToString(v, " ");
407}
408
409
410template <>
411inline std::string toString(const std::set<std::string>& v, std::streamsize) {
412 return joinToString(v, " ");
413}
414
415
416template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
417inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
418 std::ostringstream oss;
419 bool connect = false;
420 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
421 if (connect) {
422 oss << toString(between, accuracy);
423 } else {
424 connect = true;
425 }
426 oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
427 }
428 return oss.str();
429}
430
431
432template <>
433inline std::string toString(const Parameterised::Map& v, std::streamsize) {
434 return joinToString(v, ", ", ":");
435}
436
437template <>
438inline std::string toString(const MMVersion& v, std::streamsize) {
439 // we only need higher accuracy on the minor version for hotfix releases
440 return toString(v.first) + "." + toString(v.second, 0);
441}
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...
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:26
#define UNUSED_PARAMETER(x)
Definition StdDefs.h:30
std::pair< int, double > MMVersion
(M)ajor/(M)inor version for written networks and default version for loading
Definition StdDefs.h:67
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:317
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:299
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition ToString.h:150
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:219
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:283
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition ToString.h:171
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:164
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:307
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:177
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:136
std::string toString< TrafficLightLayout >(const TrafficLightLayout &layout, std::streamsize accuracy)
Definition ToString.h:157
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:130
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition ToString.h:200
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:143
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< FringeType > FringeTypeValues
fringe types
const std::string & getString(int key) const
const std::string & getString(const T key) const
std::vector< std::string > getStrings() const