1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file ToString.h
11 /// @author Christian Roessel
12 /// @author Daniel Krajzewicz
13 /// @author Jakob Erdmann
14 /// @author Michael Behrisch
15 /// @date Wed, 23 Sep 2002
16 /// @version $Id$
17 ///
18 // -------------------
19 /****************************************************************************/
20 #ifndef ToString_h
21 #define ToString_h
22
23
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27
28 #include <sstream>
29 #include <string>
30 #include <iomanip>
31 #include <algorithm>
32 #include <list>
33 #include <utils/xml/SUMOXMLDefinitions.h>
34 #include <utils/common/SUMOVehicleClass.h>
35 #include <utils/common/Named.h>
36 #include <utils/distribution/Distribution_Parameterized.h>
37 #include "StdDefs.h"
38
39
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
43 /**
44 * Template for conversions from origin format to string representation
45 * (when supplied by c++/the stl)
46 */
47 template <class T>
48 inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
49 std::ostringstream oss;
50 oss.setf(std::ios::fixed, std::ios::floatfield);
51 oss << std::setprecision(accuracy);
52 oss << t;
53 return oss.str();
54 }
55
56
57 template<typename T>
58 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
59 // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
60 std::stringstream stream;
61 stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
62 return stream.str();
63 }
64
65
toString(const Named * obj,std::streamsize accuracy)66 inline std::string toString(const Named* obj, std::streamsize accuracy) {
67 UNUSED_PARAMETER(accuracy);
68 return Named::getIDSecure(obj);
69 }
70
71
72 template <>
73 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
74 UNUSED_PARAMETER(accuracy);
75 return SUMOXMLDefinitions::Tags.getString(tag);
76 }
77
78
79 template <>
80 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
81 UNUSED_PARAMETER(accuracy);
82 return SUMOXMLDefinitions::Attrs.getString(attr);
83 }
84
85
86 template <>
87 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
88 UNUSED_PARAMETER(accuracy);
89 return SUMOXMLDefinitions::NodeTypes.getString(nodeType);
90 }
91
92
93 template <>
94 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
95 UNUSED_PARAMETER(accuracy);
96 return SUMOXMLDefinitions::EdgeFunctions.getString(edgeFunc);
97 }
98
99
100 template <>
101 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
102 UNUSED_PARAMETER(accuracy);
103 return SumoVehicleClassStrings.getString(vClass);
104 }
105
106
107 template <>
108 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
109 UNUSED_PARAMETER(accuracy);
110 return SUMOXMLDefinitions::LaneSpreadFunctions.getString(lsf);
111 }
112
113 template <>
114 inline std::string toString<RightOfWay>(const RightOfWay& row, std::streamsize accuracy) {
115 UNUSED_PARAMETER(accuracy);
116 return SUMOXMLDefinitions::RightOfWayValues.getString(row);
117 }
118
119 template <>
120 inline std::string toString<FringeType>(const FringeType& fringeType, std::streamsize accuracy) {
121 UNUSED_PARAMETER(accuracy);
122 return SUMOXMLDefinitions::FringeTypeValues.getString(fringeType);
123 }
124
125 template <>
126 inline std::string toString<PersonMode>(const PersonMode& personMode, std::streamsize accuracy) {
127 UNUSED_PARAMETER(accuracy);
128 return SUMOXMLDefinitions::PersonModeValues.getString(personMode);
129 }
130
131 template <>
132 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
133 UNUSED_PARAMETER(accuracy);
134 return SUMOXMLDefinitions::LinkStates.getString(linkState);
135 }
136
137
138 template <>
139 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
140 UNUSED_PARAMETER(accuracy);
141 return SUMOXMLDefinitions::LinkDirections.getString(linkDir);
142 }
143
144
145 template <>
146 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
147 UNUSED_PARAMETER(accuracy);
148 return SUMOXMLDefinitions::TrafficLightTypes.getString(type);
149 }
150
151
152 template <>
153 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
154 UNUSED_PARAMETER(accuracy);
155 return SUMOXMLDefinitions::LaneChangeModels.getString(model);
156 }
157
158 template <>
159 inline std::string toString<LateralAlignment>(const LateralAlignment& latA, std::streamsize accuracy) {
160 UNUSED_PARAMETER(accuracy);
161 return SUMOXMLDefinitions::LateralAlignments.getString(latA);
162 }
163
164 template <>
165 inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
166 UNUSED_PARAMETER(accuracy);
167 std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
168 bool hadOne = false;
169 std::ostringstream oss;
170 for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
171 if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
172 if (hadOne) {
173 oss << "|";
174 } else {
175 hadOne = true;
176 }
177 oss << (*it);
178 }
179 }
180 return oss.str();
181 }
182
183 template <>
184 inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
185 return dist.toStr(accuracy);
186 }
187
188 template <typename V>
189 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
190 return toString<V>(v.begin(), v.end(), accuracy);
191 }
192
193 template <typename V>
194 inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
195 UNUSED_PARAMETER(accuracy);
196 std::ostringstream oss;
197 for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
198 if (it != b) {
199 oss << " ";
200 }
201 oss << Named::getIDSecure(*it);
202 }
203 return oss.str();
204 }
205
206 template <typename V>
207 inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
208 return toString<V>(v.begin(), v.end(), accuracy);
209 }
210
211 template <typename V>
212 inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
213 UNUSED_PARAMETER(accuracy);
214 std::ostringstream oss;
215 for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
216 if (it != b) {
217 oss << " ";
218 }
219 oss << Named::getIDSecure(*it);
220 }
221 return oss.str();
222 }
223
224
225
226 //template <typename V>
227 //inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
228 // return toString<V>(v.begin(), v.end(), accuracy);
229 //}
230 //
231 //
232 //template <typename V>
233 //inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
234 // UNUSED_PARAMETER(accuracy);
235 // std::ostringstream oss;
236 // for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
237 // if (it != b) {
238 // oss << " ";
239 // }
240 // oss << Named::getIDSecure(*it);
241 // }
242 // return oss.str();
243 //}
244
245
246 template <typename T, typename T_BETWEEN>
247 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
248 std::ostringstream oss;
249 bool connect = false;
250 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
251 if (connect) {
252 oss << toString(between, accuracy);
253 } else {
254 connect = true;
255 }
256 oss << toString(*it, accuracy);
257 }
258 return oss.str();
259 }
260
261
262 template <typename T, typename T_BETWEEN>
263 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
264 std::vector<T> sorted(v);
265 std::sort(sorted.begin(), sorted.end());
266 return joinToString(sorted, between, accuracy);
267 }
268
269
270 template <typename T, typename T_BETWEEN>
joinNamedToStringSorting(const std::set<T * > & ns,const T_BETWEEN & between)271 inline std::string joinNamedToStringSorting(const std::set<T*>& ns, const T_BETWEEN& between) {
272 std::vector<std::string> ids;
273 for (T* n : ns) {
274 ids.push_back(Named::getIDSecure(n));
275 }
276 return joinToStringSorting(ids, between);
277 }
278
279
280 template <typename T, typename C, typename T_BETWEEN>
joinNamedToString(const std::set<T *,C> & ns,const T_BETWEEN & between)281 inline std::string joinNamedToString(const std::set<T*, C>& ns, const T_BETWEEN& between) {
282 std::vector<std::string> ids;
283 for (T* n : ns) {
284 ids.push_back(Named::getIDSecure(n));
285 }
286 return joinToString(ids, between);
287 }
288
289
290 template <typename V>
291 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
292 UNUSED_PARAMETER(accuracy);
293 std::vector<std::string> ids;
294 for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
295 ids.push_back((*it)->getID());
296 }
297 return joinToStringSorting(ids, " ");
298 }
299
300
301 template <>
toString(const std::vector<int> & v,std::streamsize accuracy)302 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
303 return joinToString(v, " ", accuracy);
304 }
305
306
307 template <>
toString(const std::vector<long long int> & v,std::streamsize accuracy)308 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
309 return joinToString(v, " ", accuracy);
310 }
311
312
313 template <>
toString(const std::vector<double> & v,std::streamsize accuracy)314 inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
315 return joinToString(v, " ", accuracy);
316 }
317
318
319 template <typename T, typename T_BETWEEN>
320 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
321 std::ostringstream oss;
322 bool connect = false;
323 for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
324 if (connect) {
325 oss << toString(between, accuracy);
326 } else {
327 connect = true;
328 }
329 oss << toString(*it, accuracy);
330 }
331 return oss.str();
332 }
333
334
335 template <>
toString(const std::vector<std::string> & v,std::streamsize)336 inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
337 return joinToString(v, " ");
338 }
339
340
341 template <>
toString(const std::set<std::string> & v,std::streamsize)342 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
343 return joinToString(v, " ");
344 }
345
346
347 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
348 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
349 std::ostringstream oss;
350 bool connect = false;
351 for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
352 if (connect) {
353 oss << toString(between, accuracy);
354 } else {
355 connect = true;
356 }
357 oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
358 }
359 return oss.str();
360 }
361
362
363 template <>
toString(const std::map<std::string,std::string> & v,std::streamsize)364 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
365 return joinToString(v, ", ", ":");
366 }
367
368
369 #endif
370
371 /****************************************************************************/
372
373