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-2016 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>
43 #include <utils/geom/Position.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 = OUTPUT_ACCURACY) {
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 template <>
74 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
75  UNUSED_PARAMETER(accuracy);
77 }
78 
79 
80 template <>
81 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
82  UNUSED_PARAMETER(accuracy);
84 }
85 
86 
87 template <>
88 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
89  UNUSED_PARAMETER(accuracy);
91 }
92 
93 
94 template <>
95 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
96  UNUSED_PARAMETER(accuracy);
98 }
99 
100 
101 template <>
102 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
103  UNUSED_PARAMETER(accuracy);
104  return SumoVehicleClassStrings.getString(vClass);
105 }
106 
107 
108 template <>
109 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
110  UNUSED_PARAMETER(accuracy);
112 }
113 
114 
115 template <>
116 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
117  UNUSED_PARAMETER(accuracy);
118  return SUMOXMLDefinitions::LinkStates.getString(linkState);
119 }
120 
121 
122 template <>
123 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
124  UNUSED_PARAMETER(accuracy);
126 }
127 
128 
129 template <>
130 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
131  UNUSED_PARAMETER(accuracy);
133 }
134 
135 
136 template <>
137 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
138  UNUSED_PARAMETER(accuracy);
140 }
141 
142 template <>
143 inline std::string toString<LateralAlignment>(const LateralAlignment& latA, std::streamsize accuracy) {
144  UNUSED_PARAMETER(accuracy);
146 }
147 
148 template <>
149 inline std::string toString<LaneChangeAction>(const LaneChangeAction& action, std::streamsize accuracy) {
150  UNUSED_PARAMETER(accuracy);
151  std::vector<std::string> strings = SUMOXMLDefinitions::LaneChangeActions.getStrings();
152  bool hadOne = false;
153  std::ostringstream oss;
154  for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it) {
155  if ((action & SUMOXMLDefinitions::LaneChangeActions.get(*it)) != 0) {
156  if (hadOne) {
157  oss << "|";
158  } else {
159  hadOne = true;
160  }
161  oss << (*it);
162  }
163  }
164  return oss.str();
165 }
166 
167 template <typename V>
168 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
169  return toString<V>(v.begin(), v.end(), accuracy);
170 }
171 
172 
173 template <typename V>
174 inline std::string toString(const typename std::vector<V*>::const_iterator& b, const typename std::vector<V*>::const_iterator& e, std::streamsize accuracy = OUTPUT_ACCURACY) {
175  UNUSED_PARAMETER(accuracy);
176  std::ostringstream oss;
177  for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
178  if (it != b) {
179  oss << " ";
180  }
181  oss << Named::getIDSecure(*it);
182  }
183  return oss.str();
184 }
185 
186 
187 template <typename T, typename T_BETWEEN>
188 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
189  std::ostringstream oss;
190  bool connect = false;
191  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
192  if (connect) {
193  oss << toString(between, accuracy);
194  } else {
195  connect = true;
196  }
197  oss << toString(*it, accuracy);
198  }
199  return oss.str();
200 }
201 
202 
203 template <typename T, typename T_BETWEEN>
204 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
205  std::vector<T> sorted(v);
206  std::sort(sorted.begin(), sorted.end());
207  return joinToString(sorted, between, accuracy);
208 }
209 
210 
211 template <typename V>
212 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
213  UNUSED_PARAMETER(accuracy);
214  std::vector<std::string> ids;
215  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
216  ids.push_back((*it)->getID());
217  }
218  return joinToStringSorting(ids, " ");
219 }
220 
221 
222 template <>
223 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
224  return joinToString(v, " ", accuracy);
225 }
226 
227 
228 template <>
229 inline std::string toString(const std::vector<long long int>& v, std::streamsize accuracy) {
230  return joinToString(v, " ", accuracy);
231 }
232 
233 
234 template <>
235 inline std::string toString(const std::vector<SUMOReal>& v, std::streamsize accuracy) {
236  return joinToString(v, " ", accuracy);
237 }
238 
239 
240 template <typename T, typename T_BETWEEN>
241 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
242  std::ostringstream oss;
243  bool connect = false;
244  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
245  if (connect) {
246  oss << toString(between, accuracy);
247  } else {
248  connect = true;
249  }
250  oss << toString(*it, accuracy);
251  }
252  return oss.str();
253 }
254 
255 
256 template <>
257 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
258  return joinToString(v, " ");
259 }
260 
261 
262 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
263 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = OUTPUT_ACCURACY) {
264  std::ostringstream oss;
265  bool connect = false;
266  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
267  if (connect) {
268  oss << toString(between, accuracy);
269  } else {
270  connect = true;
271  }
272  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
273  }
274  return oss.str();
275 }
276 
277 
278 template <>
279 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
280  return joinToString(v, ", ", ":");
281 }
282 
283 
284 #endif
285 
286 /****************************************************************************/
287 
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:123
static StringBijection< SumoXMLNodeType > NodeTypes
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
static StringBijection< LaneSpreadFunction > LaneSpreadFunctions
const std::string & getString(const T key) const
std::string toString< LaneChangeAction >(const LaneChangeAction &action, std::streamsize accuracy)
Definition: ToString.h:149
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.
static StringBijection< LinkState > LinkStates
#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...
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:204
#define OUTPUT_ACCURACY
Definition: config.h:163
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
static StringBijection< LinkDirection > LinkDirections
LaneChangeModel
static StringBijection< LaneChangeAction > LaneChangeActions
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:130
static StringBijection< TrafficLightType > TrafficLightTypes
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:74
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:137
std::string toString< LateralAlignment >(const LateralAlignment &latA, std::streamsize accuracy)
Definition: ToString.h:143
static StringBijection< LateralAlignment > LateralAlignments
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:95
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:116
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:102
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:81
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:188
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
static StringBijection< LaneChangeModel > LaneChangeModels
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition: ToString.h:109
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:88
TrafficLightType