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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // -------------------
21 /****************************************************************************/
22 #ifndef ToString_h
23 #define ToString_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <sstream>
36 #include <string>
37 #include <iomanip>
38 #include <algorithm>
39 #include <list>
42 #include <utils/common/Named.h>
44 #include "StdDefs.h"
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
54 template <class T>
55 inline std::string toString(const T& t, std::streamsize accuracy = gPrecision) {
56  std::ostringstream oss;
57  oss.setf(std::ios::fixed , std::ios::floatfield);
58  oss << std::setprecision(accuracy);
59  oss << t;
60  return oss.str();
61 }
62 
63 
64 template<typename T>
65 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
66  // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
67  std::stringstream stream;
68  stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
69  return stream.str();
70 }
71 
72 
73 inline std::string toString(const Named* obj, std::streamsize accuracy) {
74  UNUSED_PARAMETER(accuracy);
75  return Named::getIDSecure(obj);
76 }
77 
78 template <>
79 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
80  UNUSED_PARAMETER(accuracy);
82 }
83 
84 
85 template <>
86 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
87  UNUSED_PARAMETER(accuracy);
89 }
90 
91 
92 template <>
93 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
94  UNUSED_PARAMETER(accuracy);
96 }
97 
98 
99 template <>
100 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
101  UNUSED_PARAMETER(accuracy);
103 }
104 
105 
106 template <>
107 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
108  UNUSED_PARAMETER(accuracy);
109  return SumoVehicleClassStrings.getString(vClass);
110 }
111 
112 
113 template <>
114 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
115  UNUSED_PARAMETER(accuracy);
117 }
118 
119 
120 template <>
121 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
122  UNUSED_PARAMETER(accuracy);
123  return SUMOXMLDefinitions::LinkStates.getString(linkState);
124 }
125 
126 
127 template <>
128 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
129  UNUSED_PARAMETER(accuracy);
131 }
132 
133 
134 template <>
135 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
136  UNUSED_PARAMETER(accuracy);
138 }
139 
140 
141 template <>
142 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
143  UNUSED_PARAMETER(accuracy);
145 }
146 
147 template <>
148 inline std::string toString<LateralAlignment>(const LateralAlignment& latA, std::streamsize accuracy) {
149  UNUSED_PARAMETER(accuracy);
151 }
152 
153 template <>
154 inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
155  UNUSED_PARAMETER(accuracy);
156  std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
157  bool hadOne = false;
158  std::ostringstream oss;
159  for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
160  if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
161  if (hadOne) {
162  oss << "|";
163  } else {
164  hadOne = true;
165  }
166  oss << (*it);
167  }
168  }
169  return oss.str();
170 }
171 
172 template <>
173 inline std::string toString<Distribution_Parameterized>(const Distribution_Parameterized& dist, std::streamsize accuracy) {
174  return dist.toStr(accuracy);
175 }
176 
177 template <typename V>
178 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = gPrecision) {
179  return toString<V>(v.begin(), v.end(), accuracy);
180 }
181 
182 template <typename V>
183 inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
184  UNUSED_PARAMETER(accuracy);
185  std::ostringstream oss;
186  for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
187  if (it != b) {
188  oss << " ";
189  }
190  oss << Named::getIDSecure(*it);
191  }
192  return oss.str();
193 }
194 
195 template <typename V>
196 inline std::string toString(const std::list<V*>& v, std::streamsize accuracy = gPrecision) {
197  return toString<V>(v.begin(), v.end(), accuracy);
198 }
199 
200 template <typename V>
201 inline std::string toString(const typename std::list<V*>::const_iterator& b, const typename std::list<V*>::const_iterator& e, std::streamsize accuracy = gPrecision) {
202  UNUSED_PARAMETER(accuracy);
203  std::ostringstream oss;
204  for (typename std::list<V*>::const_iterator it = b; it != e; ++it) {
205  if (it != b) {
206  oss << " ";
207  }
208  oss << Named::getIDSecure(*it);
209  }
210  return oss.str();
211 }
212 
213 
214 
215 //template <typename V>
216 //inline std::string toString(const std::vector<V>& v, std::streamsize accuracy = gPrecision) {
217 // return toString<V>(v.begin(), v.end(), accuracy);
218 //}
219 //
220 //
221 //template <typename V>
222 //inline std::string toString(const typename std::vector<V>::const_iterator& b, const typename std::vector<V>::const_iterator& e, std::streamsize accuracy = gPrecision) {
223 // UNUSED_PARAMETER(accuracy);
224 // std::ostringstream oss;
225 // for (typename std::vector<V>::const_iterator it = b; it != e; ++it) {
226 // if (it != b) {
227 // oss << " ";
228 // }
229 // oss << Named::getIDSecure(*it);
230 // }
231 // return oss.str();
232 //}
233 
234 
235 template <typename T, typename T_BETWEEN>
236 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
237  std::ostringstream oss;
238  bool connect = false;
239  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
240  if (connect) {
241  oss << toString(between, accuracy);
242  } else {
243  connect = true;
244  }
245  oss << toString(*it, accuracy);
246  }
247  return oss.str();
248 }
249 
250 
251 template <typename T, typename T_BETWEEN>
252 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
253  std::vector<T> sorted(v);
254  std::sort(sorted.begin(), sorted.end());
255  return joinToString(sorted, between, accuracy);
256 }
257 
258 
259 template <typename V>
260 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = gPrecision) {
261  UNUSED_PARAMETER(accuracy);
262  std::vector<std::string> ids;
263  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
264  ids.push_back((*it)->getID());
265  }
266  return joinToStringSorting(ids, " ");
267 }
268 
269 
270 template <>
271 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
272  return joinToString(v, " ", accuracy);
273 }
274 
275 
276 template <>
277 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
278  return joinToString(v, " ", accuracy);
279 }
280 
281 
282 template <>
283 inline std::string toString(const std::vector<double>& v, std::streamsize accuracy) {
284  return joinToString(v, " ", accuracy);
285 }
286 
287 
288 template <typename T, typename T_BETWEEN>
289 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = gPrecision) {
290  std::ostringstream oss;
291  bool connect = false;
292  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
293  if (connect) {
294  oss << toString(between, accuracy);
295  } else {
296  connect = true;
297  }
298  oss << toString(*it, accuracy);
299  }
300  return oss.str();
301 }
302 
303 
304 template <>
305 inline std::string toString(const std::vector<std::string>& v, std::streamsize) {
306  return joinToString(v, " ");
307 }
308 
309 
310 template <>
311 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
312  return joinToString(v, " ");
313 }
314 
315 
316 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
317 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = gPrecision) {
318  std::ostringstream oss;
319  bool connect = false;
320  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
321  if (connect) {
322  oss << toString(between, accuracy);
323  } else {
324  connect = true;
325  }
326  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
327  }
328  return oss.str();
329 }
330 
331 
332 template <>
333 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
334  return joinToString(v, ", ", ":");
335 }
336 
337 
338 #endif
339 
340 /****************************************************************************/
341 
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:128
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:29
const std::string & getString(const T key) const
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition: ToString.h:154
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
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static StringBijection< LinkState > LinkStates
link states
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
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:173
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:55
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:135
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
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:79
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:142
Base class for objects which have an id.
Definition: Named.h:54
std::string toString< LateralAlignment >(const LateralAlignment &latA, std::streamsize accuracy)
Definition: ToString.h:148
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:100
LaneChangeAction
The state of a vehicle&#39;s lane-change behavior.
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
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:121
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:252
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:107
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:86
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:236
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition: ToString.h:114
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:93
TrafficLightType