Line data Source code
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 : /****************************************************************************/
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 146506547 : inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
47 146506547 : std::ostringstream oss;
48 : oss.setf(std::ios::fixed, std::ios::floatfield);
49 146506547 : oss << std::setprecision(accuracy);
50 122794705 : oss << t;
51 146506547 : return oss.str();
52 146506547 : }
53 :
54 :
55 : template<typename T>
56 2002 : 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 2002 : std::stringstream stream;
59 4004 : stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
60 2002 : return stream.str();
61 2002 : }
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 17083311 : inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
72 : UNUSED_PARAMETER(accuracy);
73 17083311 : return SUMOXMLDefinitions::Tags.getString(tag);
74 : }
75 :
76 :
77 : template <>
78 66741144 : inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
79 : UNUSED_PARAMETER(accuracy);
80 66741144 : return SUMOXMLDefinitions::Attrs.getString(attr);
81 : }
82 :
83 :
84 : template <>
85 68033 : inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
86 : UNUSED_PARAMETER(accuracy);
87 68033 : return SUMOXMLDefinitions::NodeTypes.getString(nodeType);
88 : }
89 :
90 :
91 : template <>
92 104887 : inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
93 : UNUSED_PARAMETER(accuracy);
94 104887 : return SUMOXMLDefinitions::EdgeFunctions.getString(edgeFunc);
95 : }
96 :
97 :
98 : template <>
99 1598 : inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
100 : UNUSED_PARAMETER(accuracy);
101 1598 : return SumoVehicleClassStrings.getString(vClass);
102 : }
103 :
104 :
105 : template <>
106 67264 : inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
107 : UNUSED_PARAMETER(accuracy);
108 67264 : return SUMOXMLDefinitions::LaneSpreadFunctions.getString(lsf);
109 : }
110 :
111 : template <>
112 9303 : inline std::string toString<ParkingType>(const ParkingType& pt, std::streamsize accuracy) {
113 : UNUSED_PARAMETER(accuracy);
114 9303 : return SUMOXMLDefinitions::ParkingTypes.getString(pt);
115 : }
116 :
117 : template <>
118 32 : inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
119 : UNUSED_PARAMETER(accuracy);
120 32 : return SUMOXMLDefinitions::RightOfWayValues.getString(row);
121 : }
122 :
123 : template <>
124 318 : inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
125 : UNUSED_PARAMETER(accuracy);
126 318 : return SUMOXMLDefinitions::FringeTypeValues.getString(fringeType);
127 : }
128 :
129 : template <>
130 1118 : inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
131 : UNUSED_PARAMETER(accuracy);
132 1118 : return SUMOXMLDefinitions::PersonModeValues.getString(personMode);
133 : }
134 :
135 : template <>
136 179700 : inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
137 : UNUSED_PARAMETER(accuracy);
138 179700 : return SUMOXMLDefinitions::LinkStates.getString(linkState);
139 : }
140 :
141 :
142 : template <>
143 865607 : inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
144 : UNUSED_PARAMETER(accuracy);
145 865607 : return SUMOXMLDefinitions::LinkDirections.getString(linkDir);
146 : }
147 :
148 :
149 : template <>
150 2171 : inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
151 : UNUSED_PARAMETER(accuracy);
152 2171 : 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 26 : inline std::string toString<InsertionCheck>(const InsertionCheck& check, std::streamsize accuracy) {
165 : UNUSED_PARAMETER(accuracy);
166 26 : return SUMOXMLDefinitions::InsertionChecks.getString(check);
167 : }
168 :
169 :
170 : template <>
171 2711 : inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
172 : UNUSED_PARAMETER(accuracy);
173 2711 : return SUMOXMLDefinitions::LaneChangeModels.getString(model);
174 : }
175 :
176 : template <>
177 92 : inline std::string toString<LatAlignmentDefinition>(const LatAlignmentDefinition& lad, std::streamsize accuracy) {
178 : UNUSED_PARAMETER(accuracy);
179 92 : switch (lad) {
180 : case LatAlignmentDefinition::RIGHT:
181 0 : return "right";
182 : case LatAlignmentDefinition::CENTER:
183 82 : return "center";
184 : case LatAlignmentDefinition::ARBITRARY:
185 9 : return "arbitrary";
186 : case LatAlignmentDefinition::NICE:
187 0 : return "nice";
188 : case LatAlignmentDefinition::COMPACT:
189 1 : return "compact";
190 : case LatAlignmentDefinition::LEFT:
191 0 : return "left";
192 : case LatAlignmentDefinition::GIVEN:
193 : case LatAlignmentDefinition::DEFAULT:
194 : default:
195 0 : return "";
196 : }
197 : }
198 :
199 : template <>
200 14841 : inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
201 : UNUSED_PARAMETER(accuracy);
202 14841 : std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
203 : bool hadOne = false;
204 14841 : std::ostringstream oss;
205 296820 : for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
206 281979 : if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
207 23039 : if (hadOne) {
208 8202 : oss << "|";
209 : } else {
210 : hadOne = true;
211 : }
212 : oss << (*it);
213 : }
214 : }
215 14841 : return oss.str();
216 14841 : }
217 :
218 : template <>
219 : inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
220 428 : 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 679660 : return toString<V>(v.begin(), v.end(), accuracy);
226 : }
227 :
228 :
229 : template <typename V>
230 680996 : 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 680996 : std::ostringstream oss;
233 3667644 : for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
234 2986648 : if (it != b) {
235 2311475 : oss << " ";
236 : }
237 5973296 : oss << Named::getIDSecure(*it);
238 : }
239 680996 : return oss.str();
240 680996 : }
241 :
242 : template <typename V>
243 : inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
244 3 : return toString<V>(v.begin(), v.end(), accuracy);
245 : }
246 :
247 : template <typename V>
248 3 : 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 3 : std::ostringstream oss;
251 7 : for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
252 4 : if (it != b) {
253 2 : oss << " ";
254 : }
255 8 : oss << Named::getIDSecure(*it);
256 : }
257 3 : return oss.str();
258 3 : }
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 18215617 : inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
284 18215617 : std::ostringstream oss;
285 : bool connect = false;
286 38012182 : for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
287 19796565 : if (connect) {
288 3241900 : oss << toString(between, accuracy);
289 : } else {
290 : connect = true;
291 : }
292 39593130 : oss << toString(*it, accuracy);
293 : }
294 18215617 : return oss.str();
295 18215617 : }
296 :
297 :
298 : template <typename T, typename T_BETWEEN>
299 17420 : inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
300 17420 : std::vector<T> sorted(v);
301 17420 : std::sort(sorted.begin(), sorted.end());
302 34840 : return joinToString(sorted, between, accuracy);
303 17420 : }
304 :
305 :
306 : template <typename T, typename T_BETWEEN>
307 95 : inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
308 : std::vector<std::string> ids;
309 355 : for (T* n : ns) {
310 520 : ids.push_back(Named::getIDSecure(n));
311 : }
312 190 : return joinToStringSorting(ids, between);
313 95 : }
314 :
315 :
316 : template <typename T, typename C, typename T_BETWEEN>
317 3271 : inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
318 : std::vector<std::string> ids;
319 14219 : for (T* n : ns) {
320 21896 : ids.push_back(Named::getIDSecure(n));
321 : }
322 6542 : return joinToString(ids, between);
323 3271 : }
324 :
325 :
326 : template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
327 : 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) {
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 :
342 : template <typename V>
343 4436 : inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
344 : UNUSED_PARAMETER(accuracy);
345 : std::vector<std::string> ids;
346 9138 : for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
347 4702 : ids.push_back((*it)->getID());
348 : }
349 8872 : return joinToStringSorting(ids, " ");
350 4436 : }
351 :
352 :
353 : template <>
354 : inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
355 168 : return joinToString(v, " ", accuracy);
356 : }
357 :
358 :
359 : template <>
360 : inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
361 665 : return joinToString(v, " ", accuracy);
362 : }
363 :
364 :
365 : template <>
366 : inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
367 207817 : return joinToString(v, " ", accuracy);
368 : }
369 :
370 :
371 : template <typename V, typename W>
372 : inline 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 :
388 : template <typename T, typename T_BETWEEN>
389 3717 : inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
390 3717 : std::ostringstream oss;
391 : bool connect = false;
392 8547 : for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
393 4830 : if (connect) {
394 2246 : oss << toString(between, accuracy);
395 : } else {
396 : connect = true;
397 : }
398 9660 : oss << toString(*it, accuracy);
399 : }
400 3717 : return oss.str();
401 3717 : }
402 :
403 :
404 : template <>
405 : inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
406 153364 : return joinToString(v, " ");
407 : }
408 :
409 :
410 : template <>
411 : inline std::string toString(const std::set<std::string>& v, std::streamsize) {
412 2092 : return joinToString(v, " ");
413 : }
414 :
415 :
416 : template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
417 10 : inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
418 10 : std::ostringstream oss;
419 : bool connect = false;
420 30 : for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
421 20 : if (connect) {
422 20 : oss << toString(between, accuracy);
423 : } else {
424 : connect = true;
425 : }
426 60 : oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
427 : }
428 10 : return oss.str();
429 10 : }
430 :
431 :
432 : template <>
433 : inline std::string toString(const Parameterised::Map& v, std::streamsize) {
434 : return joinToString(v, ", ", ":");
435 : }
436 :
437 : template <>
438 2120 : inline std::string toString(const MMVersion& v, std::streamsize) {
439 : // we only need higher accuracy on the minor version for hotfix releases
440 6360 : return toString(v.first) + "." + toString(v.second, 0);
441 : }
|