SUMO - Simulation of Urban MObility
ToString.h
Go to the documentation of this file.
1 /****************************************************************************/
10 // -------------------
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2002-2017 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 #ifndef ToString_h
24 #define ToString_h
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <sstream>
37 #include <string>
38 #include <iomanip>
39 #include <algorithm>
42 #include <utils/common/Named.h>
44 #include <utils/geom/Position.h>
45 #include "StdDefs.h"
46 
47 
48 // ===========================================================================
49 // class definitions
50 // ===========================================================================
55 template <class T>
56 inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
57  std::ostringstream oss;
58  oss.setf(std::ios::fixed , std::ios::floatfield);
59  oss << std::setprecision(accuracy);
60  oss << t;
61  return oss.str();
62 }
63 
64 
65 template<typename T>
66 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
67  // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
68  std::stringstream stream;
69  stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
70  return stream.str();
71 }
72 
73 
74 inline std::string toString(const Named* obj, std::streamsize accuracy) {
75  UNUSED_PARAMETER(accuracy);
76  if (obj == 0) {
77  return "NULL";
78  }
79  return obj->getID();
80 }
81 
82 template <>
83 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
84  UNUSED_PARAMETER(accuracy);
86 }
87 
88 
89 template <>
90 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
91  UNUSED_PARAMETER(accuracy);
93 }
94 
95 
96 template <>
97 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
98  UNUSED_PARAMETER(accuracy);
100 }
101 
102 
103 template <>
104 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
105  UNUSED_PARAMETER(accuracy);
107 }
108 
109 
110 template <>
111 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
112  UNUSED_PARAMETER(accuracy);
113  return SumoVehicleClassStrings.getString(vClass);
114 }
115 
116 
117 template <>
118 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
119  UNUSED_PARAMETER(accuracy);
121 }
122 
123 
124 template <>
125 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
126  UNUSED_PARAMETER(accuracy);
127  return SUMOXMLDefinitions::LinkStates.getString(linkState);
128 }
129 
130 
131 template <>
132 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
133  UNUSED_PARAMETER(accuracy);
135 }
136 
137 
138 template <>
139 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
140  UNUSED_PARAMETER(accuracy);
142 }
143 
144 
145 template <>
146 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
147  UNUSED_PARAMETER(accuracy);
149 }
150 
151 template <>
152 inline std::string toString<LateralAlignment>(const LateralAlignment& latA, std::streamsize accuracy) {
153  UNUSED_PARAMETER(accuracy);
155 }
156 
157 template <>
158 inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
159  UNUSED_PARAMETER(accuracy);
160  std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
161  bool hadOne = false;
162  std::ostringstream oss;
163  for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
164  if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
165  if (hadOne) {
166  oss << "|";
167  } else {
168  hadOne = true;
169  }
170  oss << (*it);
171  }
172  }
173  return oss.str();
174 }
175 
176 template <>
177 inline std::string toString<Position>(const Position& pos, std::streamsize accuracy) {
178  UNUSED_PARAMETER(accuracy);
179  return toString(pos.x(), accuracy) + "," + toString(pos.y(), accuracy);
180 }
181 
182 template <>
183 inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
184  return dist.toStr(accuracy);
185 }
186 
187 template <typename V>
188 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
189  return toString<V>(v.begin(), v.end(), accuracy);
190 }
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 
207 //template <typename V>
208 //inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
209 // return toString<V>(v.begin(), v.end(), accuracy);
210 //}
211 //
212 //
213 //template <typename V>
214 //inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
215 // UNUSED_PARAMETER(accuracy);
216 // std::ostringstream oss;
217 // for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
218 // if (it != b) {
219 // oss << " ";
220 // }
221 // oss << Named::getIDSecure(*it);
222 // }
223 // return oss.str();
224 //}
225 
226 
227 template <typename T, typename T_BETWEEN>
228 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
229  std::ostringstream oss;
230  bool connect = false;
231  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
232  if (connect) {
233  oss << toString(between, accuracy);
234  } else {
235  connect = true;
236  }
237  oss << toString(*it, accuracy);
238  }
239  return oss.str();
240 }
241 
242 
243 template <typename T, typename T_BETWEEN>
244 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
245  std::vector<T> sorted(v);
246  std::sort(sorted.begin(), sorted.end());
247  return joinToString(sorted, between, accuracy);
248 }
249 
250 
251 template <typename V>
252 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
253  UNUSED_PARAMETER(accuracy);
254  std::vector<std::string> ids;
255  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
256  ids.push_back((*it)->getID());
257  }
258  return joinToStringSorting(ids, " ");
259 }
260 
261 
262 template <>
263 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
264  return joinToString(v, " ", accuracy);
265 }
266 
267 
268 template <>
269 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
270  return joinToString(v, " ", accuracy);
271 }
272 
273 
274 template <>
275 inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
276  return joinToString(v, " ", accuracy);
277 }
278 
279 
280 template <typename T, typename T_BETWEEN>
281 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
282  std::ostringstream oss;
283  bool connect = false;
284  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
285  if (connect) {
286  oss << toString(between, accuracy);
287  } else {
288  connect = true;
289  }
290  oss << toString(*it, accuracy);
291  }
292  return oss.str();
293 }
294 
295 
296 template <>
297 inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
298  return joinToString(v, " ");
299 }
300 
301 
302 template <>
303 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
304  return joinToString(v, " ");
305 }
306 
307 
308 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
309 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
310  std::ostringstream oss;
311  bool connect = false;
312  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
313  if (connect) {
314  oss << toString(between, accuracy);
315  } else {
316  connect = true;
317  }
318  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
319  }
320  return oss.str();
321 }
322 
323 
324 template <>
325 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
326  return joinToString(v, ", ", ":");
327 }
328 
329 
330 #endif
331 
332 /****************************************************************************/
333 
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:132
static StringBijection< SumoXMLNodeType > NodeTypes
node types
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
static StringBijection< LaneSpreadFunction > LaneSpreadFunctions
lane spread functions
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:30
const std::string & getString(const T key) const
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition: ToString.h:158
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:59
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
static StringBijection< LinkState > LinkStates
link states
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:38
std::vector< std::string > getStrings() const
LateralAlignment
Numbers representing special SUMO-XML-attribute values Information how vehicles align themselves with...
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
std::string toString< Distribution_Parameterized >(const Distribution_Parameterized &dist, std::streamsize accuracy)
Definition: ToString.h:183
static StringBijection< LinkDirection > LinkDirections
link directions
LaneChangeModel
static StringBijection< LaneChangeAction > LaneChangeActions
lane change actions
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:139
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
std::string toString< Position >(const Position &pos, std::streamsize accuracy)
Definition: ToString.h:177
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
std::string toString< SumoXMLTag >(const SumoXMLTag &tag, std::streamsize accuracy)
Definition: ToString.h:83
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:146
Base class for objects which have an id.
Definition: Named.h:46
std::string toString< LateralAlignment >(const LateralAlignment &latA, std::streamsize accuracy)
Definition: ToString.h:152
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:104
LaneChangeAction
The state of a vehicle&#39;s lane-change behavior.
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
std::string toString< LinkState >(const LinkState &linkState, std::streamsize accuracy)
Definition: ToString.h:125
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:244
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:111
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
LaneSpreadFunction
Numbers representing special SUMO-XML-attribute values Information how the edge&#39;s lateral offset shal...
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition: ToString.h:90
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static StringBijection< LaneChangeModel > LaneChangeModels
lane change models
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:228
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition: ToString.h:118
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:97
TrafficLightType