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