SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ToString.h
Go to the documentation of this file.
1 /****************************************************************************/
10 // -------------------
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2002-2014 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  UNUSED_PARAMETER(accuracy);
141  std::ostringstream oss;
142  for (typename std::vector<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
143  if (it != v.begin()) {
144  oss << " ";
145  }
146  oss << (*it)->getID();
147  }
148  return oss.str();
149 }
150 
151 
152 template <typename T, typename T_BETWEEN>
153 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
154  std::ostringstream oss;
155  bool connect = false;
156  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
157  if (connect) {
158  oss << toString(between, accuracy);
159  } else {
160  connect = true;
161  }
162  oss << toString(*it, accuracy);
163  }
164  return oss.str();
165 }
166 
167 
168 template <typename T, typename T_BETWEEN>
169 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
170  std::vector<T> sorted(v);
171  std::sort(sorted.begin(), sorted.end());
172  return joinToString(sorted, between, accuracy);
173 }
174 
175 
176 template <typename V>
177 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
178  UNUSED_PARAMETER(accuracy);
179  std::vector<std::string> ids;
180  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
181  ids.push_back((*it)->getID());
182  }
183  return joinToStringSorting(ids, " ");
184 }
185 
186 
187 template <>
188 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
189  return joinToString(v, " ", accuracy);
190 }
191 
192 
193 template <>
194 inline std::string toString(const std::vector<SUMOReal>& v, std::streamsize accuracy) {
195  return joinToString(v, " ", accuracy);
196 }
197 
198 
199 template <typename T, typename T_BETWEEN>
200 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
201  std::ostringstream oss;
202  bool connect = false;
203  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
204  if (connect) {
205  oss << toString(between, accuracy);
206  } else {
207  connect = true;
208  }
209  oss << toString(*it, accuracy);
210  }
211  return oss.str();
212 }
213 
214 template <>
215 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
216  return joinToString(v, " ");
217 }
218 
219 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
220 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) {
221  std::ostringstream oss;
222  bool connect = false;
223  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
224  if (connect) {
225  oss << toString(between, accuracy);
226  } else {
227  connect = true;
228  }
229  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
230  }
231  return oss.str();
232 }
233 
234 template <>
235 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
236  return joinToString(v, ", ", ":");
237 }
238 
239 
240 #endif
241 
242 /****************************************************************************/
243 
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:169
#define OUTPUT_ACCURACY
Definition: config.h:162
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:153
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