Eclipse SUMO - Simulation of Urban MObility
ToString.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2002-2022 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
228template <typename V>
229inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
230 UNUSED_PARAMETER(accuracy);
231 std::ostringstream oss;
232 for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
233 if (it != b) {
234 oss << " ";
235 }
236 oss << Named::getIDSecure(*it);
237 }
238 return oss.str();
239}
240
241template <typename V>
242inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
243 return toString<V>(v.begin(), v.end(), accuracy);
244}
245
246template <typename V>
247inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
248 UNUSED_PARAMETER(accuracy);
249 std::ostringstream oss;
250 for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
251 if (it != b) {
252 oss << " ";
253 }
254 oss << Named::getIDSecure(*it);
255 }
256 return oss.str();
257}
258
259
260
261//template <typename V>
262//inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
263// return toString<V>(v.begin(), v.end(), accuracy);
264//}
265//
266//
267//template <typename V>
268//inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
269// UNUSED_PARAMETER(accuracy);
270// std::ostringstream oss;
271// for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
272// if (it != b) {
273// oss << " ";
274// }
275// oss << Named::getIDSecure(*it);
276// }
277// return oss.str();
278//}
279
280
281template <typename T, typename T_BETWEEN>
282inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
283 std::ostringstream oss;
284 bool connect = false;
285 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
286 if (connect) {
287 oss << toString(between, accuracy);
288 } else {
289 connect = true;
290 }
291 oss << toString(*it, accuracy);
292 }
293 return oss.str();
294}
295
296
297template <typename T, typename T_BETWEEN>
298inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
299 std::vector<T> sorted(v);
300 std::sort(sorted.begin(), sorted.end());
301 return joinToString(sorted, between, accuracy);
302}
303
304
305template <typename T, typename T_BETWEEN>
306inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
307 std::vector<std::string> ids;
308 for (T* n : ns) {
309 ids.push_back(Named::getIDSecure(n));
310 }
311 return joinToStringSorting(ids, between);
312}
313
314
315template <typename T, typename C, typename T_BETWEEN>
316inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
317 std::vector<std::string> ids;
318 for (T* n : ns) {
319 ids.push_back(Named::getIDSecure(n));
320 }
321 return joinToString(ids, between);
322}
323
324
325template <typename V>
326inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
327 UNUSED_PARAMETER(accuracy);
328 std::vector<std::string> ids;
329 for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
330 ids.push_back((*it)->getID());
331 }
332 return joinToStringSorting(ids, " ");
333}
334
335
336template <>
337inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
338 return joinToString(v, " ", accuracy);
339}
340
341
342template <>
343inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
344 return joinToString(v, " ", accuracy);
345}
346
347
348template <>
349inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
350 return joinToString(v, " ", accuracy);
351}
352
353
354template <typename T, typename T_BETWEEN>
355inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
356 std::ostringstream oss;
357 bool connect = false;
358 for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
359 if (connect) {
360 oss << toString(between, accuracy);
361 } else {
362 connect = true;
363 }
364 oss << toString(*it, accuracy);
365 }
366 return oss.str();
367}
368
369
370template <>
371inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
372 return joinToString(v, " ");
373}
374
375
376template <>
377inline std::string toString(const std::set<std::string>& v, std::streamsize) {
378 return joinToString(v, " ");
379}
380
381
382template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
383inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
384 std::ostringstream oss;
385 bool connect = false;
386 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
387 if (connect) {
388 oss << toString(between, accuracy);
389 } else {
390 connect = true;
391 }
392 oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
393 }
394 return oss.str();
395}
396
397
398template <>
399inline std::string toString(const Parameterised::Map& v, std::streamsize) {
400 return joinToString(v, ", ", ":");
401}
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
TrafficLightType
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.
LaneChangeModel
RightOfWay
algorithms for computing right of way
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:25
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
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:316
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:298
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:282
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:306
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
Definition: Parameterised.h:45
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 StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static StringBijection< TrafficLightLayout > TrafficLightLayouts
traffic light layouts
static StringBijection< ParkingType > ParkingTypes
parking types
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< LaneChangeModel > LaneChangeModels
lane change models
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
static StringBijection< LinkDirection > LinkDirections
link directions
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
static StringBijection< FringeType > FringeTypeValues
fringe types
const std::string & getString(const T key) const
std::vector< std::string > getStrings() const
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.hpp:4451