Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/common/ToString.h
169678 views
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
/****************************************************************************/
14
/// @file ToString.h
15
/// @author Christian Roessel
16
/// @author Daniel Krajzewicz
17
/// @author Jakob Erdmann
18
/// @author Michael Behrisch
19
/// @date Wed, 23 Sep 2002
20
///
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
#include <utils/xml/SUMOXMLDefinitions.h>
31
#include <utils/common/SUMOVehicleClass.h>
32
#include <utils/common/Named.h>
33
#include <utils/distribution/Distribution_Parameterized.h>
34
#include <utils/vehicle/SUMOVTypeParameter.h>
35
#include "StdDefs.h"
36
37
38
// ===========================================================================
39
// class definitions
40
// ===========================================================================
41
/**
42
* Template for conversions from origin format to string representation
43
* (when supplied by c++/the stl)
44
*/
45
template <class T>
46
inline 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
55
template<typename T>
56
inline 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
64
inline std::string toString(const Named* obj, std::streamsize accuracy) {
65
UNUSED_PARAMETER(accuracy);
66
return Named::getIDSecure(obj);
67
}
68
69
70
template <>
71
inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
72
UNUSED_PARAMETER(accuracy);
73
return SUMOXMLDefinitions::Tags.getString(tag);
74
}
75
76
77
template <>
78
inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
79
UNUSED_PARAMETER(accuracy);
80
return SUMOXMLDefinitions::Attrs.getString(attr);
81
}
82
83
84
template <>
85
inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
86
UNUSED_PARAMETER(accuracy);
87
return SUMOXMLDefinitions::NodeTypes.getString(nodeType);
88
}
89
90
91
template <>
92
inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
93
UNUSED_PARAMETER(accuracy);
94
return SUMOXMLDefinitions::EdgeFunctions.getString(edgeFunc);
95
}
96
97
98
template <>
99
inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
100
UNUSED_PARAMETER(accuracy);
101
return SumoVehicleClassStrings.getString(vClass);
102
}
103
104
105
template <>
106
inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
107
UNUSED_PARAMETER(accuracy);
108
return SUMOXMLDefinitions::LaneSpreadFunctions.getString(lsf);
109
}
110
111
template <>
112
inline std::string toString<ParkingType>(const ParkingType& pt, std::streamsize accuracy) {
113
UNUSED_PARAMETER(accuracy);
114
return SUMOXMLDefinitions::ParkingTypes.getString(pt);
115
}
116
117
template <>
118
inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
119
UNUSED_PARAMETER(accuracy);
120
return SUMOXMLDefinitions::RightOfWayValues.getString(row);
121
}
122
123
template <>
124
inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
125
UNUSED_PARAMETER(accuracy);
126
return SUMOXMLDefinitions::FringeTypeValues.getString(fringeType);
127
}
128
129
template <>
130
inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
131
UNUSED_PARAMETER(accuracy);
132
return SUMOXMLDefinitions::PersonModeValues.getString(personMode);
133
}
134
135
template <>
136
inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
137
UNUSED_PARAMETER(accuracy);
138
return SUMOXMLDefinitions::LinkStates.getString(linkState);
139
}
140
141
142
template <>
143
inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
144
UNUSED_PARAMETER(accuracy);
145
return SUMOXMLDefinitions::LinkDirections.getString(linkDir);
146
}
147
148
149
template <>
150
inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
151
UNUSED_PARAMETER(accuracy);
152
return SUMOXMLDefinitions::TrafficLightTypes.getString(type);
153
}
154
155
156
template <>
157
inline std::string toString<TrafficLightLayout>(const TrafficLightLayout& layout, std::streamsize accuracy) {
158
UNUSED_PARAMETER(accuracy);
159
return SUMOXMLDefinitions::TrafficLightLayouts.getString(layout);
160
}
161
162
163
template <>
164
inline std::string toString<InsertionCheck>(const InsertionCheck& check, std::streamsize accuracy) {
165
UNUSED_PARAMETER(accuracy);
166
return SUMOXMLDefinitions::InsertionChecks.getString(check);
167
}
168
169
170
template <>
171
inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
172
UNUSED_PARAMETER(accuracy);
173
return SUMOXMLDefinitions::LaneChangeModels.getString(model);
174
}
175
176
template <>
177
inline std::string toString<LatAlignmentDefinition>(const LatAlignmentDefinition& lad, std::streamsize accuracy) {
178
UNUSED_PARAMETER(accuracy);
179
switch (lad) {
180
case LatAlignmentDefinition::RIGHT:
181
return "right";
182
case LatAlignmentDefinition::CENTER:
183
return "center";
184
case LatAlignmentDefinition::ARBITRARY:
185
return "arbitrary";
186
case LatAlignmentDefinition::NICE:
187
return "nice";
188
case LatAlignmentDefinition::COMPACT:
189
return "compact";
190
case LatAlignmentDefinition::LEFT:
191
return "left";
192
case LatAlignmentDefinition::GIVEN:
193
case LatAlignmentDefinition::DEFAULT:
194
default:
195
return "";
196
}
197
}
198
199
template <>
200
inline 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
218
template <>
219
inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
220
return dist.toStr(accuracy);
221
}
222
223
template <typename V>
224
inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
225
return toString<V>(v.begin(), v.end(), accuracy);
226
}
227
228
229
template <typename V>
230
inline 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
242
template <typename V>
243
inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
244
return toString<V>(v.begin(), v.end(), accuracy);
245
}
246
247
template <typename V>
248
inline 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
282
template <typename T, typename T_BETWEEN>
283
inline 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
298
template <typename T, typename T_BETWEEN>
299
inline 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
306
template <typename T, typename T_BETWEEN>
307
inline 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
template <typename T, typename T_BETWEEN>
316
inline std::string joinNamedToStringSorting(const std::set<T*, ComparatorIdLess>& 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 joinToStringSorting(ids, between);
322
}
323
324
325
template <typename T, typename C, typename T_BETWEEN>
326
inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
327
std::vector<std::string> ids;
328
for (T* n : ns) {
329
ids.push_back(Named::getIDSecure(n));
330
}
331
return joinToString(ids, between);
332
}
333
334
335
template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
336
inline std::string joinNamedToString(const std::map<KEY, VAL, ComparatorIdLess>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
337
std::ostringstream oss;
338
bool connect = false;
339
for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
340
if (connect) {
341
oss << toString(between, accuracy);
342
} else {
343
connect = true;
344
}
345
oss << Named::getIDSecure(it->first) << between_keyval << toString(it->second, accuracy);
346
}
347
return oss.str();
348
}
349
350
351
template <typename V>
352
inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
353
UNUSED_PARAMETER(accuracy);
354
std::vector<std::string> ids;
355
for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
356
ids.push_back((*it)->getID());
357
}
358
return joinToStringSorting(ids, " ");
359
}
360
361
362
template <>
363
inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
364
return joinToString(v, " ", accuracy);
365
}
366
367
368
template <>
369
inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
370
return joinToString(v, " ", accuracy);
371
}
372
373
374
template <>
375
inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
376
return joinToString(v, " ", accuracy);
377
}
378
379
380
template <typename V, typename W>
381
inline std::string toString(const std::vector<std::pair<V, W> >& v, std::streamsize accuracy = gPrecision, const std::string& between = ";", const std::string& between2 = ",") {
382
std::ostringstream oss;
383
oss << std::setprecision(accuracy);
384
bool connect = false;
385
for (auto it : v) {
386
if (connect) {
387
oss << toString(between, accuracy);
388
} else {
389
connect = true;
390
}
391
oss << toString(it.first) << between2 << toString(it.second);
392
}
393
return oss.str();
394
}
395
396
397
template <typename T, typename T_BETWEEN>
398
inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
399
std::ostringstream oss;
400
bool connect = false;
401
for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
402
if (connect) {
403
oss << toString(between, accuracy);
404
} else {
405
connect = true;
406
}
407
oss << toString(*it, accuracy);
408
}
409
return oss.str();
410
}
411
412
413
template <>
414
inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
415
return joinToString(v, " ");
416
}
417
418
419
template <>
420
inline std::string toString(const std::set<std::string>& v, std::streamsize) {
421
return joinToString(v, " ");
422
}
423
424
425
template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
426
inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
427
std::ostringstream oss;
428
bool connect = false;
429
for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
430
if (connect) {
431
oss << toString(between, accuracy);
432
} else {
433
connect = true;
434
}
435
oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
436
}
437
return oss.str();
438
}
439
440
441
template <>
442
inline std::string toString(const Parameterised::Map& v, std::streamsize) {
443
return joinToString(v, ", ", ":");
444
}
445
446
template <>
447
inline std::string toString(const MMVersion& v, std::streamsize) {
448
// we only need higher accuracy on the minor version for hotfix releases
449
return toString(v.first) + "." + toString(v.second, 0);
450
}
451
452