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 /****************************************************************************/
9 // -------------------
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
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>
41 #include "StdDefs.h"
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
51 template <class T>
52 inline std::string toString(const T& t, std::streamsize accuracy = OUTPUT_ACCURACY) {
53  std::ostringstream oss;
54  oss.setf(std::ios::fixed , std::ios::floatfield);
55  oss << std::setprecision(accuracy);
56  oss << t;
57  return oss.str();
58 }
59 
60 
61 template<typename T>
62 inline std::string toHex(const T i, std::streamsize numDigits = 0) {
63  // taken from http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
64  std::stringstream stream;
65  stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
66  return stream.str();
67 }
68 
69 
70 template <>
71 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
72  UNUSED_PARAMETER(accuracy);
74 }
75 
76 
77 template <>
78 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
79  UNUSED_PARAMETER(accuracy);
81 }
82 
83 
84 template <>
85 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
86  UNUSED_PARAMETER(accuracy);
88 }
89 
90 
91 template <>
92 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
93  UNUSED_PARAMETER(accuracy);
95 }
96 
97 
98 template <>
99 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
100  UNUSED_PARAMETER(accuracy);
101  return SumoVehicleClassStrings.getString(vClass);
102 }
103 
104 
105 template <>
106 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
107  UNUSED_PARAMETER(accuracy);
109 }
110 
111 
112 template <>
113 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
114  UNUSED_PARAMETER(accuracy);
115  return SUMOXMLDefinitions::LinkStates.getString(linkState);
116 }
117 
118 template <>
119 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
120  UNUSED_PARAMETER(accuracy);
122 }
123 
124 template <>
125 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
126  UNUSED_PARAMETER(accuracy);
128 }
129 
130 template <>
131 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
132  UNUSED_PARAMETER(accuracy);
134 }
135 
136 
137 template <typename V>
138 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
139  UNUSED_PARAMETER(accuracy);
140  std::ostringstream oss;
141  for (typename std::vector<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
142  if (it != v.begin()) {
143  oss << " ";
144  }
145  oss << (*it)->getID();
146  }
147  return oss.str();
148 }
149 
150 
151 template <typename T, typename T_BETWEEN>
152 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
153  std::ostringstream oss;
154  bool connect = false;
155  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
156  if (connect) {
157  oss << toString(between, accuracy);
158  } else {
159  connect = true;
160  }
161  oss << toString(*it, accuracy);
162  }
163  return oss.str();
164 }
165 
166 
167 template <typename T, typename T_BETWEEN>
168 inline std::string joinToStringSorting(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
169  std::vector<T> sorted(v);
170  std::sort(sorted.begin(), sorted.end());
171  return joinToString(sorted, between, accuracy);
172 }
173 
174 
175 template <typename V>
176 inline std::string toString(const std::set<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
177  UNUSED_PARAMETER(accuracy);
178  std::vector<std::string> ids;
179  for (typename std::set<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
180  ids.push_back((*it)->getID());
181  }
182  return joinToStringSorting(ids, " ");
183 }
184 
185 
186 template <>
187 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
188  return joinToString(v, " ", accuracy);
189 }
190 
191 
192 template <>
193 inline std::string toString(const std::vector<SUMOReal>& v, std::streamsize accuracy) {
194  return joinToString(v, " ", accuracy);
195 }
196 
197 
198 template <typename T, typename T_BETWEEN>
199 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
200  std::ostringstream oss;
201  bool connect = false;
202  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
203  if (connect) {
204  oss << toString(between, accuracy);
205  } else {
206  connect = true;
207  }
208  oss << toString(*it, accuracy);
209  }
210  return oss.str();
211 }
212 
213 template <>
214 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
215  return joinToString(v, " ");
216 }
217 
218 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
219 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) {
220  std::ostringstream oss;
221  bool connect = false;
222  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
223  if (connect) {
224  oss << toString(between, accuracy);
225  } else {
226  connect = true;
227  }
228  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
229  }
230  return oss.str();
231 }
232 
233 template <>
234 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
235  return joinToString(v, ", ", ":");
236 }
237 
238 
239 #endif
240 
241 /****************************************************************************/
242 
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:119
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:38
std::string joinToStringSorting(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:168
#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
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2)
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:125
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:71
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:52
std::string toString< LaneChangeModel >(const LaneChangeModel &model, std::streamsize accuracy)
Definition: ToString.h:131
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:92
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:62
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:113
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:99
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:78
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:152
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:106
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:85
TrafficLightType