SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2015 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 "StdDefs.h"
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
52 template <class T>
53 inline std::string toString(const T& t, std::streamsize accuracy = OUTPUT_ACCURACY) {
54  std::ostringstream oss;
55  oss.setf(std::ios::fixed , std::ios::floatfield);
56  oss << std::setprecision(accuracy);
57  oss << t;
58  return oss.str();
59 }
60 
61 
62 template<typename T>
63 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
64  // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
65  std::stringstream stream;
66  stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
67  return stream.str();
68 }
69 
70 
71 template <>
72 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
73  UNUSED_PARAMETER(accuracy);
75 }
76 
77 
78 template <>
79 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
80  UNUSED_PARAMETER(accuracy);
82 }
83 
84 
85 template <>
86 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
87  UNUSED_PARAMETER(accuracy);
89 }
90 
91 
92 template <>
93 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
94  UNUSED_PARAMETER(accuracy);
96 }
97 
98 
99 template <>
100 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
101  UNUSED_PARAMETER(accuracy);
102  return SumoVehicleClassStrings.getString(vClass);
103 }
104 
105 
106 template <>
107 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
108  UNUSED_PARAMETER(accuracy);
110 }
111 
112 
113 template <>
114 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
115  UNUSED_PARAMETER(accuracy);
116  return SUMOXMLDefinitions::LinkStates.getString(linkState);
117 }
118 
119 template <>
120 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
121  UNUSED_PARAMETER(accuracy);
123 }
124 
125 template <>
126 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
127  UNUSED_PARAMETER(accuracy);
129 }
130 
131 template <>
132 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
133  UNUSED_PARAMETER(accuracy);
135 }
136 
137 
138 template <typename V>
139 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
140  return toString<V>(v.begin(), v.end(), accuracy);
141 }
142 
143 
144 template <typename V>
145 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) {
146  UNUSED_PARAMETER(accuracy);
147  std::ostringstream oss;
148  for (typename std::vector<V*>::const_iterator it = b; it != e; ++it) {
149  if (it != b) {
150  oss << " ";
151  }
152  oss << (*it)->getID();
153  }
154  return oss.str();
155 }
156 
157 
158 template <typename T, typename T_BETWEEN>
159 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
160  std::ostringstream oss;
161  bool connect = false;
162  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
163  if (connect) {
164  oss << toString(between, accuracy);
165  } else {
166  connect = true;
167  }
168  oss << toString(*it, accuracy);
169  }
170  return oss.str();
171 }
172 
173 
174 template <typename T, typename T_BETWEEN>
175 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
176  std::vector<T> sorted(v);
177  std::sort(sorted.begin(), sorted.end());
178  return joinToString(sorted, between, accuracy);
179 }
180 
181 
182 template <typename V>
183 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
184  UNUSED_PARAMETER(accuracy);
185  std::vector<std::string> ids;
186  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
187  ids.push_back((*it)->getID());
188  }
189  return joinToStringSorting(ids, " ");
190 }
191 
192 
193 template <>
194 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
195  return joinToString(v, " ", accuracy);
196 }
197 
198 
199 template <>
200 inline std::string toString(const std::vector<SUMOReal>& v, std::streamsize accuracy) {
201  return joinToString(v, " ", accuracy);
202 }
203 
204 
205 template <typename T, typename T_BETWEEN>
206 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
207  std::ostringstream oss;
208  bool connect = false;
209  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
210  if (connect) {
211  oss << toString(between, accuracy);
212  } else {
213  connect = true;
214  }
215  oss << toString(*it, accuracy);
216  }
217  return oss.str();
218 }
219 
220 template <>
221 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
222  return joinToString(v, " ");
223 }
224 
225 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
226 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) {
227  std::ostringstream oss;
228  bool connect = false;
229  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
230  if (connect) {
231  oss << toString(between, accuracy);
232  } else {
233  connect = true;
234  }
235  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
236  }
237  return oss.str();
238 }
239 
240 template <>
241 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
242  return joinToString(v, ", ", ":");
243 }
244 
245 
246 #endif
247 
248 /****************************************************************************/
249 
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:120
static StringBijection< SumoXMLNodeType > NodeTypes
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
static StringBijection< LaneSpreadFunction > LaneSpreadFunctions
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static StringBijection< LinkState > LinkStates
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:175
#define OUTPUT_ACCURACY
Definition: config.h:165
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
static StringBijection< LinkDirection > LinkDirections
LaneChangeModel
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:126
static StringBijection< TrafficLightType > TrafficLightTypes
const std::string & getString(const T key) const
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:72
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:132
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:93
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:63
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:114
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:100
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's lateral offset shal...
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition: ToString.h:79
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:159
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:107
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:86
TrafficLightType